Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trmckay/micro-test
A micro unit-testing library for C/C++
https://github.com/trmckay/micro-test
Last synced: about 1 month ago
JSON representation
A micro unit-testing library for C/C++
- Host: GitHub
- URL: https://github.com/trmckay/micro-test
- Owner: trmckay
- License: gpl-3.0
- Created: 2021-10-16T20:53:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-08T00:14:50.000Z (about 3 years ago)
- Last Synced: 2024-10-12T06:22:37.114Z (2 months ago)
- Language: C
- Size: 42 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# µ-test
A micro unit testing framework for C/C++ to get you up and running with unit-testing ASAP (even without libc).
# Usage
Simply include the C and header file in your codebase and register/run your tests. Each test
consists of:* A test funtion (`int (*fn)(void)`) that returns `TEST_PASS` or `TEST_FAIL`
* Optional setup function (`void (*fn)(void)`, `NULL` if not used)
* Optional teardown function (`void (*fn)(void)`, `NULL` if not used)
* A nameIn your entrypoint, register each test and then call `run_tests()`.
µ-test works without the standard library, as well! Compile with `-D NO_STD` to avoid including
stdlib/stdio. Then in your main routine, set the function pointer `int (*utest_printer)(const char *, ...)`
to provide a replacement for `printf(3)`.By default, output (e.g. from `printf(3)`) is only echoed when a test fails. To turn off stdout/stderr capturing, set
`utest_capture = 0`.# Provided assertions
See [`utest.h`](./utest.h) for all assertions.
# Example
**demo.c**:
```c
#includestatic void *handle;
void lib_init(void) {
if ((handle = malloc(512)) == NULL)
exit(EXIT_FAILURE);
}void lib_shutdown(void) {
if (handle)
free(handle);
}int lib_fn(int a, int b) { return a + b; }
#ifdef TEST
#include "utest.h"
int test_should_pass(void) {
ASSERT_EQ(5, lib_fn(2, 3));
return TEST_PASS;
}int test_should_fail(void) {
ASSERT_EQ(7, lib_fn(2, 3));
return TEST_PASS;
}int test_explicit_fail(void) { FAIL(); }
int main(void) {
register_test(test_should_pass, lib_init, lib_shutdown, "should pass");
register_test(test_should_fail, lib_init, lib_shutdown, "should fail");
register_test(test_explicit_fail, lib_init, lib_shutdown, "explicit fail");return run_tests();
}#endif
``````
$ gcc -o -c utest.o utest.c
$ gcc -D TEST -o -c demo.o demo.c
$ gcc -o demo demo.o utest.o
$ ./demoshould pass ... ok
should fail ... failed: demo.c+27: 7 != 5
explicit fail ... failed: demo.c+32: explicit failSummary: 1 passed, 2 failed.
```