Unit tests binary
unit_tests
This module defines a unit_tests build target and helpers to ease
configuring, developing and testing C++ projects.
- include (unit_tests)
Does all the work.
include (unit_tests)
Creates a
unit_teststarget, linked with GoogleTest library (without the predefined main)Also defines a
run_unit_teststarget to execute the tests. The call will generate a report (XML format) namedunit_tests-results.xmlin${data_base_path}/tests-reports.If not set, ${data_base_path} will be set as ${CMAKE_BINARY_DIR}
Additional arguments can be passed by defining the user-defined target property UNIT_TESTS_ARGS.
Note that tests will be executed and report will be generated only on first call, or if the
unit_testsbinary has been updated since last call. Thus the call torun_unit_testsshould be sufficent to get an up-to-date report, with no duplicated execution.For use-cases where the unit_tests binary is used to execute functional tests, with fully automated checks (meaning no rebuild is necessary for updating the tests), or if only the input files are modified, the binary cannot see the modified tests, so the witness file generated by run_unit_tests will have to be removed (search for .witness files in the build directory).
Usage example:
include(unit_tests) set_target_properties (unit_tests PROPERTIES UNIT_TESTS_ARGS "--lang;${PROJECT_SOURCE_DIR}/lang;--gtest_filter=-ft.*") target_sources (unit_tests PRIVATE src/my_first_test.cc src/my_second_test.cc) # in case user does not need custom main target_link_libraries (unit_tests PRIVATE GTest::gtest_main)