Run C/C++ Tests on Target Using Self-Managed Builds
R2026bIn the context of test execution in Polyspace® Test™, the word target refers to embedded systems with specific processor architecture, cross-compilation tools, and channels for communication with a host computer.
The steps below show how to leverage existing toolchain setup for on-target execution to run tests authored using the Polyspace Test C/C++ xUnit API.
Prerequisites
This topic assumes that:
You have source files and tests authored using the Polyspace Test C/C++ xUnit API. For information on authoring tests, see Write C/C++ Unit Tests Using Polyspace Test xUnit API and Run Tests at Command Line.
You have an existing toolchain setup to build and run your source files on the target. This topic shows how to incorporate the xUnit tests into that build.
Overview
You can retrieve test results from the target using one of two modes:
| Monitor Mode | Manual Conversion Mode | |
|---|---|---|
| Result conversion | Automatic — polyspace-test -monitor retrieves and converts results during execution. | Manual — You retrieve the .mrf output from the target and convert it on the host using polyspace-test -convert. |
| Configuration macro | PST_MONITOR_MODE 1 | PST_EMBEDDED_MODE 1 |
| Communication requirement | Requires a live communication channel (serial port, TCP) between host and target during execution. | No live connection required — collect output after execution completes. |
| Code profiling | Also produces code profiling results (.psprof) if you enable profiling in the same configuration. See Collect Test and Code Profiling Results from Target Using Self-Managed Builds. | Captures test results only. To also collect code profiling data, perform a separate execution with a profiling-specific configuration. See Collect Test and Code Profiling Results from Target Using Self-Managed Builds. |
If you can stream data from your target through a serial or TCP/IP port, you can use either mode. The monitor mode is recommended as it automates the retrieval and conversion of test results. If your port is not supported for the monitor mode, use the manual conversion mode. For more information on the ports supported by monitor mode, see polyspace-test -monitor.
Alternatively, you can add C/C++ xUnit tests to a Polyspace Platform project and execute the tests using this project. In this workflow, once you register your target, the test execution on target and retrieval of test results are managed entirely by the test execution framework in Polyspace Test. You do not have to manually perform the test build, communication with target, retrieval of test results, or any of the subsequent conversions. For more information on the completely managed workflow, see Run C/C++ Tests on Target in Polyspace Platform User Interface.
Monitor Mode
In monitor mode, polyspace-test -monitor launches the test executable on the target, streams the test output through a communication channel (serial or TCP), and automatically converts the results into a .pstestr file.
Configure Test Execution
Define a configuration header file to enable monitor mode:
In a header file
pstest_config.h, set these configuration macros:/* Enable monitor mode for test execution */ #define PST_MONITOR_MODE 1 /* Disable execution time calculation, otherwise requires timer function definition */ #define PST_ENABLE_EXECUTION_TIME 0 extern void uart_write(const char * str, unsigned long len); /* Transfer string of specific length */ #define PST_WRITE(str, len) uart_write(str, len)The macro
PST_MONITOR_MODEconfigures the test executable to produce output in a machine-readable format with newline delimiters, suitable for streaming through a communication channel. The macroPST_WRITEmaps to a function that sends data through the communication channel. For more information on the configuration macros, see Configuration Macros in Polyspace Test API for C/C++ Code.Define the implementation of the function mapped to the macro
PST_WRITEin a source file.For example, this source file defines the function
uart_write()to send a string of characters through the UART interface using the functionHAL_UART_Transmit()from the STM32 HAL library:#include "stm32746g_discovery.h" extern UART_HandleTypeDef huart1; void uart_write(const char * str, unsigned long len) { HAL_UART_Transmit(&huart1, (const uint8_t*)str, len, HAL_MAX_DELAY); }
Build Tests on Host Using Cross-Compiler
Build your tests and create a test executable by using your cross-compiler. Define the macro PST_USER_CONFIG during build so that the macros in the configuration file pstest_config.h get used during build. For more information on the macro PST_USER_CONFIG, see Configuration Macros in Polyspace Test API for C/C++ Code.
For example, to cross-compile using the GNU® toolchain for an Arm target, use this command at the command line:
arm-none-eabi-gcc src/example.c tests/mytest.c <PSTEST_SRC> -D PST_USER_CONFIG -I <PSTEST_INC><PSTEST_SRC>is the path to the file<polyspaceroot>\polyspace\pstest\pstunit\src\pstest.ccontaining definitions of xUnit API macros.<PSTEST_INC>is the path to the folder<polyspaceroot>\polyspace\pstest\pstunit\includecontaining the xUnit API headers.
Here, <polyspaceroot> is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026b.
Run Tests on Target
After creating the test executable on the host, use polyspace-test -monitor to run the executable on the target, stream the results, and convert them automatically.
For instance, suppose that you are running tests in Windows® on an STM32 board using the STM32_Programmer_CLI.exe
command:
"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe" --connect port=swd --erase all --download unittests.hex --gopolyspace-test -monitor -com serial -read-timeout 15 -com-timeout 30 -port COM6 -baud-rate 115200 -parity none -results-dir resultsFolder -- "C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe" --connect port=swd --erase all --download unittests.hex --gopolyspace-test -monitor command converts the results into a .pstestr file and stores it in the folder specified for the option -results-dir.For more information, see polyspace-test -monitor.
Review Results
Open the .pstestr file from the results folder and review the results using one of these methods:
Open the
.pstestrfile in the Polyspace Platform user interface. For more information, see Review C/C++ Test Execution Results.Upload the
.pstestrfile to the Polyspace Access™ web interface. For more information, see Upload Results to Polyspace Access.Generate an HTML report:
polyspace-test -report -html -report-dir reportFolder resultsFolder
Manual Conversion Mode
In manual conversion mode, you run the test executable on the target, collect the machine-readable output (.mrf file), and convert it to a .pstestr file on the host using polyspace-test -convert. This mode does not require a live communication channel during execution.
Configure Test Execution
Define a configuration header file to enable embedded mode:
In a header file
pstest_config.h, set these configuration macros:/* Enable embedded mode for compact machine-readable output */ #define PST_EMBEDDED_MODE 1 /* Disable execution time calculation, otherwise requires timer function definition */ #define PST_ENABLE_EXECUTION_TIME 0 extern void pst_write(const char * str, unsigned long len); /* Transfer string of specific length */ #define PST_WRITE(str, len) pst_write(str, len)The macro
PST_EMBEDDED_MODEconfigures the test executable to produce compact machine-readable output. The macroPST_WRITEmaps to a function that stores or sends the output data. For more information on the configuration macros, see Configuration Macros in Polyspace Test API for C/C++ Code.Define the implementation of the function mapped to the macro
PST_WRITEin a source file.For example, this source file defines the function
pst_write()to store test output in a buffer that is written to a file after execution completes:#include <stdio.h> #include <string.h> static unsigned char outBuffer[16384]; static unsigned int outIndex = 0; void pst_write(const char * str, unsigned long len) { if ((outIndex + len) <= sizeof(outBuffer)) { memcpy(&outBuffer[outIndex], str, len); outIndex += len; } } void save_results(const char * filename) { FILE *f = fopen(filename, "wb"); if (f) { fwrite(outBuffer, 1, outIndex, f); fclose(f); } }The exact implementation depends on your target. On targets with file system access, you can write directly to a file. On targets without file system access, you can store the output in a buffer and retrieve it through a debug probe or other mechanism after execution completes.
Build Tests on Host Using Cross-Compiler
Build your tests and create a test executable by using your cross-compiler. Define the macro PST_USER_CONFIG during build so that the macros in the configuration file pstest_config.h get used during build. For more information on the macro PST_USER_CONFIG, see Configuration Macros in Polyspace Test API for C/C++ Code.
For example, to cross-compile using the GNU toolchain for an Arm target, use this command at the command line:
arm-none-eabi-gcc src/example.c tests/mytest.c <PSTEST_SRC> -D PST_USER_CONFIG -I <PSTEST_INC><PSTEST_SRC>is the path to the file<polyspaceroot>\polyspace\pstest\pstunit\src\pstest.ccontaining definitions of xUnit API macros.<PSTEST_INC>is the path to the folder<polyspaceroot>\polyspace\pstest\pstunit\includecontaining the xUnit API headers.
Here, <polyspaceroot> is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026b.
Run Tests and Retrieve Results
After creating the test executable on the host, run it on the target using your regular methods of code execution. For instance, if you use options in an IDE to transfer your compiled code to the target board, you can continue to use the same method to transfer the test executable.
Collect the test output from the target and save it as an .mrf file. The method for collecting the output depends on your target:
If you use a terminal emulator program such as Tera Term or PuTTY, specify in the program settings that command execution output must be stored in a file with the
.mrfextension.If your target does not support running commands at a command-line interface, collect the binary output of the test executable through a debug probe or other mechanism and save it as an
.mrffile on the host computer.
Convert and Review Results
Convert the .mrf file into a .pstestr file by entering this command at the command line:
polyspace-test -convert -results-dir resultsFolder results.mrfresultsFolderis the folder where Polyspace stores the output.pstestrfile.results.mrfis the.mrffile collected from the target.
Review the results using one of these methods:
Open the
.pstestrfile in the Polyspace Platform user interface. For more information, see Review C/C++ Test Execution Results.Upload the
.pstestrfile to the Polyspace Access web interface. For more information, see Upload Results to Polyspace Access.Generate an HTML report:
polyspace-test -report -html -report-dir reportFolder resultsFolder
See Also
Topics
- Automate C/C++ Test Execution on Targets Using Polyspace Platform Projects
- Run C/C++ Tests on Target in Polyspace Platform User Interface