Sample CMake-based C++ application that demonstrates setup and basic usage of several Unit Testing Frameworks for C++.
CMake ≥ 3.14.0
is required.
- library configured with Boost.Test (
boost-1.81.0
) for unit testing; - library configured with Catch2 v3 (
v3.2.0
) for unit testing & FakeIt (2.3.2
) for mocking; - library configured with Criterion (
v2.4.1
) for unit testing & trompeloeil (v43
) for mocking; - library configured with Doctest (
v2.4.9
) for unit testing & FakeIt (2.3.2
) for mocking; - library configured with GoogleTest (
v1.13.0
) for unit testing and mocking; - library configured with lest (
1.35.1
) for unit testing & trompeloeil (v43
) for mocking; - application that links with them to run some code.
All unit testing frameworks except Criterion are obtained as sources from GitHub using CMake's functions FetchContent_Declare()
and FetchContent_MakeAvailable()
.
Criterion framework is added to the project as Linux release binaries taken from GitHub via FetchContent_Declare()
and FetchContent_Populate()
since that framework has migrated from CMake to Meson build system since v2.4.0
on February, 2022. Criterion binaries are available for Linux platform only - in case of other platform (i.e. Windows) corresponding library factorial_criterion
would not be built.
Support of the library that uses Boost.Test (factorial_boost
) is currently under conditional compilation and is disabled by default.
Check instructions below on how it could be enabled, but that would require time consuming fetching of Boost sources
and its building.
Start by doing:
mkdir cmake-build
cd cmake-build/
Next, if not using Boost.Test-based demo library, execute:
cmake ..
make
Otherwise, execute:
cmake -DBUILD_FACTORIAL_BOOST_TEST_LIB=ON ..
make
Run app_factorial
application:
cd app_factorial/
./app_factorial
Run unit tests (called from cmake-build
folder):
cd ..
ctest
Placeholder XXX
on depicted structure below stands for the name of the used unit testing framework.
.
│
├── app_factorial/ # Facorial application binary
│ ├── include # Application includes (empty)
│ ├── src # Application sources
│ │ └── main.cpp # Main entry of the sample factorial application
│ ├── test/ # Application test folder (empty)
│ └── CMakeLists.txt # Compile script for application
│
├── factorial_XXX # XXX-based static library
│ ├── include # XXX-based library includes
│ │ └── factorial.h # XXX-based library header
│ ├── src # XXX-based library sources
│ │ └── factorial.cpp # XXX-based library source
│ ├── test/ # XXX-based library tests
│ │ ├── factorial_test.cpp # Tests for XXX-based library
│ │ └── CMakeLists.txt # Compile script for tests of XXX-based library
│ └── CMakeLists.txt # XXX-based library compile script
│
├── .....
│
└── CMakeLists.txt # Main compile script