Skip to content
AgileLogics
4 min read

Testing in C

Prerequisites: Build systems: CMake

C has no built-in test runner — lightweight frameworks like Unity or CMocka provide one, in the same spirit as JUnit for Java:

#include "unity.h"

void setUp(void) {}
void tearDown(void) {}

void test_add_two_numbers(void) {
    TEST_ASSERT_EQUAL_INT(5, add(2, 3));
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_add_two_numbers);
    return UNITY_END();
}

Combined with the sanitizers and Valgrind from earlier lessons, a real test suite in C is checking two different things: is the logic correct, and separately, is the memory being handled safely — a distinction most higher-level languages' test suites don't need to make explicitly.