diff --git a/subsys/testsuite/ztest/Kconfig b/subsys/testsuite/ztest/Kconfig index c7b23fd57ab..3fbade151e3 100644 --- a/subsys/testsuite/ztest/Kconfig +++ b/subsys/testsuite/ztest/Kconfig @@ -149,6 +149,15 @@ config ZTEST_VERBOSE_SUMMARY This option controls whether suite summary is shown verbosely or just in one line. +config ZTEST_FAIL_ON_ASSUME + bool "Fail the test run when an assumption fails" + default y + help + When enabled, the test binary will fail at the end if an assumption failed. This means + that while tests will still be marked as skipped on failed zassume calls, the final test + result will be shown as a failure in order to increase visibility. This precludes tests + that skipped with the ZTEST_EXPECT_SKIP annotation. + endif # ZTEST_NEW_API config TEST_LOGGING_FLUSH_AFTER_TEST diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_assert.h b/subsys/testsuite/ztest/include/zephyr/ztest_assert.h index bfce8d69653..4f7c4ecd4ed 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest_assert.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest_assert.h @@ -27,6 +27,7 @@ extern "C" { const char *ztest_relative_filename(const char *file); void ztest_test_fail(void); void ztest_test_skip(void); +void ztest_skip_failed_assumption(void); #if CONFIG_ZTEST_ASSERT_VERBOSE == 0 static inline bool z_zassert_(bool cond, const char *file, int line) @@ -46,7 +47,7 @@ static inline bool z_zassume_(bool cond, const char *file, int line) { if (cond == false) { PRINT("\n Assumption failed at %s:%d\n", ztest_relative_filename(file), line); - ztest_test_skip(); + ztest_skip_failed_assumption(); return false; } @@ -93,7 +94,7 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil vprintk(msg, vargs); printk("\n"); va_end(vargs); - ztest_test_skip(); + ztest_skip_failed_assumption(); return false; } #if CONFIG_ZTEST_ASSERT_VERBOSE == 2 diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h b/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h index 77773406516..b3e8c4d6aeb 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h @@ -331,6 +331,9 @@ void ztest_test_pass(void); */ void ztest_test_skip(void); + +void ztest_skip_failed_assumption(void); + #define Z_TEST(suite, fn, t_options, use_fixture) \ struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \ static void _##suite##_##fn##_wrapper(void *data); \ diff --git a/subsys/testsuite/ztest/src/ztest_new.c b/subsys/testsuite/ztest/src/ztest_new.c index 525fad12019..ecede8060cc 100644 --- a/subsys/testsuite/ztest/src/ztest_new.c +++ b/subsys/testsuite/ztest/src/ztest_new.c @@ -291,6 +291,15 @@ static inline const char *get_friendly_phase_name(enum ztest_phase phase) } } +static bool current_test_failed_assumption; +void ztest_skip_failed_assumption(void) +{ + if (IS_ENABLED(CONFIG_ZTEST_FAIL_ON_ASSUME)) { + current_test_failed_assumption = true; + } + ztest_test_skip(); +} + #ifndef KERNEL /* Static code analysis tool can raise a violation that the standard header @@ -399,6 +408,9 @@ out: ret = get_final_test_result(test, ret); Z_TC_END_RESULT(ret, test->name); + if (ret == TC_SKIP && current_test_failed_assumption) { + test_status = 1; + } return ret; } @@ -579,6 +591,9 @@ static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test ret = get_final_test_result(test, ret); Z_TC_END_RESULT(ret, test->name); + if (ret == TC_SKIP && current_test_failed_assumption) { + test_status = 1; + } return ret; } @@ -661,6 +676,7 @@ static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite) #endif TC_SUITE_START(suite->name); + current_test_failed_assumption = false; test_result = ZTEST_RESULT_PENDING; phase = TEST_PHASE_SETUP; #ifndef KERNEL diff --git a/tests/ztest/error_hook/src/main.c b/tests/ztest/error_hook/src/main.c index 7c5ae0a35bc..8805037f7f7 100644 --- a/tests/ztest/error_hook/src/main.c +++ b/tests/ztest/error_hook/src/main.c @@ -372,12 +372,14 @@ static void *fail_assume_in_setup_setup(void) ZTEST_SUITE(fail_assume_in_setup, NULL, fail_assume_in_setup_setup, NULL, NULL, NULL); +ZTEST_EXPECT_SKIP(fail_assume_in_setup, test_to_skip0); ZTEST(fail_assume_in_setup, test_to_skip0) { /* This test should never be run */ ztest_test_fail(); } +ZTEST_EXPECT_SKIP(fail_assume_in_setup, test_to_skip1); ZTEST(fail_assume_in_setup, test_to_skip1) { /* This test should never be run */ @@ -392,12 +394,14 @@ static void fail_assume_in_before_before(void *unused) ZTEST_SUITE(fail_assume_in_before, NULL, NULL, fail_assume_in_before_before, NULL, NULL); +ZTEST_EXPECT_SKIP(fail_assume_in_before, test_to_skip0); ZTEST(fail_assume_in_before, test_to_skip0) { /* This test should never be run */ ztest_test_fail(); } +ZTEST_EXPECT_SKIP(fail_assume_in_before, test_to_skip1); ZTEST(fail_assume_in_before, test_to_skip1) { /* This test should never be run */ @@ -406,6 +410,7 @@ ZTEST(fail_assume_in_before, test_to_skip1) ZTEST_SUITE(fail_assume_in_test, NULL, NULL, NULL, NULL, NULL); +ZTEST_EXPECT_SKIP(fail_assume_in_test, test_to_skip); ZTEST(fail_assume_in_test, test_to_skip) { zassume_true(false);