From 5e8c5d90db2c55e203c11dbbec91c3e44bcb2023 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 12 Sep 2014 17:18:57 +0200 Subject: tests: Use new cmocka test runner in our tests and examples Signed-off-by: Andreas Schneider --- tests/CMakeLists.txt | 4 ++-- tests/test_assert_macros.c | 7 ++++--- tests/test_assert_macros_fail.c | 7 ++++--- tests/test_basics.c | 16 ++++++++++------ tests/test_exception_handler.c | 6 +++--- tests/test_fixtures.c | 24 ++++++++++++++---------- tests/test_group_fixtures.c | 16 ++++++++-------- tests/test_setup_fail.c | 27 +++++++++++++++++---------- 8 files changed, 62 insertions(+), 45 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a058b5f..b95c96a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,7 +50,7 @@ set_tests_properties( test_setup_fail_1_failed PROPERTIES PASS_REGULAR_EXPRESSION - "\\[ FAILED \\] 1 test\\(s\\), listed below:" + "\\[ ERROR \\] int_test_ignored" ) add_test (test_setup_fail_1_passed test_setup_fail) @@ -66,7 +66,7 @@ set_tests_properties( test_setup_fail_match_failed PROPERTIES PASS_REGULAR_EXPRESSION - "\\[ FAILED \\] int_test_ignored_setup_fail" + "\\[ ERROR \\] int_test_ignored" ) add_test (test_setup_fail_match_passed test_setup_fail) diff --git a/tests/test_assert_macros.c b/tests/test_assert_macros.c index 7f643b7..2906517 100644 --- a/tests/test_assert_macros.c +++ b/tests/test_assert_macros.c @@ -26,8 +26,9 @@ static void test_assert_return_code(void **state) } int main(void) { - const UnitTest tests[] = { - unit_test(test_assert_return_code), + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_assert_return_code), }; - return run_tests(tests); + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_assert_macros_fail.c b/tests/test_assert_macros_fail.c index cd49023..2227dfc 100644 --- a/tests/test_assert_macros_fail.c +++ b/tests/test_assert_macros_fail.c @@ -27,8 +27,9 @@ static void test_assert_return_code_fail(void **state) } int main(void) { - const UnitTest tests[] = { - unit_test(test_assert_return_code_fail), + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_assert_return_code_fail), }; - return run_tests(tests); + + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_basics.c b/tests/test_basics.c index 63fc27b..1bb493f 100644 --- a/tests/test_basics.c +++ b/tests/test_basics.c @@ -22,17 +22,21 @@ #include #include -static void setup(void **state) { +static int setup(void **state) { int *answer = malloc(sizeof(int)); assert_non_null(answer); *answer = 42; *state = answer; + + return 0; } -static void teardown(void **state) { +static int teardown(void **state) { free(*state); + + return 0; } /* A test case that does nothing and succeeds. */ @@ -49,10 +53,10 @@ static void int_test_success(void **state) { int main(void) { - const UnitTest tests[] = { - unit_test(null_test_success), - unit_test_setup_teardown(int_test_success, setup, teardown), + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_test_success), + cmocka_unit_test_setup_teardown(int_test_success, setup, teardown), }; - return run_tests(tests); + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_exception_handler.c b/tests/test_exception_handler.c index 5727d1e..94c6842 100644 --- a/tests/test_exception_handler.c +++ b/tests/test_exception_handler.c @@ -20,9 +20,9 @@ static void test_segfault_recovery(void **state) } int main(void) { - const UnitTest tests[] = { - unit_test(test_segfault_recovery), + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_segfault_recovery), }; - return run_tests(tests); + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_fixtures.c b/tests/test_fixtures.c index d7c166a..4597626 100644 --- a/tests/test_fixtures.c +++ b/tests/test_fixtures.c @@ -5,14 +5,18 @@ #include -static void setup_only(void **state) +static int setup_only(void **state) { *state = malloc(1); + + return 0; } -static void teardown_only(void **state) +static int teardown_only(void **state) { free(*state); + + return 0; } static void malloc_setup_test(void **state) @@ -28,14 +32,14 @@ static void malloc_teardown_test(void **state) } int main(void) { - const UnitTest tests[] = { - unit_test_setup(malloc_setup_test, setup_only), - unit_test_setup(malloc_setup_test, setup_only), - unit_test_teardown(malloc_teardown_test, teardown_only), - unit_test_teardown(malloc_teardown_test, teardown_only), - unit_test_teardown(malloc_teardown_test, teardown_only), - unit_test_teardown(malloc_teardown_test, teardown_only), + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup(malloc_setup_test, setup_only), + cmocka_unit_test_setup(malloc_setup_test, setup_only), + cmocka_unit_test_teardown(malloc_teardown_test, teardown_only), + cmocka_unit_test_teardown(malloc_teardown_test, teardown_only), + cmocka_unit_test_teardown(malloc_teardown_test, teardown_only), + cmocka_unit_test_teardown(malloc_teardown_test, teardown_only), }; - return run_tests(tests); + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_group_fixtures.c b/tests/test_group_fixtures.c index 6f49e24..09f39b1 100644 --- a/tests/test_group_fixtures.c +++ b/tests/test_group_fixtures.c @@ -6,20 +6,22 @@ #include #include -static void group_setup(void **state) +static int group_setup(void **state) { int *answer = malloc(sizeof(int)); assert_non_null(answer); *answer = 42; *state = answer; + return 0; } -static void group_teardown(void **state) +static int group_teardown(void **state) { int *answer = (int *)*state; free(answer); + return 0; } static void test_value_equal(void **state) @@ -37,12 +39,10 @@ static void test_value_range(void **state) } int main(void) { - const UnitTest tests[] = { - group_test_setup(group_setup), - unit_test(test_value_equal), - unit_test(test_value_range), - group_test_teardown(group_teardown), + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_value_equal), + cmocka_unit_test(test_value_range), }; - return run_group_tests(tests); + return cmocka_run_group_tests(tests, group_setup, group_teardown); } diff --git a/tests/test_setup_fail.c b/tests/test_setup_fail.c index 923886c..e3f8df8 100644 --- a/tests/test_setup_fail.c +++ b/tests/test_setup_fail.c @@ -5,11 +5,11 @@ #include #include -static void setup_fail(void **state) { +static int setup_fail(void **state) { *state = NULL; /* We need to fail in setup */ - assert_non_null(NULL); + return -1; } static void int_test_ignored(void **state) { @@ -17,13 +17,18 @@ static void int_test_ignored(void **state) { assert_non_null(*state); } -static void setup_ok(void **state) { - int *answer = malloc(sizeof(int)); +static int setup_ok(void **state) { + int *answer; - assert_non_null(answer); + answer = malloc(sizeof(int)); + if (answer == NULL) { + return -1; + } *answer = 42; *state = answer; + + return 0; } /* A test case that does check if an int is equal. */ @@ -33,15 +38,17 @@ static void int_test_success(void **state) { assert_int_equal(*answer, 42); } -static void teardown(void **state) { +static int teardown(void **state) { free(*state); + + return 0; } int main(void) { - const UnitTest tests[] = { - unit_test_setup_teardown(int_test_ignored, setup_fail, teardown), - unit_test_setup_teardown(int_test_success, setup_ok, teardown), + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(int_test_ignored, setup_fail, teardown), + cmocka_unit_test_setup_teardown(int_test_success, setup_ok, teardown), }; - return run_tests(tests); + return cmocka_run_group_tests(tests, NULL, NULL); } -- cgit v1.2.3