aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Slebodnik <lslebodn@redhat.com>2014-02-26 13:45:02 +0100
committerAndreas Schneider <asn@cryptomilk.org>2014-03-03 09:58:23 +0100
commit70c723c5d39555f97cf37feb60255118ca68e14e (patch)
treed955af2093b7198fc0160e5e97f469daaddd3e49
parente6e99619e760076198281daa5042edb4f9840509 (diff)
downloadcmocka-70c723c5d39555f97cf37feb60255118ca68e14e.tar.gz
cmocka-70c723c5d39555f97cf37feb60255118ca68e14e.tar.xz
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>
-rw-r--r--src/cmocka.c7
-rw-r--r--tests/CMakeLists.txt42
-rw-r--r--tests/test_setup_fail.c47
3 files changed, 94 insertions, 2 deletions
diff --git a/src/cmocka.c b/src/cmocka.c
index 76cd783..23abf7e 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -1764,6 +1764,8 @@ int _run_tests(const UnitTest * const tests, const size_t number_of_tests) {
int run_next_test = 1;
/* Whether the previous test failed. */
int previous_test_failed = 0;
+ /* Whether the previous setup failed. */
+ int previous_setup_failed = 0;
/* Check point of the heap state. */
const ListNode * const check_point = check_point_allocated_blocks();
/* Current test being executed. */
@@ -1803,7 +1805,9 @@ int _run_tests(const UnitTest * const tests, const size_t number_of_tests) {
switch (test->function_type) {
case UNIT_TEST_FUNCTION_TYPE_TEST:
- run_next_test = 1;
+ if (! previous_setup_failed) {
+ run_next_test = 1;
+ }
break;
case UNIT_TEST_FUNCTION_TYPE_SETUP: {
/* Checkpoint the heap before the setup. */
@@ -1851,6 +1855,7 @@ int _run_tests(const UnitTest * const tests, const size_t number_of_tests) {
tests_executed ++;
/* Skip forward until the next test or setup function. */
run_next_test = 0;
+ previous_setup_failed = 1;
}
previous_test_failed = 0;
break;
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);
+}