From 72715901ffbb69a85862184b4cbc6b77d0433041 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Mon, 21 May 2018 11:38:55 +0200 Subject: [PATCH] Split tests and mark them as skipped if setup fails --- tests/meson.build | 21 ++++++++++++++-- tests/test_document.c | 8 +++++- tests/test_session.c | 9 ++++++- tests/test_utils.c | 8 +++++- tests/tests.c | 57 ++++++++++++++++++++----------------------- tests/tests.h | 11 +++++++++ 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 tests/tests.h diff --git a/tests/meson.build b/tests/meson.build index 90f8954..44d57f8 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -14,13 +14,30 @@ if check.found() 'test_utils.c' ] - tests = executable('tests', test_sources, + document = executable('test_document', ['test_document.c', 'tests.c'], dependencies: build_dependencies + test_dependencies, include_directories: include_directories, c_args: defines + flags ) + test('document', document, + timeout: 60*60 + ) - test('tests', tests, + session = executable('test_session', ['test_session.c', 'tests.c'], + dependencies: build_dependencies + test_dependencies, + include_directories: include_directories, + c_args: defines + flags + ) + test('session', session, + timeout: 60*60 + ) + + utils = executable('test_utils', ['test_utils.c', 'tests.c'], + dependencies: build_dependencies + test_dependencies, + include_directories: include_directories, + c_args: defines + flags + ) + test('utils', utils, timeout: 60*60 ) endif diff --git a/tests/test_document.c b/tests/test_document.c index 75b401d..dd78d27 100644 --- a/tests/test_document.c +++ b/tests/test_document.c @@ -3,6 +3,7 @@ #include #include "document.h" +#include "tests.h" START_TEST(test_open) { fail_unless(zathura_document_open(NULL, NULL, NULL, NULL, NULL) == NULL, "Could create document", NULL); @@ -11,7 +12,7 @@ START_TEST(test_open) { fail_unless(zathura_document_open(NULL, "fl", NULL, "pw", NULL) == NULL, "Could create document", NULL); } END_TEST -Suite* suite_document() +static Suite* suite_document(void) { TCase* tcase = NULL; Suite* suite = suite_create("Document"); @@ -23,3 +24,8 @@ Suite* suite_document() return suite; } + +int main() +{ + return run_suite(suite_document()); +} diff --git a/tests/test_session.c b/tests/test_session.c index 86d1b95..48c08bb 100644 --- a/tests/test_session.c +++ b/tests/test_session.c @@ -3,6 +3,7 @@ #include #include "zathura.h" +#include "tests.h" START_TEST(test_create) { zathura_t* zathura = zathura_create(); @@ -11,15 +12,21 @@ START_TEST(test_create) { zathura_free(zathura); } END_TEST -Suite* suite_session() +static Suite* suite_session(void) { TCase* tcase = NULL; Suite* suite = suite_create("Session"); /* basic */ tcase = tcase_create("basic"); + tcase_add_checked_fixture(tcase, setup, NULL); tcase_add_test(tcase, test_create); suite_add_tcase(suite, tcase); return suite; } + +int main() +{ + return run_suite(suite_session()); +} diff --git a/tests/test_utils.c b/tests/test_utils.c index 10c224f..81ef1c4 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -3,6 +3,7 @@ #include #include "utils.h" +#include "tests.h" START_TEST(test_file_valid_extension_null) { fail_unless(file_valid_extension(NULL, NULL) == false, NULL); @@ -10,7 +11,7 @@ START_TEST(test_file_valid_extension_null) { fail_unless(file_valid_extension(NULL, "pdf") == false, NULL); } END_TEST -Suite* suite_utils() +static Suite* suite_utils(void) { TCase* tcase = NULL; Suite* suite = suite_create("Utils"); @@ -22,3 +23,8 @@ Suite* suite_utils() return suite; } + +int main() +{ + return run_suite(suite_utils()); +} diff --git a/tests/tests.c b/tests/tests.c index 5e3098f..cc75599 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -1,41 +1,38 @@ /* See LICENSE file for license and copyright information */ -#include -#include #include +#include -#define LENGTH(x) (sizeof(x)/sizeof((x)[0])) +#include "tests.h" -extern Suite* suite_session(); -extern Suite* suite_utils(); -extern Suite* suite_document(); - -typedef Suite* (*suite_create_fnt_t)(void); - -const suite_create_fnt_t suites[] = { - suite_utils, - suite_document, - suite_session, -}; - -int -main(int argc, char* argv[]) +int run_suite(Suite* suite) { - Suite* suite = NULL; - SRunner* suite_runner = NULL; - int number_failed = 0; + SRunner* suite_runner = srunner_create(suite); + srunner_run_all(suite_runner, CK_NORMAL); + const int number_failed = srunner_ntests_failed(suite_runner); - /* init gtk */ - gtk_init(&argc, &argv); + int ret = EXIT_SUCCESS; + if (number_failed != 0) { + ret = EXIT_FAILURE; - /* run test suites */ - for (unsigned int i = 0; i < LENGTH(suites); i++) { - suite = suites[i](); - suite_runner = srunner_create(suite); - srunner_run_all(suite_runner, CK_NORMAL); - number_failed += srunner_ntests_failed(suite_runner); - srunner_free(suite_runner); + const int tests_run = srunner_ntests_run(suite_runner); + TestResult** results = srunner_failures(suite_runner); + + for (int i = 0; i < tests_run; ++i) { + if (tr_ctx(results[i]) == CK_CTX_SETUP) { + /* mark tests as skipped */ + ret = 77; + break; + } + } } - return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; + srunner_free(suite_runner); + + return ret; +} + +void setup(void) +{ + fail_unless(gtk_init_check(NULL, NULL) == TRUE, "GTK+ initializitation failed", NULL); } diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..8593c5c --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,11 @@ +/* See LICENSE file for license and copyright information */ + +#ifndef ZATHURA_TESTS_H +#define ZATHURA_TESTS_H + +#include + +int run_suite(Suite* suite); +void setup(void); + +#endif