Testing with CTest
Introduction
CTest is a testing tool that comes bundled with CMake. It allows you to:
- Define tests in your
CMakeLists.txt
. - Run tests and generate reports.
- Integrate with testing frameworks like GoogleTest.
Key Concepts
enable_testing()
: Enables testing for the project.add_test()
: Defines a test.ctest
: The command-line tool to run tests.
Code Sample
- Create
CMakeLists.txt
or copy it fromChapter 1
and add the lines to enable testing:
- Write
main.cpp
or copy it fromChapter 1
- Build and run the tests:
Output:
Code Sample : Advanced Tests with GoogleTest
Now let’s integrate GoogleTest for more sophisticated testing.
- Update your
CMakeLists.txt
- Write
test.cpp
- Build and run the tests
Output: