A lightweight and portable unit testing framework for C.
test-runner
is designed to be a lightweight and portable unit testing framework for C. It consists of a single C header file, relies on minimal standard C library functions, and complies with all C standards.
- A Standard C compiler
- (Recommended) A POSIX-compliant shell and utilities
-
Clone the repository:
git clone https://github.com/jonruttan/test-runner
-
Write some tests:
mkdir tests cp -a test-runner/examples/minimal/*.c tests
This creates a
tests
directory with example test files. -
Run the tests:
$ sh test-runner/test-runner.sh tests tests/add-integers-test.c . OK: 1 tests (0 incomplete, 0 empty, 0 skipped), 1 assertions (0 skipped)
-
Create a new project:
mkdir test-runner-project cd test-runner-project git clone https://github.com/jonruttan/test-runner
-
Write some code to be tested:
mkdir src cat - > src/add-integers.c
int add_integers(int a, int b) { return a + b; }
-
Write some tests:
mkdir tests cat - > tests/add-integers-test.c
#include "test-runner.h" #include "../src/add-integers.c" static char *test_add_integers(void) { _it_should("add two integers", 3 == add_integers(1, 2)); _xit_should("skip this test", 3 == add_integers(1, 1)); return NULL; } static char *test_incomplete(void) { _it_should("add two integers", 5 == add_integers(2, 3)); _mark_incomplete(); return NULL; } static char *test_skip(void) { _it_should("skip all assertions in this test", 3 == add_integers(2, 2)); return NULL; } static char *test_empty(void) { /* The lack of assertions in this test will be reported. */ return NULL; } static char *run_tests() { _run_test(test_add_integers); _run_test(test_incomplete); _xrun_test(test_skip); _run_test(test_empty); return NULL; }
-
Run the tests:
Using the shell script:
$ sh test-runner/test-runner.sh tests tests/add-integers-test.c .s.ie WARN: 4 tests (1 incomplete, 1 empty, 1 skipped), 3 assertions (1 skipped)
Or manually:
$ for test in tests/*-test.c; do echo $test; cc -Itest-runner/include $test && ./a.out; rm a.out; done tests/add-integers-test.c .s.ie WARN: 4 tests (1 incomplete, 1 empty, 1 skipped), 3 assertions (1 skipped)
See the open issues for a list of proposed features and known issues.
Contributions are welcome! Please follow these steps:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Jon Ruttan - @jonruttan - [email protected]
Project Link: https://github.com/jonruttan/test-runner