Unit Testing (C)
Installation
Install criterion
sudo apt install libcriterion-dev
Compile source-code and test. Link them to Criterion library
gcc fun.c test.c -o testing -l criterion
Run test!
./testing
Unit testing sample
Function example fun.c
char * greet() {
return "Hello World";
}
Test example test.c
#include <criterion/criterion.h>
#include <string.h>
char * greet();
Test(greet_tests, returns_hello_world){
char * result = greet();
cr_assert_str_eq(result, "Hello World", "Expected 'Hello World' but got '%s'", result);
}
Test(greet_tests, returns_eleven){
char * result = greet();
size_t len = strlen(result);
cr_assert_eq(len, 11, "Expected length of 11 but got %zu", len);
}
Assumed project structure
myProject/
├── fun.c
├── test.c
└── testing