Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds
R2026bPolyspace® Test™ supports two approaches for testing and profiling C/C++ code:
Project-based approach: Add your source code to a Polyspace Platform project and create tests in a tabular format or as code using the C/C++ xUnit API. Build both source files and tests using the project. You can also enable code profiling to collect metrics such as code coverage, execution time, memory use or run-time error occurrences.
Self-managed approach: Author tests using the Polyspace Test xUnit API and build them using your existing toolchain.
In the project-based approach, building sources and tests is managed using options in your project configuration. In contrast, the self-managed approach requires you to handle test builds and code profiling within your current build system. This topic describes the file paths you need for setting up self-managed builds.
Sources to Compile for Testing and Code Profiling
To build tests authored using the xUnit API or run source code profiling, you need the following files and folders with test and profiling macro definitions.
Source file
<polyspaceroot>/polyspace/pstest/pstunit/src/pstest.c(or<polyspaceroot>/polyspace/pstest/pstunit/src/pstest.cppif you want the macros compiled as C++ code). In the examples on self-managed builds, this path is referred to as<PSTEST_SOURCE>.xUnit test include folder
<polyspaceroot>/polyspace/pstest/pstunit/include. In the examples on self-managed builds, this path is referred to as<PSTEST_INCLUDE>. This folder contains the headerpstunit.h, which you must include in your test files in order to use the Polyspace Test xUnit API.Profiling include folder
<polyspaceroot>\polyspace\psprofile\include. In the examples on self-managed builds, this path is referred to as<PSPROFILE_INCLUDE>. This folder contains the headerpsprofile.h, which you might include in your sources to stream data from a target to host.
Here, <polyspaceroot> is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026b
on Windows®. The list above uses Windows-style paths. Adjust them as needed for other operating systems.
Precompiled Libraries to Include for Code Profiling
To collect code profiling metrics such as code coverage, execution time, memory use, or run-time error occurrences, you first instrument your source files using the polyspace-code-profiler command. The instrumentation step injects macros into copies of your source files and then compiles the instrumented source files instead of the original ones. To generate the final executable, you must link the compiled sources to a library containing definitions of the
instrumentation macros.
For your convenience, Polyspace
Test provides a precompiled library for certain compilers. The library path depends on the compiler you intend to use. In the examples on self-managed builds, this path is referred to as <PSTEST_LIB>.
Microsoft® Visual C++ (Windows):
<polyspaceroot>\polyspace\pstest\runtime\lib\win64\import\microsoft\libmwpstest_runtime.libMinGW (Windows):
<polyspaceroot>\polyspace\pstest\runtime\lib\win64\import\mingw64\libmwpstest_runtime.libGCC (Linux®):
<polyspaceroot>/polyspace/pstest/runtime/lib/glnxa64/static/libmwpstest_runtime.a
Here, <polyspaceroot> is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026b
on Windows. The list above uses Windows-style paths. Adjust them as needed for other operating systems.
If you do not use one of these compilers or the precompiled libraries do not work for you, you can also create a library by compiling the source files containing the instrumentation macros. For more information, see Create Library for Code Profiling Using Self-Managed Builds.
Adding Files and Folders to Self-Managed Builds
To build your sources for testing, code profiling or test executable monitoring, you can refer to the above files in your compile commands, makefiles, or other build scripts. If you use the CMake package available with Polyspace Test to generate makefiles, you do not need to set these paths explicitly. Instead, you specify link targets and functions available with the CMake package. For more information, see Use CMake to Execute Tests Authored Using Polyspace Test xUnit API.
You can refer to these paths as variables in build scripts. The syntax for defining environment variables differs depending on the operating system and scripts:
In Shell scripts on UNIX®-like systems (Linux and Mac OS X), use the
exportcommand to define variables, for instance:You reference the environment variable by prefixing its name with a dollar sign (export PSTEST_SOURCE="/usr/local/Polyspace/R2026b/polyspace/pstest/pstunit/src/pstest.c"$). For example, you can refer to the above environment variablePSTEST_SOURCEas$PSTEST_SOURCE.In batch files on Windows, use the
setcommand to define variables, for instance:set PSTEST_SOURCE="C:\Program Files\Polyspace\R2026b\polyspace\pstest\pstunit\src\pstest.c"You reference the environment variable by enclosing its name in percent signs (
%). For example, you can refer to the above environment variablePSTEST_SOURCEas%PSTEST_SOURCE%.
See Also
polyspace-code-profiler | polyspace-test -monitor