Minimalist C unit testing library
- Zero external dependencies (pure C).
- Single-header distribution for quick setup.
- Easy test registration via macro.
- File name and line number reporting on assertion failures.
- ANSI-colored console output for clear PASS/FAIL visibility.
- Support resource cleanup section in each test.
- Program exits with
0if all tests pass,1if any test fails.
Unit testing in C can sometimes feel overwhelming, especially when libraries come with extensive setup, extra dependencies, or a long list of features that aren't always needed. xd-ctest was created to keep things simple. Many times, all you need is a basic ASSERT, a clean test structure, and readable output to catch bugs quickly. This library offers just enough functionality to write and run tests without unnecessary complexity.
- 🚀 Getting Started
- ✍️ Writing Tests
- 🧾 Registering Tests
- ⚙️ Running Tests
- 🛠️ Building Test Files
- 🖥️ Example Output
- 📜 License
Copy the header file xd_ctest.h to your project, then include it in your test source files:
#include "xd_ctest.h"No additional installation or linking is required.
Write your tests in functions:
- Each test function must match the signature
int test_function(void). - Use
XD_TEST_STARTandXD_TEST_ENDto wrap your test logic. - Use
XD_TEST_ASSERT(condition)for assertions. - Write your cleanup code after the
xd_test_cleanup:label.
int test_example_pass() {
XD_TEST_START;
XD_TEST_ASSERT(1 == 1);
XD_TEST_ASSERT(5 > 3);
xd_test_cleanup:
// Optional: test cleanup code
XD_TEST_END;
}
int test_example_fail() {
XD_TEST_START;
int *num = (int *)malloc(sizeof(int));
*num = 2;
XD_TEST_ASSERT(*num == 3); // Will fail and jump to cleanup
XD_TEST_ASSERT(*num == 2);
xd_test_cleanup:
free(num); // test cleanup
XD_TEST_END;
}Use the XD_TEST_CASE() macro to register the test functions in the test_suite array:
static xd_test_case test_suite[] = {
XD_TEST_CASE(test_example_pass),
XD_TEST_CASE(test_example_fail)
};The macro automatically captures the function pointer and its name.
Use the XD_TEST_RUN_ALL(test_suite) macro at the end of your main() function to run all the registered tests.
int main() {
XD_TEST_RUN_ALL(test_suite);
}The program prints each test result, then a summary.
It exits with code 0 if all tests pass or 1 if any fail.
Compile your test source files normally:
gcc example.c -o run_testsThen execute:
./run_testsThe following is the output of the example test file example.c:
[TEST] Running test_example_pass
[PASS] test_example_pass
[TEST] Running test_example_fail
[FAIL] example.c:40: *num == 3
[FAIL] test_example_fail
[SUMMARY] Passed: 1, Failed: 1, Total: 2
SOME TESTS FAILED!
This project is released under the MIT License. See LICENSE for details.