Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 2 additions & 28 deletions my_exe/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 1 addition & 23 deletions my_header_lib/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 1 addition & 20 deletions my_lib/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
37 changes: 18 additions & 19 deletions my_lib/test/constexpr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)