A minimalist C unit testing framework
- A single header-file with plain C and
#define
macros. - Plays well with CTest with a
discover_tests()
CMake function.
mtest is a simple, C-only unit testing framework inspired by CuTest with some modern features inspired by doctest.
For mature and feature-complete C/C++ testing frameworks, take a look at Boost.Test, Catch2, doctest, or Google Test.
Recommended installation using CMake's FetchContent:
include(FetchContent)
FetchContent_Declare(mtest
GIT_REPOSITORY https://github.com/MortenSchou/mtest.git
GIT_TAG main # Or specify a specific branch, tag, or commit hash.
)
FetchContent_MakeAvailable(mtest)
Now the CMake library target mtest
is available. For example:
add_executable(my_example_test my_test.c)
target_link_libraries(my_example_test mtest)
# Automatically discover test cases in my_example_test and add them to CTest.
discover_tests(my_example_test)
Example test file my_test.c
:
#include "mtest.h"
TEST_CASE(my_first_test_case, {
int i = 42;
CHECK_EQ_INT(1, i); // Test fails, but we continue running.
REQUIRE_GT_INT(i, 0); // This test is executed and succeeds, but the test case failed due to the previous line.
})
TEST_CASE(my_other_test_case, {
double i = 3.14;
// When comparing doubles, we need to specify a tolerance - here we choose 0.01
REQUIRE_EQ_DOUBLE(4.0, i, 0.01); // Test fails and the test case exits.
CHECK_TRUE(1); // Not executed but it would succeed.
})
// This macro creates a main function that can call each test case, and it tells CTest which test cases are available.
MAIN_RUN_TESTS(my_first_test_case, my_other_test_case)
See mtest.h
for more test macros.