From a9daedd8bee1b5620a933b612c3335b90bde263d Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Mon, 5 Jul 2021 11:44:15 +0200 Subject: [PATCH] add optional ctest & catch2 based test infrastructure This commit adds a CMakeLists.txt in the tests directory containing the foundation for easily creating new test executables. It includes creation of a shared library that the individual test executables can be linked to, allowing them to be as small as possible. The tests are optional and can be enabled with the CMake option with_cxx_tests. Also added is a first simple test case that always passes to show how to create test cases. --- CMakeLists.txt | 7 +++++++ tests/CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ tests/catch2_main.cpp | 3 +++ tests/test_simple.cpp | 4 ++++ 4 files changed, 44 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/catch2_main.cpp create mode 100644 tests/test_simple.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bd8a27d5b..1df011e2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ option(with_ortho "ORTHO features in neato layout engine." ON ) option(with_sfdp "sfdp layout engine." ON ) option(with_smyrna "SMYRNA large graph viewer (disabled by default - experimental)" OFF) option(use_sanitizers "enables using address and undefined behavior sanitizer" OFF) +option(with_cxx_tests "enables building the C++ tests" OFF) if (enable_ltdl) add_definitions(-DENABLE_LTDL) @@ -224,10 +225,16 @@ include(InstallRequiredSystemLibraries) include(package_info) include(CPack) +# ==================================== Test ==================================== +Include(CTest) + # ======================= Specify subdirectories to build ====================== add_subdirectory(lib) add_subdirectory(plugin) add_subdirectory(cmd) +if (with_cxx_tests) + add_subdirectory(tests) +endif() MATH(EXPR GRAPHVIZ_PLUGIN_VERSION "${GRAPHVIZ_PLUGIN_VERSION}+1") set(GVPLUGIN_VERSION "${GRAPHVIZ_PLUGIN_VERSION}") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 000000000..5d87a4463 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required (VERSION 3.12 FATAL_ERROR) + +find_package(Catch2 REQUIRED) + +enable_testing() + +if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) + add_compile_options(-Wextra) +endif() + +# for convenience, make a library that depends on everything so each +# separate test can be as small as possible +add_library(test_common STATIC + catch2_main.cpp + ) +set_target_properties(test_common PROPERTIES CXX_STANDARD 20) +set_target_properties(test_common PROPERTIES CXX_STANDARD_REQUIRED ON) +target_link_libraries(test_common PUBLIC Catch2::Catch2) + +macro(create_test testname) + add_executable(test_${testname} test_${testname}.cpp) + set_target_properties(test_${testname} PROPERTIES CXX_STANDARD 20) + set_target_properties(test_${testname} PROPERTIES CXX_STANDARD_REQUIRED ON) + add_test(NAME test_${testname} COMMAND test_${testname}) + target_link_libraries(test_${testname} PRIVATE + test_common + ) +endmacro() + +create_test(simple) diff --git a/tests/catch2_main.cpp b/tests/catch2_main.cpp new file mode 100644 index 000000000..f481b16fe --- /dev/null +++ b/tests/catch2_main.cpp @@ -0,0 +1,3 @@ +#define CATCH_CONFIG_MAIN + +#include diff --git a/tests/test_simple.cpp b/tests/test_simple.cpp new file mode 100644 index 000000000..f01f5c41c --- /dev/null +++ b/tests/test_simple.cpp @@ -0,0 +1,4 @@ +#include + +TEST_CASE("always pass") { +} -- 2.40.0