From dbd63e8008b4e04863c407d91693b85b1eb7ef3f Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Mon, 29 Aug 2022 12:44:31 -0600 Subject: [PATCH] ztest: add tests for failing on zassume Add a test to make sure that when a zassume fails, we mark the test as failed and print an "Assumption failed" error for the test Signed-off-by: Yuval Peress --- tests/ztest/fail/Kconfig | 4 ++++ tests/ztest/fail/core/CMakeLists.txt | 2 ++ tests/ztest/fail/core/src/unexpected_assume.cpp | 16 ++++++++++++++++ tests/ztest/fail/testcase.yaml | 8 ++++++++ 4 files changed, 30 insertions(+) create mode 100644 tests/ztest/fail/core/src/unexpected_assume.cpp diff --git a/tests/ztest/fail/Kconfig b/tests/ztest/fail/Kconfig index 8f5527a23da..251175623ae 100644 --- a/tests/ztest/fail/Kconfig +++ b/tests/ztest/fail/Kconfig @@ -22,6 +22,9 @@ config ZTEST_FAIL_TEST_PASS_AFTER config ZTEST_FAIL_TEST_PASS_TEARDOWN bool "Add a call to ztest_test_pass() in the teardown phase" +config ZTEST_FAIL_TEST_UNEXPECTED_ASSUME + bool "Add a test which fails a zassume() call" + endchoice config TEST_ERROR_STRING @@ -32,5 +35,6 @@ config TEST_ERROR_STRING default "ERROR: cannot skip in test 'teardown()', bailing" if ZTEST_FAIL_TEST_ASSUME_TEARDOWN default "ERROR: cannot pass in test 'after()', bailing" if ZTEST_FAIL_TEST_PASS_AFTER default "ERROR: cannot pass in test 'teardown()', bailing" if ZTEST_FAIL_TEST_PASS_TEARDOWN + default "Assumption failed at " if ZTEST_FAIL_TEST_UNEXPECTED_ASSUME source "Kconfig.zephyr" diff --git a/tests/ztest/fail/core/CMakeLists.txt b/tests/ztest/fail/core/CMakeLists.txt index 70f884aa3d6..cf56362b9ee 100644 --- a/tests/ztest/fail/core/CMakeLists.txt +++ b/tests/ztest/fail/core/CMakeLists.txt @@ -19,6 +19,8 @@ elseif(CONFIG_ZTEST_FAIL_TEST_PASS_AFTER) list(APPEND SOURCES src/pass_after.cpp) elseif(CONFIG_ZTEST_FAIL_TEST_PASS_TEARDOWN) list(APPEND SOURCES src/pass_teardown.cpp) +elseif(CONFIG_ZTEST_FAIL_TEST_UNEXPECTED_ASSUME) + list(APPEND SOURCES src/unexpected_assume.cpp) endif() if(BOARD STREQUAL unit_testing) diff --git a/tests/ztest/fail/core/src/unexpected_assume.cpp b/tests/ztest/fail/core/src/unexpected_assume.cpp new file mode 100644 index 00000000000..6e59a3239a4 --- /dev/null +++ b/tests/ztest/fail/core/src/unexpected_assume.cpp @@ -0,0 +1,16 @@ +/* Copyright (c) 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "fail_test.hpp" + +void fail_test_after_impl(void) {} + +void fail_test_teardown_impl(void) {} + +ZTEST(fail, test_unexpected_assume) +{ + zassume_true(false, NULL); +} diff --git a/tests/ztest/fail/testcase.yaml b/tests/ztest/fail/testcase.yaml index 67bb0986be6..35311ebd825 100644 --- a/tests/ztest/fail/testcase.yaml +++ b/tests/ztest/fail/testcase.yaml @@ -50,3 +50,11 @@ tests: platform_allow: native_posix extra_configs: - CONFIG_ZTEST_FAIL_TEST_PASS_TEARDOWN=y + testing.fail.unit.fail_on_bad_assumption: + type: unit + extra_configs: + - CONFIG_ZTEST_FAIL_TEST_UNEXPECTED_ASSUME=y + testing.fail.zephyr.fail_on_bad_assumption: + platform_allow: native_posix + extra_configs: + - CONFIG_ZTEST_FAIL_TEST_UNEXPECTED_ASSUME=y