Skip to content
Snippets Groups Projects
Commit b69ac182 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

add ctest example

parent 7a4ebc10
Branches
No related tags found
1 merge request!2add ctest example
......@@ -42,4 +42,8 @@ check-recipes:
- cmake -S 07_git_build -B 07_build
- cmake --build 07_build
- 07_build/git-hash
\ No newline at end of file
- 07_build/git-hash
- cmake -S 08_ctest -B 08_build
- cmake --build 08_build
- ctest --test-dir 08_build || FAILED=true
cmake_minimum_required(VERSION 3.20)
project(08_ctest
LANGUAGES CXX)
# create an executable that returns success
add_executable(success success.cpp)
# create an executable that returns failure
add_executable(failure failure.cpp)
# Make sure to include CTest before adding any test with add_test
# otherwise the tests will not be registered.
include(CTest)
# Check if this project is the main project and if testing is enabled.
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
# Adding a CMake executable target to the list of tests.
add_test(
NAME success
COMMAND success )
# Adding a CMake executable target to the list of test that will fail.
add_test(
NAME failure
COMMAND failure )
# Adding the same executable as above but now via a direct call to the executable.
add_test(
NAME failure_will_fail
COMMAND ./failure )
# Since we know this test is supposed to fail let's add the corresponding
# property. This way it will not count as a failure.
set_property(
TEST failure_will_fail
PROPERTY WILL_FAIL TRUE )
endif()
# How to manage all the tests in your project?
[CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) is a powerful utility to orchestrate all your tests.
You can register your test executables with CTest and run them via `ctest`. After running all tests it will print
statistics and inform you which tests returned a non-zero exit code.
Testing in CMake is enabled via `include(CTest)`. If tests are built and executed, can be controlled via the
`BUILD_TESTING` CMake variable. After enabling testing you can add tests via
[add_test](https://cmake.org/cmake/help/latest/command/add_test.html). It can handle CMake executable targets as
well as direct paths to binaries. Tests can be further specialized via
[properties](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#test-properties). This includes
tests that are supposed to fail or labels to only run a subset of the tests.
However, CTest is not a unit testing library. If you want library support to write tests have a look at the
following libraries.
For C++
- [googletest](https://cliutils.gitlab.io/modern-cmake/chapters/testing/googletest.html)
- [Catch2](https://cliutils.gitlab.io/modern-cmake/chapters/testing/catch.html)
\ No newline at end of file
#include <cstdlib>
int main()
{
return EXIT_FAILURE;
}
#include <cstdlib>
int main()
{
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment