This is a template for test driven development tdd for a C project. The project gets generated by cmake
and build by gcc
. The tests use the integrated ctest
funcionality of cmake and are written with the cmocka framework. Additionally doxygen
is used to automaticly generate source code documentation and cppcheck
to statically analyze the source code.
Version in brackets is what was used at the time of writing.
- cmake (3.18.4)
- gcc (10.3.0)
- cppcheck (2.3)
- cmocka (1.1.5-2)
- doxygen (1.9.1)
A Dockerfile is available for easy setup of the needed tools. Or even easier as prebuilt image on dockerhub nikolodion/cmake-tdd. If VSCode or another supported editor is used one could also use the additional devcontainer.json to setup the environment automatically.
Please note that the commands have to be executed from the build
subdirectory.
command | action | |
---|---|---|
cmake .. |
generate buildsystem | |
make |
compile source code | executable can be found under build/src/ |
make test_cmocka |
run cmocka tests | |
make test_cppcheck |
check source code with cppcheck | |
make tests |
run all tests | cmocka & cppcheck |
make doc |
generate documentation | to view open build/doc/html/index.html |
make clean |
remove compiled files |
If VSCode is used these commands are also available as tasks in tasks.json.
- lib/
- Place for external dependencies.
- src/
- Here is all the source code for the application placed.
- For modular code subfolders with appropriate CMake-Scripts can be created. As example see mylib/CMakeLists.txt. The module is built as library linked with main.
- If a file or module is added, add it also to the CMakeLists.txt.
- test/
- For each source file in
src
a file that tests the implementation should be created here. The cmocka framework makes this very easy. - In the spirit of TDD, the tests should be written at the same time as the code.
- For each source file in
- cmake-tdd-template - A similar cmake template that is based on Catch2 framework and supports C++.
- FreeRTOS-Emulator - Inspiration for defining the steps like test & check as build targets with cmake.