diff options
author | Lukas Slebodnik <lslebodn@redhat.com> | 2014-02-26 13:45:02 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2014-03-03 09:58:23 +0100 |
commit | 70c723c5d39555f97cf37feb60255118ca68e14e (patch) | |
tree | d955af2093b7198fc0160e5e97f469daaddd3e49 /tests | |
parent | e6e99619e760076198281daa5042edb4f9840509 (diff) | |
download | cmocka-70c723c5d39555f97cf37feb60255118ca68e14e.tar.gz cmocka-70c723c5d39555f97cf37feb60255118ca68e14e.tar.bz2 cmocka-70c723c5d39555f97cf37feb60255118ca68e14e.zip |
Test should not be run if setup function fails
Assertions are commonly used in setup function. If setup function fail
test should not be executed, because it may result into unexpected behaviour
(crash)
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 42 | ||||
-rw-r--r-- | tests/test_setup_fail.c | 47 |
2 files changed, 88 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 82cc046..8287b61 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,7 +11,8 @@ set(CMOCKA_TESTS test_assert_macros test_assert_macros_fail test_exception_handler - test_basics) + test_basics + test_setup_fail) foreach(_CMOCKA_TEST ${CMOCKA_TESTS}) add_cmocka_test(${_CMOCKA_TEST} ${_CMOCKA_TEST}.c ${CMOCKA_SHARED_LIBRARY}) @@ -34,3 +35,42 @@ set_tests_properties( PASS_REGULAR_EXPRESSION "Test failed with exception: (Segmentation fault|Segmentation Fault|11)" ) + +set_tests_properties( + test_setup_fail + PROPERTIES + WILL_FAIL + 1 +) + +add_test (test_setup_fail_1_failed test_setup_fail) +set_tests_properties( + test_setup_fail_1_failed + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ FAILED \\] 1 test\\(s\\), listed below:" +) + +add_test (test_setup_fail_1_passed test_setup_fail) +set_tests_properties( + test_setup_fail_1_passed + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ PASSED \\] 1 test\\(s\\)." +) + +add_test (test_setup_fail_match_failed test_setup_fail) +set_tests_properties( + test_setup_fail_match_failed + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ FAILED \\] int_test_ignored_setup_fail" +) + +add_test (test_setup_fail_match_passed test_setup_fail) +set_tests_properties( + test_setup_fail_match_passed + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ OK \\] int_test_success" +) diff --git a/tests/test_setup_fail.c b/tests/test_setup_fail.c new file mode 100644 index 0000000..923886c --- /dev/null +++ b/tests/test_setup_fail.c @@ -0,0 +1,47 @@ +#define UNIT_TESTING 1 + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +static void setup_fail(void **state) { + *state = NULL; + + /* We need to fail in setup */ + assert_non_null(NULL); +} + +static void int_test_ignored(void **state) { + /* should not be called */ + assert_non_null(*state); +} + +static void setup_ok(void **state) { + int *answer = malloc(sizeof(int)); + + assert_non_null(answer); + *answer = 42; + + *state = answer; +} + +/* A test case that does check if an int is equal. */ +static void int_test_success(void **state) { + int *answer = *state; + + assert_int_equal(*answer, 42); +} + +static void teardown(void **state) { + free(*state); +} + +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), + }; + + return run_tests(tests); +} |