Use CMake to Execute Tests Authored Using Polyspace Test xUnit API
CMake is a third-party, open source tool for build process management that allows you to specify the build instructions for your code base in a platform and toolchain independent CMake language. When using CMake, you:
Create a
CMakeLists.txtfile that contains the build instructions in the CMake language.Invoke the
cmakecommand that uses theCMakeLists.txtfile to generate standard build files. These include makefile and Ninja, as well as Microsoft® Visual Studio® and Xcode project builds.
To learn more about CMake, see the CMake Tutorial.
Polyspace® Test™ includes a CMake package that supplements the standard CMake API with additional variables, targets, and functions. This example shows how to use this package to:
Link source and test targets to the
pstunittarget to create an executableConvert test results to standard Polyspace Test formats
Generate XML and HTML reports from test results
Prerequisites
To use the Polyspace Test CMake package, you must have CMake version 13.0.0 or higher.
In this example, you instruct CMake to generate a makefile. To run this example, you must have make installed on your system.
Alternatively, you can instruct CMake generate an IDE build system, for example one that uses Microsoft
Visual Studio. For more information on how to modify the cmake command in this example, refer to the CMake documentation.
Example Files
The files used for this example are available in the folder polyspaceroot/polyspace/examples/pstest/Getting_Started_Example. Here, polyspaceroot is the Polyspace
Test installation folder, for example /usr/local/Polyspace/R2025a. Copy this folder to a writable location in your file system, such as /usr/data/Getting_Started_Example.
Create CMakeLists.txt File
In the copied Getting_Started_Example folder, create a CMakeLists.txt file that contains these commands:
# Find pstest CMake package.
cmake_minimum_required(VERSION 3.13.0)
project(MyProject)
find_package(pstest PATHS "<polyspaceroot>/polyspace/pstest/pstunit/cmake")
# Create lib target for source code. Instrument this target for MC/DC coverage.
add_library(ecocar sources/utils.c)
target_include_directories(ecocar PUBLIC includes)
# Create exe target for pstunit tests.
enable_testing()
add_executable(mypstest tests/test.c)
target_link_libraries(mypstest pstest::pstest_c ecocar)
# Add test and set output file names.
set(CMAKE_CTEST_ARGUMENTS "-O;out/pstest_results.mrf;--verbose")
add_test(NAME mypstest COMMAND mypstest -format mrf)
# Create targets for result and report generation.
pstest_add_convert_target(convert RESULTS_DIRS out CONVERTED_RESULTS_DIR results)
pstest_add_report_target(report RESULTS_DIRS results REPORTS_DIR reports HTML XML)In this file, you use the standard CMake functions cmake_minimum_required, project, set, find_package, add_library, add_executable, target_include_directories, target_link_libraries, enable_testing, and add_test.
You also use these targets and functions that are included with the pstest CMake package:
pstest::pstest_ctarget — You link the test executablemypstestagainst this targetpstest_add_convert_targetfunction — You use this function to create a target for converting test and code profiling results to the standard Polyspace Test formatpstest_add_report_targetfunction — You use this function to create a target for generating reports from test and code profiling results
Build and Run Tests
Create a build folder named Getting_Started_Example_build at the same level as the Getting_Started_Example folder. From the build folder, run the cmake command to generate a build system for your source and test files. Use the -G option to specify the CMake generator. For example, on Linux® platform, run this command in the shell:
cmake -G "Unix Makefiles" ../Getting_Started_ExampleCMake generates a makefile and other supporting files in the Getting_Started_Example_build folder. Run make to execute the makefile and build the executable mypstest. Then invoke the make command three more times to execute the test, convert, and report targets.
make
make test
make convert
make reportThe Getting_Started_Example_build folder now contains the subfolders out, results, and reports. Each of these subfolders contains the test execution results in a certain format.
For example, you can now open the .pstestr file in the results subfolder using the Polyspace Platform user interface. Alternately, you can inspect the XML and HTML files in the reports subfolder in a web browser.
In particular, note that all tests have passed except addLatestReading_test_1_fail.
See Also
pstest_add_convert_target | pstest_add_report_target
Topics
- Calculate Code Coverage of xUnit API-Based Tests Using CMake
- Run GoogleTest Tests Using Polyspace Test CMake Package