What it is
CMocka is a lightweight, modern unit testing framework for C. It provides a simple API for writing unit tests, mocking functions, and verifying behavior in C programs.
CMocka allows you to define test functions, use setup and teardown functions for test fixtures, perform assertions, and run tests via a test runner. It supports mocking to replace functions and verify interactions.
Installation
sudo apt install cmocka cmocka-docGetting started
The smallest useful thing you can do with it, and what each part means.
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
int add(int a, int b) { return a + b; }
static void test_add(void **state) {
assert_int_equal(add(2, 3), 5);
}
int main(void) {
const struct CMUnitTest tests[] = { cmocka_unit_test(test_add) };
return cmocka_run_group_tests(tests, NULL, NULL);
}static int *ptr;
static int setup(void **state) { ptr = malloc(sizeof(int)); *ptr = 42; return 0; }
static int teardown(void **state) { free(ptr); return 0; }
static void test_example(void **state) { assert_int_equal(*ptr, 42); }Advanced usage
Where the library earns its place over a simpler alternative.
// Use cmocka's mock() and expect_value() functions to replace and verify calls to other functions in the tested code.static void test_subtract(void **state) { assert_int_equal(5 - 3, 2); }
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_add),
cmocka_unit_test(test_subtract)
};Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Assertion failure
- Check the assertion message to identify why the test failed and debug the function accordingly.
- Segmentation fault
- Ensure proper memory allocation and pointer usage in tests and tested functions.
Best practices
- Use setup and teardown functions to handle resource allocation and cleanup.
- Keep tests independent to avoid side effects.
- Use meaningful assertions for clarity.
- Leverage mocks for testing functions with external dependencies.
- Run tests frequently to catch regressions early.
Background
Why it exists, and what it was reacting to.
CMocka was developed as a modern alternative to other C unit testing frameworks. It emphasizes simplicity, minimal dependencies, and provides support for mock objects, test fixtures, and assertions to ensure reliable C code testing.
