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. */
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. */
tests_executed ++;
/* Skip forward until the next test or setup function. */
run_next_test = 0;
+ previous_setup_failed = 1;
}
previous_test_failed = 0;
break;
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})
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"
+)
--- /dev/null
+#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);
+}