Unit Testing
Best Practices
Test Runners
ctest
ctest
doesn’t provide any testing macros/functions/classes on its own. It’s just a test runner.
- “CTest is useful for running multiple types of tests. You might have unit tests for C++ in Catch2, unit tests for python bindings in pytest, and you might have application-level testing that runs you program with specified arguments in a short shell script. CTest is the place to wire all these frameworks together so you can run all your testing in one shot. It works well for that.”
- “And since I haven’t seen it mentioned, the big C++ unit testing frameworks can integrate with CTest so you can see the pass/fail status of each test run by the framework”
Test Frameworks
Catch2
catch2 vs gtest:
- see snorristurluson.github.io
- see anteru.net
GoogleTest, gtest
- based on the xUnit architecture
Build
To see some gtest samples:
- build as Standalone CMake Project
Build method used in AdvCpp course:
- build by Incorporating Into An Existing CMake Project
- “Use CMake to download GoogleTest as part of the build’s configure step. This approach doesn’t have the limitations of the other methods.”
Just add to your CMakeLists.txt
(CMake 3.14 or later):
include(FetchContent)
FetchContent_Declare(
googletest
# Specify the commit you depend on and update it regularly.
URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)