Lightweight unit testing harness for C projects.
Inspired by siu/minunit and MinUnit.
This unit testing harness uses GCC's __attribute__((constructor))
magic to automagically register each test to its test suite without the need to do so explicitely.
- Lightweight and easy to install (just a
.h
file) - Simple tests management interface
- define suites and define the tests.
- No need to add the tests to the suites explicitly.
- Easily extensible (PRs wellcome).
- No dynamic memory allocations.
- Requires support for
__attribute__((constructor))
from the compiler.
#include "lightunit.h"
static int g_var;
static const char *g_foo = "foo";
LU_SUITE_CREATE(suite);
LU_SUITE_SETUP(suite) {
g_var = 1;
}
LU_TEST(suite, test1) {
LU_ASSERT(g_var == 1);
g_var++;
LU_ASSERT_STR_EQ("foo", g_foo);
LU_ASSERT(2 == 3);
}
LU_TEST(suite, test2) {
LU_ASSERT(++g_var == 2);
LU_ASSERT_STR_EQ("bar", g_foo);
}
int main() {
LU_SUITE_RUN(suite);
LU_SUITE_REPORT(suite);
return LU_SUITE_STATUS(suite);
}
Prints:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SUITE: suite
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> test1 : -1 (examples/readme_example.c+16: '2 == 3' asserted to False)
> test2 : -2 (examples/readme_example.c+21: Strings different: "bar" <> g_foo)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
STATUS: FAIL (2 tests, 2 fails, 5 assertions)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Add verbosity options to report
- Add more assertion types
ASSERT_FLT_EQ
/ASSERT_DBL_EQ
for float/double approx.FAIL(message)
- Time tests
- Other compilers support