From 4eee27953cbc2b50e6d58964e4de49d6fa22b987 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sun, 16 Feb 2020 23:07:12 +0100 Subject: [PATCH] generate a test report in junit format should improve gitlab-ci output --- .gitlab-ci.yml | 3 +++ src/Makefile | 2 +- src/tests/testmain.c | 49 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cda29240..7bcabd26 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 script: - "CFLAGS=-fsanitize=address LDFLAGS=-fsanitize=address OPTIM=0 make check || exit 1" + artifacts: + reports: + junit: test-report.xml checkversions: stage: test diff --git a/src/Makefile b/src/Makefile index 8a3ca5d6..a303fd76 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1023,7 +1023,7 @@ ifeq ($(SWITCH), 1) endif check: $(TESTSBIN) - $(TESTSBIN) + $(TESTSBIN) --xml ../test-report.xml # .tgz archive with source only files SRCARCH = ../src-$(VERSIONTAG).tgz diff --git a/src/tests/testmain.c b/src/tests/testmain.c index c320eef4..13c46cfe 100644 --- a/src/tests/testmain.c +++ b/src/tests/testmain.c @@ -105,8 +105,8 @@ static const struct { #define TEST(func) { Test_ ## func, # func }, #include "testlist.h" #undef TEST - { NULL, NULL} }; +#define TEST_COUNT (sizeof(tests) / sizeof(tests[0])) /** * Initializations for test program @@ -170,9 +170,29 @@ int main(int argc, char * * argv) { int i; int fail = 0; - int r[sizeof(tests) / sizeof(tests[0])]; - (void)argc; - (void)argv; + int r[TEST_COUNT]; + int fail_early = 0; + 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 ]\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; if (init() < 0) @@ -181,17 +201,30 @@ int main(int argc, char * * argv) 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, "\n"); + fprintf(xml, "\n", TEST_COUNT); + for (i = 0; i < (int)TEST_COUNT && !(fail_early && fail > 0) ; i++) { printf("Testing %s :\n", tests[i].test_name); + fprintf(xml, " \n", tests[i].test_name, "GrafX2"); r[i] = tests[i].test_func(); if (r[i]) printf(ESC_GREEN "OK" ESC_RESET "\n"); else { printf(ESC_RED "FAILED" ESC_RESET "\n"); fail++; + fprintf(xml, " failure details\n"); } + fprintf(xml, " \n"); } + fprintf(xml, "\n"); + fclose(xml); finish(); @@ -200,10 +233,10 @@ int main(int argc, char * * argv) printf(ESC_GREEN "All tests succesfull" ESC_RESET "\n"); return 0; } - else + else if (!fail_early) { 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]) { @@ -211,6 +244,6 @@ int main(int argc, char * * argv) } } puts(ESC_RESET); /* puts writes an additional newline character */ - return 1; } + return 1; }