A lightweight unit testing framework for C inspired by JUnit, supporting grouped tests, assertions, source tracking, and formatted output.
First, build the library on the root folder of the repo using the next command:
makeOnce the library has been build, in order to install it in your system you can do the next command:
sudo make installIf you want to completely remove the library from your system, just execute the follwing command:
sudo make uninstallInclude ctest.h in your project and link against the implementation file (not shown here).
#include "ctest.h"Create tests as functions, group them, and run the suite.
When compiling, make sure to include the following flag:
gcc main.c -o whatevername -lctestint initGroup(TestGroup *group);
int addTest(TestGroup *group, const char *name, CTest *test);
int runTests(TestGroup *group);void sampleTest(TestResult *res) {
assertEqualsInt(5, 2 + 3);
}
int main() {
TestGroup group;
initGroup(&group);
CTest test = { .func = sampleTest };
addTest(&group, "Addition Test", &test);
runTests(&group);
return 0;
}All assertions track the source file, line number, and function using __FILE__, __LINE__, and __func__ via the CUR_SOURCE_LOCATION macro.
assertEqualsInt(expected, actual);
assertEqualsShort(expected, actual);
assertEqualsLong(expected, actual);
assertEqualsFloat(expected, actual);
assertEqualsDelta(expected, actual, delta);
assertEqualsDouble(expected, actual);
assertEqualsChar(expected, actual);
assertEqualsStr(expected, actual);assertEqualsArrayInt(expected, actual);
assertEqualsArrayShort(expected, actual);
assertEqualsArrayLong(expected, actual);
assertEqualsArrayFloat(expected, actual);
assertEqualsArrayDouble(expected, actual);assertNotEqualsInt(expected, actual);
assertNotEqualsShort(expected, actual);
assertNotEqualsLong(expected, actual);
assertNotEqualsFloat(expected, actual);
assertNotEqualsDouble(expected, actual);
assertNotEqualsChar(expected, actual);
assertNotEqualsStr(expected, actual);assertNotEqualsArrayInt(expected, actual);
assertNotEqualsArrayShort(expected, actual);
assertNotEqualsArrayLong(expected, actual);
assertNotEqualsArrayFloat(expected, actual);
assertNotEqualsArrayDouble(expected, actual);assertTrue(actual);
assertFalse(actual);assertNotNull(pointer);
assertNull(pointer);You can enable extra debug and verbosity at compile-time using:
#define DEBUG_ASSERT_ENABLED 1
#define VERBOSE_ASSERT_RESULT 1When enabled, DEBUG_ASSERT() will track and display failed expressions with source metadata.
The framework uses ANSI color codes:
red()– Failuresgreen()– Successesyellow(),blue(),white()– Neutral/informationalclear()– Clears terminal output
These macros can be used in user code for custom formatting as well.
Include your license here if applicable.