generate a test report in junit format

should improve gitlab-ci output
This commit is contained in:
Thomas Bernard 2020-02-16 23:07:12 +01:00
parent 1664c8410d
commit 4eee27953c
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
3 changed files with 45 additions and 9 deletions

View File

@ -13,6 +13,9 @@ tests:
- apt-get install -y -qq libsdl1.2-dev libpng-dev libsdl-ttf2.0-dev libsdl-image1.2-dev liblua5.3-dev - apt-get install -y -qq libsdl1.2-dev libpng-dev libsdl-ttf2.0-dev libsdl-image1.2-dev liblua5.3-dev
script: script:
- "CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address OPTIM=0 make check || exit 1" - "CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address OPTIM=0 make check || exit 1"
artifacts:
reports:
junit: test-report.xml
checkversions: checkversions:
stage: test stage: test

View File

@ -1023,7 +1023,7 @@ ifeq ($(SWITCH), 1)
endif endif
check: $(TESTSBIN) check: $(TESTSBIN)
$(TESTSBIN) $(TESTSBIN) --xml ../test-report.xml
# .tgz archive with source only files # .tgz archive with source only files
SRCARCH = ../src-$(VERSIONTAG).tgz SRCARCH = ../src-$(VERSIONTAG).tgz

View File

@ -105,8 +105,8 @@ static const struct {
#define TEST(func) { Test_ ## func, # func }, #define TEST(func) { Test_ ## func, # func },
#include "testlist.h" #include "testlist.h"
#undef TEST #undef TEST
{ NULL, NULL}
}; };
#define TEST_COUNT (sizeof(tests) / sizeof(tests[0]))
/** /**
* Initializations for test program * Initializations for test program
@ -170,9 +170,29 @@ int main(int argc, char * * argv)
{ {
int i; int i;
int fail = 0; int fail = 0;
int r[sizeof(tests) / sizeof(tests[0])]; int r[TEST_COUNT];
(void)argc; int fail_early = 0;
(void)argv; const char * xml_path = "test-report.xml";
FILE * xml; // see https://llg.cubic.org/docs/junit/
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0)
{
printf("Usage: %s [--fail-early] [--xml <report.xml>]\n", argv[0]);
printf("default path for xml report is \"%s\"\n", xml_path);
return 0;
}
else if (strcmp(argv[i], "--fail-early") == 0)
fail_early = 1;
else if ((i < (argc - 1)) && strcmp(argv[i], "--xml") == 0)
xml_path = argv[++i];
else
{
fprintf(stderr, "Unrecognized option \"%s\"\n", argv[i]);
return 1;
}
}
GFX2_verbosity_level = GFX2_DEBUG; GFX2_verbosity_level = GFX2_DEBUG;
if (init() < 0) if (init() < 0)
@ -181,17 +201,30 @@ int main(int argc, char * * argv)
return 1; return 1;
} }
for (i = 0; tests[i].test_func != 0; i++) xml = fopen(xml_path, "w");
if (xml == NULL)
{
fprintf(stderr, "Failed to open %s for writing\n", xml_path);
return 1;
}
fprintf(xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(xml, "<testsuite name=\"GrafX2\" tests=\"%lu\">\n", TEST_COUNT);
for (i = 0; i < (int)TEST_COUNT && !(fail_early && fail > 0) ; i++)
{ {
printf("Testing %s :\n", tests[i].test_name); printf("Testing %s :\n", tests[i].test_name);
fprintf(xml, " <testcase name=\"%s\" classname=\"%s\">\n", tests[i].test_name, "GrafX2");
r[i] = tests[i].test_func(); r[i] = tests[i].test_func();
if (r[i]) if (r[i])
printf(ESC_GREEN "OK" ESC_RESET "\n"); printf(ESC_GREEN "OK" ESC_RESET "\n");
else { else {
printf(ESC_RED "FAILED" ESC_RESET "\n"); printf(ESC_RED "FAILED" ESC_RESET "\n");
fail++; fail++;
fprintf(xml, " <failure message=\"test failure\">failure details</failure>\n");
} }
fprintf(xml, " </testcase>\n");
} }
fprintf(xml, "</testsuite>\n");
fclose(xml);
finish(); finish();
@ -200,10 +233,10 @@ int main(int argc, char * * argv)
printf(ESC_GREEN "All tests succesfull" ESC_RESET "\n"); printf(ESC_GREEN "All tests succesfull" ESC_RESET "\n");
return 0; return 0;
} }
else else if (!fail_early)
{ {
printf(ESC_RED "%d tests failed :\n ", fail); printf(ESC_RED "%d tests failed :\n ", fail);
for (i = 0; tests[i].test_func != 0; i++) for (i = 0; i < (int)TEST_COUNT; i++)
{ {
if (!r[i]) if (!r[i])
{ {
@ -211,6 +244,6 @@ int main(int argc, char * * argv)
} }
} }
puts(ESC_RESET); /* puts writes an additional newline character */ puts(ESC_RESET); /* puts writes an additional newline character */
}
return 1; return 1;
} }
}