Skip to content

xduraid/xd-ctest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧪 XD-Ctest

Minimalist C unit testing library


🌟 Features

  • 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 0 if all tests pass, 1 if any test fails.

❔ Why XD-Ctest?

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.


📑 Table of Contents

  1. 🚀 Getting Started
  2. ✍️ Writing Tests
  3. 🧾 Registering Tests
  4. ⚙️ Running Tests
  5. 🛠️ Building Test Files
  6. 🖥️ Example Output
  7. 📜 License

🚀 Getting Started

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.


✍️ Writing Tests

Write your tests in functions:

  • Each test function must match the signature int test_function(void).
  • Use XD_TEST_START and XD_TEST_END to 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;
}

🧾 Registering Tests

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.


⚙️ Running Tests

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.


🛠️ Building Test Files

Compile your test source files normally:

gcc example.c -o run_tests

Then execute:

./run_tests

🖥️ Example Output

The 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!

📜 License

This project is released under the MIT License. See LICENSE for details.

About

Minimalist C unit testing library.

Topics

Resources

License

Stars

Watchers

Forks

Languages