Writing Unit Tests in C
devc

Introduction
We all love C, right? You can basically write anything that you want. But that comes at the cost of you trying to debug everything. So, I'll explain my process of writing unit tests in C, utilizing the CTest Framework with CMake.
The code used in this example is borrowed from the cmdfx library.
Prerequisites
Let's assume your file tree looks something like this:
|CMakeLists.txt
|README.md
|LICENSE
|test/
├── CMakeLists.txt
├── src/
│ ├── test1.c
│ ├── test2.c
│ ├── test3_main.c
│ ├── test3_others.c
│ └── test.h
|include/
└── library.hIn your root CMakeLists.txt, get started with this code:
option(TESTING "Build tests for ${PROJECT_NAME}" ON)
if (TESTING)
enable_testing()
add_subdirectory(test)
endif()This will allow you to start configuring tests when you run cmake .. in the test directory.
Now, in your test/CMakeLists.txt, you can add the following code:
cmakLoading full content...