From 784920714ae465821e203c6a97def9d6c4311a21 Mon Sep 17 00:00:00 2001 From: tbeu Date: Sat, 15 Feb 2025 09:38:07 +0100 Subject: [PATCH] Introduce four CMake options to enable/disabled test variants or test frameworks By default all four test variants (Catch2 vs. GTest and Approval vs. Unit) are enabled. --- cpp/CMakeLists.txt | 8 ++++ cpp/README.md | 31 +++++++++---- cpp/test/CMakeLists.txt | 46 +++++++++++-------- .../cpp_catch2_approvaltest/CMakeLists.txt | 34 +++++++------- cpp/test/cpp_catch2_unittest/CMakeLists.txt | 30 ++++++------ .../CMakeLists.txt | 32 +++++++------ .../cpp_googletest_unittest/CMakeLists.txt | 30 ++++++------ cpp/test/cpp_texttest/CMakeLists.txt | 8 ++-- 8 files changed, 127 insertions(+), 92 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 3fd016e0..0e3cc23e 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -6,6 +6,14 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) # uncomment this line to enable coverage measurements using gcov # set(CMAKE_CXX_FLAGS "--coverage") +option(BUILD_APPROVAL_TESTS_WITH_GTEST "Use GoogleTest for approval testing" ON) + +option(BUILD_UNIT_TESTS_WITH_GTEST "Use GoogleTest for unit testing" ON) + +option(BUILD_APPROVAL_TESTS_WITH_CATCH2 "Use Catch2 for approval testing" ON) + +option(BUILD_UNIT_TESTS_WITH_CATCH2 "Use Catch2 for unit testing" ON) + enable_testing() add_subdirectory(src) add_subdirectory(test) diff --git a/cpp/README.md b/cpp/README.md index e981ce1a..e1f04ee5 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -6,16 +6,16 @@ The C++ version of the Gilded Rose refactoring kata is available in four variant * Catch2 test framework 1. Traditional unit test with the [Catch2](https://github.com/catchorg/Catch2) test framework in the `test/cpp_catch2_unittest` folder. 2. [Approval tests](https://github.com/approvals/ApprovalTests.cpp) with the [Catch2](https://github.com/catchorg/Catch2) test framework in the `test/cpp_catch2_approvaltest` folder. -* Google Test framework - 1. Traditional unit test with the [Googletest](https://github.com/google/googletest) test framework in the `test/cpp_googletest_unittest` folder. - 2. [Approval tests](https://github.com/approvals/ApprovalTests.cpp) with the [Googletest](https://github.com/google/googletest) test framework in the `test/cpp_googletest_approvaltest` folder. +* GoogleTest framework + 1. Traditional unit test with the [GoogleTest](https://github.com/google/googletest) test framework in the `test/cpp_googletest_unittest` folder. + 2. [Approval tests](https://github.com/approvals/ApprovalTests.cpp) with the [GoogleTest](https://github.com/google/googletest) test framework in the `test/cpp_googletest_approvaltest` folder. The `GildedRose.cc` file, i.e. the code under test, is identical in all four variants. ## Prerequisites -* CMake version >= 3.13 -* C++ compiler that supports C++14 +* CMake version ≥ 3.13 +* C++ compiler that supports C++14 ## How to build and run tests in a terminal @@ -27,6 +27,19 @@ The `GildedRose.cc` file, i.e. the code under test, is identical in all four var $ cmake .. $ cmake --build . +The following test specific options for building with CMake are available. + +* `BUILD_APPROVAL_TESTS_WITH_CATCH2:BOOL=ON` +This option builds the approval tests with the Catch2 test framework. +* `BUILD_UNIT_TESTS_WITH_CATCH2:BOOL=ON` +This option builds the unit tests with the Catch2 test framework. +* `BUILD_APPROVAL_TESTS_WITH_GTEST:BOOL=ON` +This option builds the approval tests with the GoogleTest test framework. +* `BUILD_UNIT_TESTS_WITH_GTEST:BOOL=ON` +This option builds the unit tests with the GoogleTest test framework. + +For example, run the CMake configuration `cmake -DBUILD_APPROVAL_TESTS_WITH_CATCH2=OFF -DBUILD_UNIT_TESTS_WITH_CATCH2=OFF ..` to disable the Catch2 based tests. + ### Show available tests $ cd ${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp/build @@ -55,12 +68,12 @@ The `GildedRose.cc` file, i.e. the code under test, is identical in all four var 2. Select menu `File - Open...` 3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp` 4. Select menu `Build - Build Project` -4. Select menu `Run - Run...` -4. Select what test variant to run, e.g. `GildedRoseCatch2ApprovalTests`. +5. Select menu `Run - Run...` +6. Select what test variant to run, e.g. `GildedRoseCatch2ApprovalTests`. -## How to build and run tests using Visual Studio 2019 +## How to build and run tests using Visual Studio ≥ 2019 -1. Start Visual Studio 2019 +1. Start Visual Studio 2. Select `Open a local folder` 3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp` 4. Wait for message `CMake generation finished.` in the CMake output window at the bottom diff --git a/cpp/test/CMakeLists.txt b/cpp/test/CMakeLists.txt index 1f455ed8..f51ce64b 100644 --- a/cpp/test/CMakeLists.txt +++ b/cpp/test/CMakeLists.txt @@ -1,29 +1,35 @@ include(FetchContent) -FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG v1.16.0 - GIT_SHALLOW TRUE -) +if (BUILD_APPROVAL_TESTS_WITH_GTEST OR BUILD_UNIT_TESTS_WITH_GTEST) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.16.0 + GIT_SHALLOW TRUE + ) + FetchContent_MakeAvailable(googletest) +endif() -FetchContent_Declare( - catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v3.8.0 - GIT_SHALLOW TRUE -) +if (BUILD_APPROVAL_TESTS_WITH_CATCH2 OR BUILD_UNIT_TESTS_WITH_CATCH2) + FetchContent_Declare( + catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.8.0 + GIT_SHALLOW TRUE + ) + FetchContent_MakeAvailable(catch2) +endif() -FetchContent_MakeAvailable(googletest catch2) +if (BUILD_APPROVAL_TESTS_WITH_GTEST OR BUILD_UNIT_TESTS_WITH_GTEST) + # Force Google Test to link the C/C++ runtimes dynamically + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -# Force Google Test to link the C/C++ runtimes dynamically -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + # Disable building GMock + set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) -# Disable building GMock -set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) - -# Do not install GTest -set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) + # Do not install GTest + set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) +endif() add_subdirectory(cpp_catch2_approvaltest) add_subdirectory(cpp_catch2_unittest) diff --git a/cpp/test/cpp_catch2_approvaltest/CMakeLists.txt b/cpp/test/cpp_catch2_approvaltest/CMakeLists.txt index df930273..b145a9c0 100644 --- a/cpp/test/cpp_catch2_approvaltest/CMakeLists.txt +++ b/cpp/test/cpp_catch2_approvaltest/CMakeLists.txt @@ -1,20 +1,22 @@ -set(TEST_NAME GildedRoseCatch2ApprovalTests) -add_executable(${TEST_NAME}) -target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2ApprovalTests.cc) -target_include_directories(${TEST_NAME} PUBLIC ../third_party) -target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain) -set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14) -add_test( +if (BUILD_APPROVAL_TESTS_WITH_CATCH2) + set(TEST_NAME GildedRoseCatch2ApprovalTests) + add_executable(${TEST_NAME}) + target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2ApprovalTests.cc) + target_include_directories(${TEST_NAME} PUBLIC ../third_party) + target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain) + set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14) + add_test( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) - -# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. -# The __FILE__ macro is used by Catch2 to get the path to current test file. -# Links: -# * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 -# * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 -if (MSVC) + ) + + # Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. + # The __FILE__ macro is used by Catch2 to get the path to current test file. + # Links: + # * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 + # * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 + if (MSVC) target_compile_options(${TEST_NAME} PRIVATE "/FC") -endif() + endif() +endif() \ No newline at end of file diff --git a/cpp/test/cpp_catch2_unittest/CMakeLists.txt b/cpp/test/cpp_catch2_unittest/CMakeLists.txt index 80d8a3fb..5ed8ff2d 100644 --- a/cpp/test/cpp_catch2_unittest/CMakeLists.txt +++ b/cpp/test/cpp_catch2_unittest/CMakeLists.txt @@ -1,19 +1,21 @@ -set(TEST_NAME GildedRoseCatch2UnitTests) -add_executable(${TEST_NAME}) -target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2UnitTests.cc) -target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain ) -set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14) -add_test( +if (BUILD_UNIT_TESTS_WITH_CATCH2) + set(TEST_NAME GildedRoseCatch2UnitTests) + add_executable(${TEST_NAME}) + target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2UnitTests.cc) + target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain ) + set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14) + add_test( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) - -# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. -# The __FILE__ macro is used by Catch2 to get the path to current test file. -# Links: -# * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 -# * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 -if (MSVC) + ) + + # Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. + # The __FILE__ macro is used by Catch2 to get the path to current test file. + # Links: + # * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 + # * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 + if (MSVC) target_compile_options(${TEST_NAME} PRIVATE "/FC") + endif() endif() diff --git a/cpp/test/cpp_googletest_approvaltest/CMakeLists.txt b/cpp/test/cpp_googletest_approvaltest/CMakeLists.txt index 9d8e241a..79a967c9 100644 --- a/cpp/test/cpp_googletest_approvaltest/CMakeLists.txt +++ b/cpp/test/cpp_googletest_approvaltest/CMakeLists.txt @@ -1,20 +1,22 @@ -set(TEST_NAME GildedRoseGoogletestApprovalTests) -add_executable(${TEST_NAME}) -target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestApprovalTests.cc) -target_include_directories(${TEST_NAME} PUBLIC ../third_party) -target_link_libraries(${TEST_NAME} GildedRoseLib gtest) -set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11) -add_test( +if (BUILD_APPROVAL_TESTS_WITH_GTEST) + set(TEST_NAME GildedRoseGoogletestApprovalTests) + add_executable(${TEST_NAME}) + target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestApprovalTests.cc) + target_include_directories(${TEST_NAME} PUBLIC ../third_party) + target_link_libraries(${TEST_NAME} GildedRoseLib gtest) + set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11) + add_test( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) - -# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. -# The __FILE__ macro can be used to get the path to current test file. -# Links: -# * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 -# * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 -if (MSVC) + ) + + # Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. + # The __FILE__ macro can be used to get the path to current test file. + # Links: + # * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 + # * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 + if (MSVC) target_compile_options(${TEST_NAME} PRIVATE "/FC") + endif() endif() diff --git a/cpp/test/cpp_googletest_unittest/CMakeLists.txt b/cpp/test/cpp_googletest_unittest/CMakeLists.txt index 0fda4950..c705c1dd 100644 --- a/cpp/test/cpp_googletest_unittest/CMakeLists.txt +++ b/cpp/test/cpp_googletest_unittest/CMakeLists.txt @@ -1,19 +1,21 @@ -set(TEST_NAME GildedRoseGoogletestUnitTests) -add_executable(${TEST_NAME}) -target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestUnitTests.cc) -target_link_libraries(${TEST_NAME} GildedRoseLib gtest gtest_main) -set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11) -add_test( +if (BUILD_UNIT_TESTS_WITH_GTEST) + set(TEST_NAME GildedRoseGoogletestUnitTests) + add_executable(${TEST_NAME}) + target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestUnitTests.cc) + target_link_libraries(${TEST_NAME} GildedRoseLib gtest gtest_main) + set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11) + add_test( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) - -# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. -# The __FILE__ macro can be used to get the path to current test file. -# Links: -# * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 -# * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 -if (MSVC) + ) + + # Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. + # The __FILE__ macro can be used to get the path to current test file. + # Links: + # * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 + # * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 + if (MSVC) target_compile_options(${TEST_NAME} PRIVATE "/FC") + endif() endif() diff --git a/cpp/test/cpp_texttest/CMakeLists.txt b/cpp/test/cpp_texttest/CMakeLists.txt index ddd89281..928db537 100644 --- a/cpp/test/cpp_texttest/CMakeLists.txt +++ b/cpp/test/cpp_texttest/CMakeLists.txt @@ -4,9 +4,9 @@ target_sources(${TEST_NAME} PRIVATE GildedRoseTextTests.cc) target_link_libraries(${TEST_NAME} GildedRoseLib) set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11) add_test( - NAME ${TEST_NAME} - COMMAND ${TEST_NAME} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + NAME ${TEST_NAME} + COMMAND ${TEST_NAME} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) # Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path. @@ -15,5 +15,5 @@ add_test( # * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 # * https://docs.microsoft.com/en-us/cpp/build/reference/fc-full-path-of-source-code-file-in-diagnostics?view=vs-2019 if (MSVC) - target_compile_options(${TEST_NAME} PRIVATE "/FC") + target_compile_options(${TEST_NAME} PRIVATE "/FC") endif()