Check / cmocka
Check and cmocka are two common C unit-test frameworks. They give you test cases, fixtures, assertions that record failures without necessarily aborting, and a runner that can fork so a crash fails one case.
Detailed Explanation
The C standard has assert and exit. Frameworks add structure.
Check (libcheck) registers cases in suites. START_TEST / END_TEST define a function. ck_assert_int_eq, ck_assert_ptr_nonnull, and friends record a failure and continue or abort depending on the macro. Check can run each test in a forked process, so a segfault becomes a failed test instead of a dead runner. That fork is why some tests that talk to threads or to open debugger state behave oddly. Link with -lcheck and the usual pthread/rt extras your distro's pkg-config check lists.
cmocka is smaller and is good at mocking. will_return / mock_type let a test wrap malloc or a syscall via a function pointer or a weak symbol, then script the return values. Assertions are assert_int_equal, assert_non_null. Setup and teardown function pointers are the fixtures. cmocka does not require a code generator.
Others exist: Unity (embedded), Greatest (single header), μnit. Pick one per repo. Mixing them is a tax.
A framework does not replace sanitizers. Register the test binary with ASan in CI. A forking runner plus ASan can need ASAN_OPTIONS=detect_leaks=0 for the parent; read the project's notes.
Assertions in a framework should stay side-effect free, same as assert. Prefer ck_assert_int_eq(f(), 3) only if f() is safe to call once; assign to a local first if it is not.
Code Examples
/* A cmocka-shaped test. Compile with -lcmocka. */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
static int add(int a, int b) { return a + b; }
static void test_add(void **state)
{
(void)state;
assert_int_equal(add(2, 40), 42);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_add),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}/* A Check-shaped case. */
#include <check.h>
#include <stdlib.h>
START_TEST(test_sum)
{
ck_assert_int_eq(1 + 1, 2);
}
END_TEST
static Suite *suite(void)
{
Suite *s = suite_create("sum");
TCase *tc = tcase_create("core");
tcase_add_test(tc, test_sum);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
Suite *s = suite();
SRunner *r = srunner_create(s);
srunner_run_all(r, CK_NORMAL);
int failed = srunner_ntests_failed(r);
srunner_free(r);
return failed == 0 ? 0 : 1;
}Common Gotchas
Check's fork mode surprises tests that already fork or that attach a debugger
run those cases in `CK_NOFORK` if you must.
Mocking `malloc` requires a link-time wrap (`--wrap=malloc`) or a function pointer the production code already calls
you cannot intercept an inlined libc from a header.
A framework assertion that aborts skips teardown
use the fixtures, and prefer expects that do not abort if you still have to unlock a mutex.
`pkg-config --libs check` is the portable link line
copying a random `-lcheck -lpthread` from a blog will fail on the next distro.