Consider this minimal framework for running unit tests:
#include <stdio.h>
#define TEST(name) int test_##name()
#define RUN_TEST(name) \
do {printf("%s\t%s\n", #name, test_##name() ? "OK" : "FAIL");} while(0)
#define ASSERT_EQ(left, right) \
do {return (left) == (right);} while(0)
TEST(addition) {
ASSERT_EQ(1 + 2, 3);
}
TEST(mult_fail) {
ASSERT_EQ(42, 6 * 9);
}
int main() {
RUN_TEST(addition);
RUN_TEST(mult_fail);
}
It is used like so:
> gcc test_runner.c && ./a.out
addition OK
mult_fail FAIL
How can it be made so that the TEST macro automatically registers the tests? I want the main function to contain a single RUN_ALL_TESTS statement without having to refer to all the tests by name.
I know this can easily be done in C++, but I would prefer to see a purely C solution.
Aucun commentaire:
Enregistrer un commentaire