Lesson 46 of 50
cUnit Testing with CMocka
12 mins
Course progress92%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 46 of 50
c12 mins
Keep these ideas in mind as you work through the lesson.
CMocka is a lightweight unit‑testing framework for C.
apt-get install libcmocka-dev).void.assert_int_equal, assert_string_equal, etc.-lcmocka.#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) {
(void) state; // unused
assert_int_equal(add(2,3), 5);
assert_int_equal(add(-1,1), 0);
}
int main(void) {
const struct CMUnitTest tests[] = { cmocka_unit_test(test_add) };
return cmocka_run_group_tests(tests, NULL, NULL);
}
Run with ./test_add – a pass yields a green OK line.