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.
This commit is contained in:
tbeu 2025-02-15 09:38:07 +01:00
parent afd4627ee1
commit 784920714a
8 changed files with 127 additions and 92 deletions

View File

@ -6,6 +6,14 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# uncomment this line to enable coverage measurements using gcov # uncomment this line to enable coverage measurements using gcov
# set(CMAKE_CXX_FLAGS "--coverage") # 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() enable_testing()
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(test) add_subdirectory(test)

View File

@ -7,14 +7,14 @@ The C++ version of the Gilded Rose refactoring kata is available in four variant
1. Traditional unit test with the [Catch2](https://github.com/catchorg/Catch2) test framework in the `test/cpp_catch2_unittest` folder. 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. 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.
* GoogleTest framework * GoogleTest framework
1. Traditional unit test with the [Googletest](https://github.com/google/googletest) test framework in the `test/cpp_googletest_unittest` folder. 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. 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. The `GildedRose.cc` file, i.e. the code under test, is identical in all four variants.
## Prerequisites ## Prerequisites
* CMake version >= 3.13 * CMake version ≥ 3.13
* C++ compiler that supports C++14 * C++ compiler that supports C++14
## How to build and run tests in a terminal ## 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 ..
$ cmake --build . $ 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 ### Show available tests
$ cd ${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp/build $ 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...` 2. Select menu `File - Open...`
3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp` 3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp`
4. Select menu `Build - Build Project` 4. Select menu `Build - Build Project`
4. Select menu `Run - Run...` 5. Select menu `Run - Run...`
4. Select what test variant to run, e.g. `GildedRoseCatch2ApprovalTests`. 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` 2. Select `Open a local folder`
3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp` 3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/cpp`
4. Wait for message `CMake generation finished.` in the CMake output window at the bottom 4. Wait for message `CMake generation finished.` in the CMake output window at the bottom

View File

@ -1,21 +1,26 @@
include(FetchContent) include(FetchContent)
if (BUILD_APPROVAL_TESTS_WITH_GTEST OR BUILD_UNIT_TESTS_WITH_GTEST)
FetchContent_Declare( FetchContent_Declare(
googletest googletest
GIT_REPOSITORY https://github.com/google/googletest.git GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.16.0 GIT_TAG v1.16.0
GIT_SHALLOW TRUE GIT_SHALLOW TRUE
) )
FetchContent_MakeAvailable(googletest)
endif()
if (BUILD_APPROVAL_TESTS_WITH_CATCH2 OR BUILD_UNIT_TESTS_WITH_CATCH2)
FetchContent_Declare( FetchContent_Declare(
catch2 catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.8.0 GIT_TAG v3.8.0
GIT_SHALLOW TRUE 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 # Force Google Test to link the C/C++ runtimes dynamically
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
@ -24,6 +29,7 @@ set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
# Do not install GTest # Do not install GTest
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
endif()
add_subdirectory(cpp_catch2_approvaltest) add_subdirectory(cpp_catch2_approvaltest)
add_subdirectory(cpp_catch2_unittest) add_subdirectory(cpp_catch2_unittest)

View File

@ -1,3 +1,4 @@
if (BUILD_APPROVAL_TESTS_WITH_CATCH2)
set(TEST_NAME GildedRoseCatch2ApprovalTests) set(TEST_NAME GildedRoseCatch2ApprovalTests)
add_executable(${TEST_NAME}) add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2ApprovalTests.cc) target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2ApprovalTests.cc)
@ -18,3 +19,4 @@ add_test(
if (MSVC) if (MSVC)
target_compile_options(${TEST_NAME} PRIVATE "/FC") target_compile_options(${TEST_NAME} PRIVATE "/FC")
endif() endif()
endif()

View File

@ -1,3 +1,4 @@
if (BUILD_UNIT_TESTS_WITH_CATCH2)
set(TEST_NAME GildedRoseCatch2UnitTests) set(TEST_NAME GildedRoseCatch2UnitTests)
add_executable(${TEST_NAME}) add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2UnitTests.cc) target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2UnitTests.cc)
@ -17,3 +18,4 @@ add_test(
if (MSVC) if (MSVC)
target_compile_options(${TEST_NAME} PRIVATE "/FC") target_compile_options(${TEST_NAME} PRIVATE "/FC")
endif() endif()
endif()

View File

@ -1,3 +1,4 @@
if (BUILD_APPROVAL_TESTS_WITH_GTEST)
set(TEST_NAME GildedRoseGoogletestApprovalTests) set(TEST_NAME GildedRoseGoogletestApprovalTests)
add_executable(${TEST_NAME}) add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestApprovalTests.cc) target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestApprovalTests.cc)
@ -18,3 +19,4 @@ add_test(
if (MSVC) if (MSVC)
target_compile_options(${TEST_NAME} PRIVATE "/FC") target_compile_options(${TEST_NAME} PRIVATE "/FC")
endif() endif()
endif()

View File

@ -1,3 +1,4 @@
if (BUILD_UNIT_TESTS_WITH_GTEST)
set(TEST_NAME GildedRoseGoogletestUnitTests) set(TEST_NAME GildedRoseGoogletestUnitTests)
add_executable(${TEST_NAME}) add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestUnitTests.cc) target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestUnitTests.cc)
@ -17,3 +18,4 @@ add_test(
if (MSVC) if (MSVC)
target_compile_options(${TEST_NAME} PRIVATE "/FC") target_compile_options(${TEST_NAME} PRIVATE "/FC")
endif() endif()
endif()