I'm implementing a test library and I want to be able to print the test functions, which fail. Therefore I need a way to get the name of the failing function. I know there are predefined identifiers like __func__ and __FUNCTION__, but they only give me the name of the last executed function. I also don't want to use those identifiers inside the unit test functions, but in the procedure, which runs all the tests.
I posted an example, which uses an imaginary function called function_name. Is there any macro or something else available for this purpose?
/** Description
* This function calls a number of unit tests and print the results.
* Please note the imaginary function 'function_name'.
*
* Parameters:
* tests: Function pointers to unit tests
* number_tests: The number of tests to run
*/
void run_tests(unit_test* tests, unsigned int number_tests) {
int number_passed = 0;
for (unsigned int i = 0; i < number_tests; i++) {
// Execute the unit test and get the result
test_result result = (*tests[i])();
if (result == TEST_PASSED)
number_passed++;
else
printf("Test %s failed!\n", function_name(*tests[i]));
}
printf("%d/%d passed tests, %d failed tests\n", number_passed,
number_tests, number_tests - number_passed);
}
tests(somewhere not shown). One alternative if to reap them into an array ofstruct func { int (*pfn)(); const char *fname; }and stringize the name whilst your loading uptests, reporting that instead.unit_testarray. Otherwise since you have a unison API where everything returns atest_result, why can't that contain a copy of the__func__string?function_name(*tests[i])makes sense however.