ctest is a test driver program that is part of the CMake build system. It is designed to execute tests for CMake-based projects and provides a standardized approach to running and managing tests. Ctest works in conjunction with the CMakeLists.txt file, where test configurations and test cases are defined.
Here are the key features and functionalities of ctest:
- Test Execution: Ctest allows you to run tests for your project with ease. It automatically discovers and executes the tests defined in your CMakeLists.txt file. You can specify different test types, such as unit tests, integration tests, or system tests, and ctest will execute them accordingly.
- Test Configuration: Ctest provides a range of options to configure and control the test execution process. You can set various parameters for testing, including the number of parallel test jobs, the testing timeout duration, and the testing environment variables. This flexibility allows you to customize the testing process to suit your project’s requirements.
- Test Reporting: Ctest generates detailed test reports after each test run. It provides information about the status of each test case, including whether the test passed, failed, or was skipped. Ctest also collects and reports additional test metadata, such as test duration, test output, and any error messages encountered during the test execution.
- Test Dashboard: Ctest can generate a test dashboard that summarizes the overall test results in a graphical format. The dashboard provides a concise overview of the test outcomes, allowing you to quickly identify any failing or problematic test cases. The dashboard can be customized and integrated into continuous integration (CI) systems for easy monitoring of test results over time.
- Test Scripting: Ctest supports scripting capabilities that enable you to define complex test workflows. You can use CTest scripts to perform pre-test and post-test actions, set up test fixtures, and run custom test procedures. This feature allows you to automate the entire testing process and incorporate additional logic into your test suite.
- Integration with CDash: Ctest integrates seamlessly with CDash, a web-based software testing server. CDash collects and aggregates test results from multiple ctest runs, allowing you to track the history and trends of your test suite. CDash provides a centralized location for viewing and analyzing test reports and facilitates collaboration among team members.
Ctest is a valuable tool for managing and executing tests in CMake-based projects. It simplifies the test execution process, provides comprehensive test reporting, and supports advanced test configuration and scripting. By using ctest, you can ensure the quality and reliability of your codebase by regularly running and monitoring your tests.
ctest Command Examples
1. Run all tests defined in the CMake project, executing 4 jobs at a time in parallel:
# ctest -j4 --output-on-failure
2. Show a list of available tests:
# ctest -N
3. Run a single test based on its name, or filter on a regular expression:
# ctest --output-on-failure -R '^test_name$'