Test should not be run if setup function fails
authorLukas Slebodnik <lslebodn@redhat.com>
Wed, 26 Feb 2014 12:45:02 +0000 (13:45 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Mon, 3 Mar 2014 08:58:23 +0000 (09:58 +0100)
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>
src/cmocka.c
tests/CMakeLists.txt
tests/test_setup_fail.c [new file with mode: 0644]

index 76cd7830b7694f9c7308bb4064151739d093d8d4..23abf7e23b7396d36476ae8e85c4caf0766520f6 100644 (file)
@@ -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;
index 82cc046da1b7da21e0a2c8855dd510d5ca161797..8287b61f9d218ea16892e25547737629cdd26da7 100644 (file)
@@ -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 (file)
index 0000000..923886c
--- /dev/null
@@ -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);
+}