diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c4526..6f42770 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,13 +51,27 @@ if(FEATURE_TESTS) ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY) - # generate a main function for the test executable - enable_testing() - add_library(catch2_test_common INTERFACE) - target_find_dependencies(catch2_test_common INTERFACE_CONFIG Catch2) - target_link_libraries(catch2_test_common INTERFACE Catch2::Catch2) - target_compile_definitions(catch2_test_common INTERFACE CATCH_CONFIG_MAIN DO_NOT_USE_WMAIN) - include(Catch) + include(CTest) + # common settings for library test + if(${ENABLE_COVERAGE}) + set(COVERAGE_ARGS --reporter xml) + endif() + add_test_config(common + DEPENDENCIES_CONFIG + Catch2 + + LIBRARIES + my_project_options + my_project_warnings + Catch2::Catch2 + + COMPILE_DEFINITIONS + CATCH_CONFIG_MAIN + DO_NOT_USE_WMAIN + + EXECUTE_ARGS + ${COVERAGE_ARGS} + ) endif() if(FEATURE_DOCS) diff --git a/my_exe/test/CMakeLists.txt b/my_exe/test/CMakeLists.txt index e12a641..de9a87c 100644 --- a/my_exe/test/CMakeLists.txt +++ b/my_exe/test/CMakeLists.txt @@ -1,28 +1,2 @@ -# test dependencies -include(CTest) - -# ---------------------------------------------------------------------------------------------------------------------- -# test the executable part -add_test( - # calling my_exe executable directly - NAME my_exe_test - COMMAND my_exe "" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -# ---------------------------------------------------------------------------------------------------------------------- -# test the library part -add_executable(my_exe_lib_tests "./tests.cpp") -target_link_libraries( - my_exe_lib_tests - PRIVATE my_project_warnings - my_project_options - catch2_test_common - my_exe_lib) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_exe_lib_tests ${COVERAGE_ARGS}) +add_library_test(my_exe_lib tests CONFIGS common SOURCES tests.cpp) +add_executable_test(my_exe no_arg) diff --git a/my_header_lib/test/CMakeLists.txt b/my_header_lib/test/CMakeLists.txt index fb008c6..7cf10f2 100644 --- a/my_header_lib/test/CMakeLists.txt +++ b/my_header_lib/test/CMakeLists.txt @@ -1,23 +1 @@ -# test dependencies -include(CTest) - -# test executable -add_executable(my_header_lib_tests "./tests.cpp") - -target_link_libraries( - my_header_lib_tests - PRIVATE my_header_lib - my_project_warnings - my_project_options - catch2_test_common) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_header_lib_tests ${COVERAGE_ARGS}) - -# constexpr tests -add_subdirectory("./constexpr") +add_library_test(my_header_lib tests CONFIGS common SOURCES tests.cpp) diff --git a/my_lib/test/CMakeLists.txt b/my_lib/test/CMakeLists.txt index f13bc54..47db2a0 100644 --- a/my_lib/test/CMakeLists.txt +++ b/my_lib/test/CMakeLists.txt @@ -1,23 +1,4 @@ -# test dependencies -include(CTest) - -# test executable -add_executable(my_lib_tests "./tests.cpp") - -target_link_libraries( - my_lib_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_lib_tests ${COVERAGE_ARGS}) +add_library_test(my_lib tests CONFIGS common SOURCES tests.cpp) # constexpr tests add_subdirectory("./constexpr") diff --git a/my_lib/test/constexpr/CMakeLists.txt b/my_lib/test/constexpr/CMakeLists.txt index 541ad9f..050b01b 100644 --- a/my_lib/test/constexpr/CMakeLists.txt +++ b/my_lib/test/constexpr/CMakeLists.txt @@ -1,25 +1,24 @@ # Add a file containing a set of constexpr tests -add_executable(my_lib_constexpr_tests "./constexpr_tests.cpp") - -target_link_libraries( - my_lib_constexpr_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) - -catch_discover_tests(my_lib_constexpr_tests ${COVERAGE_ARGS}) +add_library_test(my_lib constexpr_tests CONFIGS common SOURCES constexpr_tests.cpp) # Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when # things go wrong with the constexpr testing -add_executable(my_lib_relaxed_constexpr_tests "./constexpr_tests.cpp") +add_library_test(my_lib relaxed_constexpr_tests + CONFIGS + common + + SOURCES + constexpr_tests.cpp -target_link_libraries( - my_lib_relaxed_constexpr_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) -target_compile_definitions(my_lib_relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE) + COMPILE_DEFINITIONS + -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE +) -catch_discover_tests(my_lib_relaxed_constexpr_tests ${COVERAGE_ARGS}) +# Or for reuse: +# +# add_test_config(relaxed_constexpr +# COMPILE_DEFINITIONS +# -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE +# ) +# +# add_library_test(my_lib relaxed_constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp)