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 uninstallYou can also delete the object files created using the command:
make cleanInclude 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 -lctestBy default, passed asserts are not displayed, meaning only the asserts that failed will showup at run time, if you wish to see both the passed and failed asserts, you can compile the program with the following flag:
gcc main.c -o whatevername -lctest -DVERBOSE_ASSERT_RESULTWhen using this option, you can expect an output like this:
✓[PASSED]: assertEqualsFloat
Time Taken: 0 ms
----------------------
✗[FAILED]: assertEqualsDouble
On file: test/main.c | Line: 54 | Function: main
Time Taken: 0 ms
Expected: 2 vs Actual: 4
----------------------int 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()– Successes
These macros can be used in user code for custom formatting as well.
To know how to use the library, you can find an example folder with some examples on how to use the library properly