From 568dee3d4e8ae7079c7d90fff1f73a70ef7d9e3c Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Tue, 10 Oct 2023 00:06:05 +0000 Subject: [PATCH] ztest: remove old ztest api Remove old Ztest API entirely. New API is the now the default. Signed-off-by: Anas Nashif --- cmake/linker_script/common/common-ram.cmake | 3 - cmake/modules/unittest.cmake | 19 +- scripts/build/gen_kobject_list.py | 4 +- subsys/logging/Kconfig.processing | 2 +- subsys/testsuite/include/zephyr/tc_util.h | 15 - subsys/testsuite/ztest/CMakeLists.txt | 14 +- subsys/testsuite/ztest/Kconfig | 13 +- .../ztest/include/zephyr/ztest_test.h | 551 +++++++++- .../include/zephyr/ztest_test_deprecated.h | 296 ------ .../ztest/include/zephyr/ztest_test_new.h | 560 ---------- subsys/testsuite/ztest/src/ztest.c | 959 ++++++++++++++---- subsys/testsuite/ztest/src/ztest_posix.c | 8 +- .../hci_uart_async/src/test_hci_uart_async.c | 2 +- .../kernel/spinlock/src/spinlock_error_case.c | 2 +- tests/subsys/emul/src/main.c | 2 +- 15 files changed, 1324 insertions(+), 1126 deletions(-) delete mode 100644 subsys/testsuite/ztest/include/zephyr/ztest_test_deprecated.h delete mode 100644 subsys/testsuite/ztest/include/zephyr/ztest_test_new.h diff --git a/cmake/linker_script/common/common-ram.cmake b/cmake/linker_script/common/common-ram.cmake index e6ef59eaf29eb06..cbe162bf3b12f0d 100644 --- a/cmake/linker_script/common/common-ram.cmake +++ b/cmake/linker_script/common/common-ram.cmake @@ -94,9 +94,6 @@ endif() if(CONFIG_ZTEST) zephyr_iterable_section(NAME ztest_suite_node GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) zephyr_iterable_section(NAME ztest_suite_stats GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) -endif() - -if(CONFIG_ZTEST_NEW_API) zephyr_iterable_section(NAME ztest_unit_test GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) zephyr_iterable_section(NAME ztest_test_rule GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) zephyr_iterable_section(NAME ztest_expected_result_entry GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) diff --git a/cmake/modules/unittest.cmake b/cmake/modules/unittest.cmake index be5fa56741a45b4..306cc879585d393 100644 --- a/cmake/modules/unittest.cmake +++ b/cmake/modules/unittest.cmake @@ -103,19 +103,12 @@ if(LIBS) message(FATAL_ERROR "This variable is not supported, see SOURCES instead") endif() -if(CONFIG_ZTEST_NEW_API) - target_sources(testbinary PRIVATE - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_new.c - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_rules.c - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_defaults.c - ) -else() - target_sources(testbinary PRIVATE - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest.c - ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c - ) -endif() +target_sources(testbinary PRIVATE + ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest.c + ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c + ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_rules.c + ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_defaults.c +) target_compile_definitions(test_interface INTERFACE ZTEST_UNITTEST) diff --git a/scripts/build/gen_kobject_list.py b/scripts/build/gen_kobject_list.py index 64524c67499d52a..420d96485d44bf5 100755 --- a/scripts/build/gen_kobject_list.py +++ b/scripts/build/gen_kobject_list.py @@ -110,8 +110,8 @@ ("k_event", ("CONFIG_EVENTS", False, True)), ("ztest_suite_node", ("CONFIG_ZTEST", True, False)), ("ztest_suite_stats", ("CONFIG_ZTEST", True, False)), - ("ztest_unit_test", ("CONFIG_ZTEST_NEW_API", True, False)), - ("ztest_test_rule", ("CONFIG_ZTEST_NEW_API", True, False)), + ("ztest_unit_test", ("CONFIG_ZTEST", True, False)), + ("ztest_test_rule", ("CONFIG_ZTEST", True, False)), ("rtio", ("CONFIG_RTIO", False, False)), ("rtio_iodev", ("CONFIG_RTIO", False, False)), ("sensor_decoder_api", ("CONFIG_SENSOR_ASYNC_API", True, False)) diff --git a/subsys/logging/Kconfig.processing b/subsys/logging/Kconfig.processing index a1d7c955d40572e..8a53f129ac376d2 100644 --- a/subsys/logging/Kconfig.processing +++ b/subsys/logging/Kconfig.processing @@ -7,7 +7,7 @@ if !LOG_MODE_MINIMAL config LOG_PRINTK bool "Process printk messages" - default y if PRINTK && (!ZTEST || ZTEST_NEW_API) + default y if PRINTK && ZTEST help If enabled, printk messages are redirected to the logging subsystem. The messages are formatted in place and logged unconditionally. diff --git a/subsys/testsuite/include/zephyr/tc_util.h b/subsys/testsuite/include/zephyr/tc_util.h index 69c9359b50f6337..ea7ed98c8c7522b 100644 --- a/subsys/testsuite/include/zephyr/tc_util.h +++ b/subsys/testsuite/include/zephyr/tc_util.h @@ -127,18 +127,11 @@ static inline void print_nothing(const char *fmt, ...) } #ifndef TC_PRINT -/* Need to check for CONFIG_ZTEST_NEW_API since the TC_PRINT - * is also used by the old ztest. - */ -#ifdef CONFIG_ZTEST_NEW_API #if defined(CONFIG_ZTEST_VERBOSE_OUTPUT) #define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__) #else #define TC_PRINT(fmt, ...) print_nothing(fmt, ##__VA_ARGS__) #endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */ -#else -#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__) -#endif /* CONFIG_ZTEST_NEW_API */ #endif /* TC_PRINT */ #ifndef TC_SUMMARY_PRINT @@ -146,15 +139,11 @@ static inline void print_nothing(const char *fmt, ...) #endif #ifndef TC_START_PRINT -#ifdef CONFIG_ZTEST_NEW_API #if defined(CONFIG_ZTEST_VERBOSE_OUTPUT) #define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name); #else #define TC_START_PRINT(name) print_nothing(name) #endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */ -#else -#define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name); -#endif /* CONFIG_ZTEST_NEW_API */ #endif /* TC_START_PRINT */ #ifndef TC_START @@ -169,15 +158,11 @@ static inline void print_nothing(const char *fmt, ...) #endif #ifndef TC_END_PRINT -#ifdef CONFIG_ZTEST_NEW_API #if defined(CONFIG_ZTEST_VERBOSE_OUTPUT) #define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE #else #define TC_END_PRINT(result, fmt, ...) print_nothing(fmt) #endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */ -#else -#define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE -#endif /* CONFIG_ZTEST_NEW_API */ #endif /* TC_END_PRINT */ /* prints result and the function name */ diff --git a/subsys/testsuite/ztest/CMakeLists.txt b/subsys/testsuite/ztest/CMakeLists.txt index c9c9332898581f7..7817bc2f30b859b 100644 --- a/subsys/testsuite/ztest/CMakeLists.txt +++ b/subsys/testsuite/ztest/CMakeLists.txt @@ -3,7 +3,6 @@ zephyr_syscall_header( ${ZEPHYR_BASE}/subsys/testsuite/ztest/include/zephyr/ztest_error_hook.h ${ZEPHYR_BASE}/subsys/testsuite/ztest/include/zephyr/ztest_test.h - ${ZEPHYR_BASE}/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h ) zephyr_include_directories( @@ -16,16 +15,17 @@ if(DEFINED TC_RUNID) endif() zephyr_library() -zephyr_library_sources_ifndef(CONFIG_ZTEST_NEW_API src/ztest.c) -zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_new.c) -zephyr_library_sources( src/ztest_error_hook.c) -zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_rules.c) +zephyr_library_sources( + src/ztest.c + src/ztest_error_hook.c + src/ztest_rules.c + ) zephyr_library_sources_ifdef(CONFIG_ZTEST_MOCKING src/ztest_mock.c) zephyr_library_sources_ifdef(CONFIG_ZTRESS src/ztress.c) if(CONFIG_ARCH_POSIX) - zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_posix.c) + zephyr_library_sources(src/ztest_posix.c) else() - zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_defaults.c) + zephyr_library_sources(src/ztest_defaults.c) endif() diff --git a/subsys/testsuite/ztest/Kconfig b/subsys/testsuite/ztest/Kconfig index 91c942b7a5cf4e2..977f882d838dc9d 100644 --- a/subsys/testsuite/ztest/Kconfig +++ b/subsys/testsuite/ztest/Kconfig @@ -10,13 +10,6 @@ config ZTEST if ZTEST -config ZTEST_NEW_API - bool "Use the new Ztest API" - default y - help - Enables the new Ztest APIs for creating suites and unit tests in - separate compilation units as well as the new 'rules' API. - config ZTEST_STACK_SIZE int "Test function thread stack size" default 2048 if COVERAGE_GCOV @@ -114,9 +107,7 @@ config ZTEST_WARN_NO_OPTIMIZATIONS optimizations. Please don't file issues when running tests that are not explicitly tuned to work in this configuration. -if ZTEST_NEW_API - -menu "ztest provided rules" +menu "ZTest provided rules" config ZTEST_RULE_1CPU bool "Run all the tests on a single CPU" @@ -185,8 +176,6 @@ config ZTEST_FAIL_ON_ASSUME 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 bool "When enabled logs are flushed after each test case" default y diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_test.h b/subsys/testsuite/ztest/include/zephyr/ztest_test.h index b9635560c02cf41..a95a73ec4a05d76 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest_test.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest_test.h @@ -1,23 +1,558 @@ /* * Copyright (c) 2016 Intel Corporation - * Copyright (c) 2021 Google LLC * * SPDX-License-Identifier: Apache-2.0 */ -#ifndef ZEPHYR_TESTSUITE_INCLUDE_ZTEST_TEST_H_ -#define ZEPHYR_TESTSUITE_INCLUDE_ZTEST_TEST_H_ +/** + * @file + * + * @brief Zephyr testing framework _test. + */ + +#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_ +#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_ -#ifdef CONFIG_ZTEST_NEW_API -#include +#include +#include +#include +#include + +#if defined(CONFIG_USERSPACE) +#define __USERSPACE_FLAGS (K_USER) #else -#include -#endif /* !CONFIG_ZTEST_NEW_API */ +#define __USERSPACE_FLAGS (0) +#endif #ifdef __cplusplus extern "C" { #endif +/** + * @brief The expected result of a test. + * + * @see ZTEST_EXPECT_FAIL + * @see ZTEST_EXPECT_SKIP + */ +enum ztest_expected_result { + ZTEST_EXPECTED_RESULT_FAIL = 0, /**< Expect a test to fail */ + ZTEST_EXPECTED_RESULT_SKIP, /**< Expect a test to pass */ +}; + +/** + * @brief A single expectation entry allowing tests to fail/skip and be considered passing. + * + * @see ZTEST_EXPECT_FAIL + * @see ZTEST_EXPECT_SKIP + */ +struct ztest_expected_result_entry { + const char *test_suite_name; /**< The test suite's name for the expectation */ + const char *test_name; /**< The test's name for the expectation */ + enum ztest_expected_result expected_result; /**< The expectation */ +}; + +extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[]; +extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_end[]; + +#define __ZTEST_EXPECT(_suite_name, _test_name, expectation) \ + static const STRUCT_SECTION_ITERABLE( \ + ztest_expected_result_entry, \ + UTIL_CAT(UTIL_CAT(z_ztest_expected_result_, _suite_name), _test_name)) = { \ + .test_suite_name = STRINGIFY(_suite_name), \ + .test_name = STRINGIFY(_test_name), \ + .expected_result = expectation, \ + } + +/** + * @brief Expect a test to fail (mark it passing if it failed) + * + * Adding this macro to your logic will allow the failing test to be considered passing, example: + * + * ZTEST_EXPECT_FAIL(my_suite, test_x); + * ZTEST(my_suite, text_x) { + * zassert_true(false, NULL); + * } + * + * @param _suite_name The name of the suite + * @param _test_name The name of the test + */ +#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \ + __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL) + +/** + * @brief Expect a test to skip (mark it passing if it failed) + * + * Adding this macro to your logic will allow the failing test to be considered passing, example: + * + * ZTEST_EXPECT_SKIP(my_suite, test_x); + * ZTEST(my_suite, text_x) { + * zassume_true(false, NULL); + * } + * + * @param _suite_name The name of the suite + * @param _test_name The name of the test + */ +#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \ + __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP) + +struct ztest_unit_test { + const char *test_suite_name; + const char *name; + void (*test)(void *data); + uint32_t thread_options; + + /** Stats */ + struct ztest_unit_test_stats *const stats; +}; + +extern struct ztest_unit_test _ztest_unit_test_list_start[]; +extern struct ztest_unit_test _ztest_unit_test_list_end[]; +#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start) + +/** + * Stats about a ztest suite + */ +struct ztest_suite_stats { + /** The number of times that the suite ran */ + uint32_t run_count; + /** The number of times that the suite was skipped */ + uint32_t skip_count; + /** The number of times that the suite failed */ + uint32_t fail_count; +}; + +struct ztest_unit_test_stats { + /** The number of times that the test ran */ + uint32_t run_count; + /** The number of times that the test was skipped */ + uint32_t skip_count; + /** The number of times that the test failed */ + uint32_t fail_count; + /** The number of times that the test passed */ + uint32_t pass_count; + /** The longest duration of the test across multiple times */ + uint32_t duration_worst_ms; +}; + +/** + * Setup function to run before running this suite + * + * @return Pointer to the data structure that will be used throughout this test suite + */ +typedef void *(*ztest_suite_setup_t)(void); + +/** + * Function to run before each test in this suite + * + * @param fixture The test suite's fixture returned from setup() + */ +typedef void (*ztest_suite_before_t)(void *fixture); + +/** + * Function to run after each test in this suite + * + * @param fixture The test suite's fixture returned from setup() + */ +typedef void (*ztest_suite_after_t)(void *fixture); + +/** + * Teardown function to run after running this suite + * + * @param fixture The test suite's data returned from setup() + */ +typedef void (*ztest_suite_teardown_t)(void *fixture); + +/** + * An optional predicate function to determine if the test should run. If NULL, then the + * test will only run once on the first attempt. + * + * @param global_state The current state of the test application. + * @return True if the suite should be run; false to skip. + */ +typedef bool (*ztest_suite_predicate_t)(const void *global_state); + +/** + * A single node of test suite. Each node should be added to a single linker section which will + * allow ztest_run_test_suites() to iterate over the various nodes. + */ +struct ztest_suite_node { + /** The name of the test suite. */ + const char *const name; + + /** Setup function */ + const ztest_suite_setup_t setup; + + /** Before function */ + const ztest_suite_before_t before; + + /** After function */ + const ztest_suite_after_t after; + + /** Teardown function */ + const ztest_suite_teardown_t teardown; + + /** Optional predicate filter */ + const ztest_suite_predicate_t predicate; + + /** Stats */ + struct ztest_suite_stats *const stats; +}; + +extern struct ztest_suite_node _ztest_suite_node_list_start[]; +extern struct ztest_suite_node _ztest_suite_node_list_end[]; +#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start) + +/** + * Create and register a ztest suite. Using this macro creates a new test suite (using + * ztest_test_suite). It then creates a struct ztest_suite_node in a specific linker section. + * + * Tests can then be run by calling ztest_run_test_suites(const void *state) by passing + * in the current state. See the documentation for ztest_run_test_suites for more info. + * + * @param SUITE_NAME The name of the suite (see ztest_test_suite for more info) + * @param PREDICATE A function to test against the state and determine if the test should run. + * @param setup_fn The setup function to call before running this test suite + * @param before_fn The function to call before each unit test in this suite + * @param after_fn The function to call after each unit test in this suite + * @param teardown_fn The function to call after running all the tests in this suite + */ +#define ZTEST_SUITE(SUITE_NAME, PREDICATE, setup_fn, before_fn, after_fn, teardown_fn) \ + struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME); \ + static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \ + UTIL_CAT(z_ztest_test_node_, SUITE_NAME)) = { \ + .name = STRINGIFY(SUITE_NAME), \ + .setup = (setup_fn), \ + .before = (before_fn), \ + .after = (after_fn), \ + .teardown = (teardown_fn), \ + .predicate = PREDICATE, \ + .stats = &UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME), \ + } +/** + * Default entry point for running or listing registered unit tests. + * + * @param state The current state of the machine as it relates to the test executable. + */ +void ztest_run_all(const void *state); + +/** + * The result of the current running test. It's possible that the setup function sets the result + * to ZTEST_RESULT_SUITE_* which will apply the failure/skip to every test in the suite. + */ +enum ztest_result { + ZTEST_RESULT_PENDING, + ZTEST_RESULT_PASS, + ZTEST_RESULT_FAIL, + ZTEST_RESULT_SKIP, + ZTEST_RESULT_SUITE_SKIP, + ZTEST_RESULT_SUITE_FAIL, +}; +/** + * Each enum member represents a distinct phase of execution for the test binary. + * TEST_PHASE_FRAMEWORK is active when internal ztest code is executing; the rest refer to + * corresponding phases of user test code. + */ +enum ztest_phase { + TEST_PHASE_SETUP, + TEST_PHASE_BEFORE, + TEST_PHASE_TEST, + TEST_PHASE_AFTER, + TEST_PHASE_TEARDOWN, + TEST_PHASE_FRAMEWORK, +}; + +/** + * Run the registered unit tests which return true from their predicate function. + * + * @param state The current state of the machine as it relates to the test executable. + * @return The number of tests that ran. + */ + +#ifdef ZTEST_UNITTEST +int z_impl_ztest_run_test_suites(const void *state); +static inline int ztest_run_test_suites(const void *state) +{ + return z_impl_ztest_run_test_suites(state); +} + +#else +__syscall int ztest_run_test_suites(const void *state); +#endif + +#ifdef ZTEST_UNITTEST +void z_impl___ztest_set_test_result(enum ztest_result new_result); +static inline void __ztest_set_test_result(enum ztest_result new_result) +{ + z_impl___ztest_set_test_result(new_result); +} + +void z_impl___ztest_set_test_phase(enum ztest_phase new_phase); +static inline void __ztest_set_test_phase(enum ztest_phase new_phase) +{ + z_impl___ztest_set_test_phase(new_phase); +} +#else +__syscall void __ztest_set_test_result(enum ztest_result new_result); +__syscall void __ztest_set_test_phase(enum ztest_phase new_phase); +#endif + +#ifndef ZTEST_UNITTEST +#include +#endif + +/** + * @brief Fails the test if any of the registered tests did not run. + * + * When registering test suites, a pragma function can be provided to determine WHEN the test should + * run. It is possible that a test suite could be registered but the pragma always prevents it from + * running. In cases where a test should make sure that ALL suites ran at least once, this function + * may be called at the end of test_main(). It will cause the test to fail if any suite was + * registered but never ran. + */ +void ztest_verify_all_test_suites_ran(void); + +/** + * @brief Run a test suite. + * + * Internal implementation. Do not call directly. This will run the full test suite along with some + * checks for fast failures and initialization. + * + * @param name The name of the suite to run. + * @return Negative value if the test suite never ran; otherwise, return the number of failures. + */ +int z_ztest_run_test_suite(const char *name); + +/** + * @brief Returns next test within suite. + * + * @param suite Name of suite to get next test from. + * @param prev Previous unit test acquired from suite, use NULL to return first + * unit test. + * @return struct ztest_unit_test* + */ +struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev); + +/* definitions for use with testing application shared memory */ +#ifdef CONFIG_USERSPACE +#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition) +#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition) +#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition) +extern struct k_mem_partition ztest_mem_partition; +#else +#define ZTEST_DMEM +#define ZTEST_BMEM +#define ZTEST_SECTION .data +#endif + +/** + * @defgroup ztest_test Ztest testing macros + * @ingroup ztest + * + * This module eases the testing process by providing helpful macros and other + * testing structures. + * + * @{ + */ + +/** + * @brief Fail the currently running test. + * + * This is the function called from failed assertions and the like. You + * probably don't need to call it yourself. + */ +void ztest_test_fail(void); + +/** + * @brief Pass the currently running test. + * + * Normally a test passes just by returning without an assertion failure. + * However, if the success case for your test involves a fatal fault, + * you can call this function from k_sys_fatal_error_handler to indicate that + * the test passed before aborting the thread. + */ +void ztest_test_pass(void); + +/** + * @brief Skip the current test. + * + */ +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); \ + static void suite##_##fn( \ + COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \ + static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \ + .test_suite_name = STRINGIFY(suite), \ + .name = STRINGIFY(fn), \ + .test = (_##suite##_##fn##_wrapper), \ + .thread_options = t_options, \ + .stats = &z_ztest_unit_test_stats_##suite##_##fn \ + }; \ + static void _##suite##_##fn##_wrapper(void *wrapper_data) \ + { \ + COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \ + (ARG_UNUSED(wrapper_data); suite##_##fn();)) \ + } \ + static inline void suite##_##fn( \ + COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))) + +#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0) +#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1) + +/** + * @brief Skips the test if config is enabled + * + * Use this macro at the start of your test case, to skip it when + * config is enabled. Useful when your test is still under development. + * + * @param config The Kconfig option used to skip the test. + */ +#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ()) + +/** + * @brief Skips the test if config is not enabled + * + * Use this macro at the start of your test case, to skip it when + * config is not enabled. Useful when your need to skip test if some + * conifiguration option is not enabled. + * + * @param config The Kconfig option used to skip the test (if not enabled). + */ +#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip())) + +/** + * @brief Create and register a new unit test. + * + * Calling this macro will create a new unit test and attach it to the declared `suite`. The `suite` + * does not need to be defined in the same compilation unit. + * + * @param suite The name of the test suite to attach this test + * @param fn The test function to call. + */ +#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0) + +/** + * @brief Define a test function that should run as a user thread + * + * This macro behaves exactly the same as ZTEST, but calls the test function in user space if + * `CONFIG_USERSPACE` was enabled. + * + * @param suite The name of the test suite to attach this test + * @param fn The test function to call. + */ +#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER) + +/** + * @brief Define a test function + * + * This macro behaves exactly the same as ZTEST(), but the function takes an argument for the + * fixture of type `struct suite##_fixture*` named `this`. + * + * @param suite The name of the test suite to attach this test + * @param fn The test function to call. + */ +#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0) + +/** + * @brief Define a test function that should run as a user thread + * + * If CONFIG_USERSPACE is not enabled, this is functionally identical to ZTEST_F(). The test + * function takes a single fixture argument of type `struct suite##_fixture*` named `this`. + * + * @param suite The name of the test suite to attach this test + * @param fn The test function to call. + */ +#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER) + +/** + * @brief Test rule callback function signature + * + * The function signature that can be used to register a test rule's before/after callback. This + * provides access to the test and the fixture data (if provided). + * + * @param test Pointer to the unit test in context + * @param data Pointer to the test's fixture data (may be NULL) + */ +typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data); + +/** @private */ +struct ztest_test_rule { + ztest_rule_cb before_each; + ztest_rule_cb after_each; +}; + +/** + * @brief Define a test rule that will run before/after each unit test. + * + * Functions defined here will run before/after each unit test for every test suite. Along with the + * callback, the test functions are provided a pointer to the test being run, and the data. This + * provides a mechanism for tests to perform custom operations depending on the specific test or + * the data (for example logging may use the test's name). + * + * Ordering: + * - Test rule's `before` function will run before the suite's `before` function. This is done to + * allow the test suite's customization to take precedence over the rule which is applied to all + * suites. + * - Test rule's `after` function is not guaranteed to run in any particular order. + * + * @param name The name for the test rule (must be unique within the compilation unit) + * @param before_each_fn The callback function (ztest_rule_cb) to call before each test + * (may be NULL) + * @param after_each_fn The callback function (ztest_rule_cb) to call after each test (may be NULL) + */ +#define ZTEST_RULE(name, before_each_fn, after_each_fn) \ + static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \ + .before_each = (before_each_fn), \ + .after_each = (after_each_fn), \ + } + +extern struct ztest_test_rule _ztest_test_rule_list_start[]; +extern struct ztest_test_rule _ztest_test_rule_list_end[]; + +/** + * @brief A 'before' function to use in test suites that just need to start 1cpu + * + * Ignores data, and calls z_test_1cpu_start() + * + * @param data The test suite's data + */ +void ztest_simple_1cpu_before(void *data); + +/** + * @brief A 'after' function to use in test suites that just need to stop 1cpu + * + * Ignores data, and calls z_test_1cpu_stop() + * + * @param data The test suite's data + */ +void ztest_simple_1cpu_after(void *data); + +/** + * @brief Run the specified test suite. + * + * @param suite Test suite to run. + */ +#define ztest_run_test_suite(suite) z_ztest_run_test_suite(STRINGIFY(suite)) + +/** + * @brief Structure for architecture specific APIs + * + */ +struct ztest_arch_api { + void (*run_all)(const void *state); + bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite); + bool (*should_test_run)(const char *suite, const char *test); +}; + +/** + * @} + */ + __syscall void z_test_1cpu_start(void); __syscall void z_test_1cpu_stop(void); @@ -31,4 +566,4 @@ __syscall void sys_clock_tick_set(uint64_t tick); #include #endif -#endif /* ZEPHYR_TESTSUITE_INCLUDE_ZTEST_TEST_H_ */ +#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */ diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_test_deprecated.h b/subsys/testsuite/ztest/include/zephyr/ztest_test_deprecated.h deleted file mode 100644 index 8207b184a5d9f77..000000000000000 --- a/subsys/testsuite/ztest/include/zephyr/ztest_test_deprecated.h +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @file - * - * @brief Zephyr testing framework _test_deprecated. - */ - -#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_ -#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_ - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct unit_test { - const char *name; - void (*test)(void); - void (*setup)(void); - void (*teardown)(void); - uint32_t thread_options; -}; - -/** - * Stats about a ztest suite - */ -struct ztest_suite_stats { - /** The number of times that the suite ran */ - uint32_t run_count; - /** The number of times that the suite was skipped */ - uint32_t skip_count; - /** The number of times that the suite failed */ - uint32_t fail_count; -}; - -/** - * A single node of test suite. Each node should be added to a single linker section which will - * allow ztest_run_registered_test_suites() to iterate over the various nodes. - */ -struct ztest_suite_node { - /** The name of the test suite. */ - const char *name; - /** Pointer to the test suite. */ - struct unit_test *suite; - /** - * An optional predicate function to determine if the test should run. If NULL, then the - * test will only run once on the first attempt. - * - * @param state The current state of the test application. - * @return True if the suite should be run; false to skip. - */ - bool (*predicate)(const void *state); - /** Stats */ - struct ztest_suite_stats *stats; -}; - -extern struct ztest_suite_node _ztest_suite_node_list_start[]; -extern struct ztest_suite_node _ztest_suite_node_list_end[]; - -/** - * Create and register a ztest suite. Using this macro creates a new test suite (using - * ztest_test_suite). It then creates a struct ztest_suite_node in a specific linker section. - * - * Tests can then be run by calling ztest_run_registered_test_suites(const void *state) by passing - * in the current state. See the documentation for ztest_run_registered_test_suites for more info. - * - * @param SUITE_NAME The name of the suite (see ztest_test_suite for more info) - * @param PREDICATE A function to test against the state and determine if the test should run. - * @param args Varargs placeholder for the remaining arguments passed for the unit tests. - */ -#define ztest_register_test_suite(SUITE_NAME, PREDICATE, args...) \ - ztest_test_suite(SUITE_NAME, ##args); \ - struct ztest_suite_stats UTIL_CAT(z_ztest_test_node_stats_, SUITE_NAME); \ - static STRUCT_SECTION_ITERABLE(ztest_suite_node, z_ztest_test_node_##SUITE_NAME) = { \ - .name = #SUITE_NAME, \ - .suite = _##SUITE_NAME, \ - .predicate = PREDICATE, \ - .stats = &UTIL_CAT(z_ztest_test_node_stats_, SUITE_NAME), \ - }; - -/** - * Run the registered unit tests which return true from their pragma function. - * - * @param state The current state of the machine as it relates to the test executable. - * @return The number of tests that ran. - */ -__deprecated -int ztest_run_registered_test_suites(const void *state); - -/** - * @brief Fails the test if any of the registered tests did not run. - * - * When registering test suites, a pragma function can be provided to determine WHEN the test should - * run. It is possible that a test suite could be registered but the pragma always prevents it from - * running. In cases where a test should make sure that ALL suites ran at least once, this function - * may be called at the end of test_main(). It will cause the test to fail if any suite was - * registered but never ran. - */ -__deprecated -void ztest_verify_all_registered_test_suites_ran(void); - -/** - * @brief Run a test suite. - * - * Internal implementation. Do not call directly. This will run the full test suite along with some - * checks for fast failures and initialization. - * - * @param name The name of the suite to run. - * @param suite Pointer to the first unit test. - * @return Negative value if the test suite never ran; otherwise, return the number of failures. - */ -int z_ztest_run_test_suite(const char *name, struct unit_test *suite); - -/** - * @defgroup ztest_test_deprecated Ztest testing macros - * @ingroup ztest - * - * This module eases the testing process by providing helpful macros and other - * testing structures. - * - * @{ - */ - -/** - * @brief Fail the currently running test. - * - * This is the function called from failed assertions and the like. You - * probably don't need to call it yourself. - */ -void ztest_test_fail(void); - -/** - * @brief Pass the currently running test. - * - * Normally a test passes just by returning without an assertion failure. - * However, if the success case for your test involves a fatal fault, - * you can call this function from k_sys_fatal_error_handler to indicate that - * the test passed before aborting the thread. - */ -void ztest_test_pass(void); - -/** - * @brief Skip the current test. - */ -void ztest_test_skip(void); - -/** - * @brief Do nothing, successfully. - * - * Unit test / setup function / teardown function that does - * nothing, successfully. Can be used as a parameter to - * ztest_unit_test_setup_teardown(). - */ -static inline void unit_test_noop(void) -{ -} - -/** - * @brief Define a test with setup and teardown functions - * - * This should be called as an argument to ztest_test_suite. The test will - * be run in the following order: @a setup, @a fn, @a teardown. - * - * @param fn Main test function - * @param setup Setup function - * @param teardown Teardown function - */ -#define ztest_unit_test_setup_teardown(fn, setup, teardown) \ - { \ - STRINGIFY(fn), fn, setup, teardown, 0 \ - } - -/** - * @brief Define a user mode test with setup and teardown functions - * - * This should be called as an argument to ztest_test_suite. The test will - * be run in the following order: @a setup, @a fn, @a teardown. ALL - * test functions will be run in user mode, and only if CONFIG_USERSPACE - * is enabled, otherwise this is the same as ztest_unit_test_setup_teardown(). - * - * @param fn Main test function - * @param setup Setup function - * @param teardown Teardown function - */ -#define ztest_user_unit_test_setup_teardown(fn, setup, teardown) \ - { \ - STRINGIFY(fn), fn, setup, teardown, K_USER \ - } - -/** - * @brief Define a test function - * - * This should be called as an argument to ztest_test_suite. - * - * @param fn Test function - */ -#define ztest_unit_test(fn) \ - ztest_unit_test_setup_teardown(fn, unit_test_noop, unit_test_noop) - -/** - * @brief Define a test function that should run as a user thread - * - * This should be called as an argument to ztest_test_suite. - * If CONFIG_USERSPACE is not enabled, this is functionally identical to - * ztest_unit_test(). - * - * @param fn Test function - */ -#define ztest_user_unit_test(fn) \ - ztest_user_unit_test_setup_teardown(fn, unit_test_noop, unit_test_noop) - -/** - * @brief Define a SMP-unsafe test function - * - * As ztest_unit_test(), but ensures all test code runs on only - * one CPU when in SMP. - * - * @param fn Test function - */ -#ifdef CONFIG_SMP -#define ztest_1cpu_unit_test(fn) \ - ztest_unit_test_setup_teardown(fn, z_test_1cpu_start, z_test_1cpu_stop) -#else -#define ztest_1cpu_unit_test(fn) ztest_unit_test(fn) -#endif - -/** - * @brief Define a SMP-unsafe test function that should run as a user thread - * - * As ztest_user_unit_test(), but ensures all test code runs on only - * one CPU when in SMP. - * - * @param fn Test function - */ -#ifdef CONFIG_SMP -#define ztest_1cpu_user_unit_test(fn) \ - ztest_user_unit_test_setup_teardown(fn, z_test_1cpu_start, z_test_1cpu_stop) -#else -#define ztest_1cpu_user_unit_test(fn) ztest_user_unit_test(fn) -#endif - -/* definitions for use with testing application shared memory */ -#ifdef CONFIG_USERSPACE -#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition) -#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition) -#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition) -extern struct k_mem_partition ztest_mem_partition; -#else -#define ZTEST_DMEM -#define ZTEST_BMEM -#define ZTEST_SECTION .data -#endif - -/** - * @brief Define a test suite - * - * This function should be called in the following fashion: - * ```{.c} - * ztest_test_suite(test_suite_name, - * ztest_unit_test(test_function), - * ztest_unit_test(test_other_function) - * ); - * - * ztest_run_test_suite(test_suite_name); - * ``` - * - * @param suite Name of the testing suite - */ -#define ztest_test_suite(suite, ...) __DEPRECATED_MACRO \ - static ZTEST_DMEM struct unit_test _##suite[] = { __VA_ARGS__, { 0 } } -/** - * @brief Run the specified test suite. - * - * @param suite Test suite to run. - */ -#define ztest_run_test_suite(suite) __DEPRECATED_MACRO \ - z_ztest_run_test_suite(#suite, _##suite) - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */ diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h b/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h deleted file mode 100644 index e676bf7d3d8719e..000000000000000 --- a/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h +++ /dev/null @@ -1,560 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @file - * - * @brief Zephyr testing framework _test. - */ - -#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_ -#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_ - -#include -#include -#include -#include - -#if defined(CONFIG_USERSPACE) -#define __USERSPACE_FLAGS (K_USER) -#else -#define __USERSPACE_FLAGS (0) -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief The expected result of a test. - * - * @see ZTEST_EXPECT_FAIL - * @see ZTEST_EXPECT_SKIP - */ -enum ztest_expected_result { - ZTEST_EXPECTED_RESULT_FAIL = 0, /**< Expect a test to fail */ - ZTEST_EXPECTED_RESULT_SKIP, /**< Expect a test to pass */ -}; - -/** - * @brief A single expectation entry allowing tests to fail/skip and be considered passing. - * - * @see ZTEST_EXPECT_FAIL - * @see ZTEST_EXPECT_SKIP - */ -struct ztest_expected_result_entry { - const char *test_suite_name; /**< The test suite's name for the expectation */ - const char *test_name; /**< The test's name for the expectation */ - enum ztest_expected_result expected_result; /**< The expectation */ -}; - -extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[]; -extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_end[]; - -#define __ZTEST_EXPECT(_suite_name, _test_name, expectation) \ - static const STRUCT_SECTION_ITERABLE( \ - ztest_expected_result_entry, \ - UTIL_CAT(UTIL_CAT(z_ztest_expected_result_, _suite_name), _test_name)) = { \ - .test_suite_name = STRINGIFY(_suite_name), \ - .test_name = STRINGIFY(_test_name), \ - .expected_result = expectation, \ - } - -/** - * @brief Expect a test to fail (mark it passing if it failed) - * - * Adding this macro to your logic will allow the failing test to be considered passing, example: - * - * ZTEST_EXPECT_FAIL(my_suite, test_x); - * ZTEST(my_suite, text_x) { - * zassert_true(false, NULL); - * } - * - * @param _suite_name The name of the suite - * @param _test_name The name of the test - */ -#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \ - __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL) - -/** - * @brief Expect a test to skip (mark it passing if it failed) - * - * Adding this macro to your logic will allow the failing test to be considered passing, example: - * - * ZTEST_EXPECT_SKIP(my_suite, test_x); - * ZTEST(my_suite, text_x) { - * zassume_true(false, NULL); - * } - * - * @param _suite_name The name of the suite - * @param _test_name The name of the test - */ -#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \ - __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP) - -struct ztest_unit_test { - const char *test_suite_name; - const char *name; - void (*test)(void *data); - uint32_t thread_options; - - /** Stats */ - struct ztest_unit_test_stats *const stats; -}; - -extern struct ztest_unit_test _ztest_unit_test_list_start[]; -extern struct ztest_unit_test _ztest_unit_test_list_end[]; -#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start) - -/** - * Stats about a ztest suite - */ -struct ztest_suite_stats { - /** The number of times that the suite ran */ - uint32_t run_count; - /** The number of times that the suite was skipped */ - uint32_t skip_count; - /** The number of times that the suite failed */ - uint32_t fail_count; -}; - -struct ztest_unit_test_stats { - /** The number of times that the test ran */ - uint32_t run_count; - /** The number of times that the test was skipped */ - uint32_t skip_count; - /** The number of times that the test failed */ - uint32_t fail_count; - /** The number of times that the test passed */ - uint32_t pass_count; - /** The longest duration of the test across multiple times */ - uint32_t duration_worst_ms; -}; - -/** - * Setup function to run before running this suite - * - * @return Pointer to the data structure that will be used throughout this test suite - */ -typedef void *(*ztest_suite_setup_t)(void); - -/** - * Function to run before each test in this suite - * - * @param fixture The test suite's fixture returned from setup() - */ -typedef void (*ztest_suite_before_t)(void *fixture); - -/** - * Function to run after each test in this suite - * - * @param fixture The test suite's fixture returned from setup() - */ -typedef void (*ztest_suite_after_t)(void *fixture); - -/** - * Teardown function to run after running this suite - * - * @param fixture The test suite's data returned from setup() - */ -typedef void (*ztest_suite_teardown_t)(void *fixture); - -/** - * An optional predicate function to determine if the test should run. If NULL, then the - * test will only run once on the first attempt. - * - * @param global_state The current state of the test application. - * @return True if the suite should be run; false to skip. - */ -typedef bool (*ztest_suite_predicate_t)(const void *global_state); - -/** - * A single node of test suite. Each node should be added to a single linker section which will - * allow ztest_run_test_suites() to iterate over the various nodes. - */ -struct ztest_suite_node { - /** The name of the test suite. */ - const char *const name; - - /** Setup function */ - const ztest_suite_setup_t setup; - - /** Before function */ - const ztest_suite_before_t before; - - /** After function */ - const ztest_suite_after_t after; - - /** Teardown function */ - const ztest_suite_teardown_t teardown; - - /** Optional predicate filter */ - const ztest_suite_predicate_t predicate; - - /** Stats */ - struct ztest_suite_stats *const stats; -}; - -extern struct ztest_suite_node _ztest_suite_node_list_start[]; -extern struct ztest_suite_node _ztest_suite_node_list_end[]; -#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start) - -/** - * Create and register a ztest suite. Using this macro creates a new test suite (using - * ztest_test_suite). It then creates a struct ztest_suite_node in a specific linker section. - * - * Tests can then be run by calling ztest_run_test_suites(const void *state) by passing - * in the current state. See the documentation for ztest_run_test_suites for more info. - * - * @param SUITE_NAME The name of the suite (see ztest_test_suite for more info) - * @param PREDICATE A function to test against the state and determine if the test should run. - * @param setup_fn The setup function to call before running this test suite - * @param before_fn The function to call before each unit test in this suite - * @param after_fn The function to call after each unit test in this suite - * @param teardown_fn The function to call after running all the tests in this suite - */ -#define ZTEST_SUITE(SUITE_NAME, PREDICATE, setup_fn, before_fn, after_fn, teardown_fn) \ - struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME); \ - static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \ - UTIL_CAT(z_ztest_test_node_, SUITE_NAME)) = { \ - .name = STRINGIFY(SUITE_NAME), \ - .setup = (setup_fn), \ - .before = (before_fn), \ - .after = (after_fn), \ - .teardown = (teardown_fn), \ - .predicate = PREDICATE, \ - .stats = &UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME), \ - } -/** - * Default entry point for running or listing registered unit tests. - * - * @param state The current state of the machine as it relates to the test executable. - */ -void ztest_run_all(const void *state); - -/** - * The result of the current running test. It's possible that the setup function sets the result - * to ZTEST_RESULT_SUITE_* which will apply the failure/skip to every test in the suite. - */ -enum ztest_result { - ZTEST_RESULT_PENDING, - ZTEST_RESULT_PASS, - ZTEST_RESULT_FAIL, - ZTEST_RESULT_SKIP, - ZTEST_RESULT_SUITE_SKIP, - ZTEST_RESULT_SUITE_FAIL, -}; -/** - * Each enum member represents a distinct phase of execution for the test binary. - * TEST_PHASE_FRAMEWORK is active when internal ztest code is executing; the rest refer to - * corresponding phases of user test code. - */ -enum ztest_phase { - TEST_PHASE_SETUP, - TEST_PHASE_BEFORE, - TEST_PHASE_TEST, - TEST_PHASE_AFTER, - TEST_PHASE_TEARDOWN, - TEST_PHASE_FRAMEWORK, -}; - -/** - * Run the registered unit tests which return true from their predicate function. - * - * @param state The current state of the machine as it relates to the test executable. - * @return The number of tests that ran. - */ - -#ifdef ZTEST_UNITTEST -int z_impl_ztest_run_test_suites(const void *state); -static inline int ztest_run_test_suites(const void *state) -{ - return z_impl_ztest_run_test_suites(state); -} - -#else -__syscall int ztest_run_test_suites(const void *state); -#endif - -#ifdef ZTEST_UNITTEST -void z_impl___ztest_set_test_result(enum ztest_result new_result); -static inline void __ztest_set_test_result(enum ztest_result new_result) -{ - z_impl___ztest_set_test_result(new_result); -} - -void z_impl___ztest_set_test_phase(enum ztest_phase new_phase); -static inline void __ztest_set_test_phase(enum ztest_phase new_phase) -{ - z_impl___ztest_set_test_phase(new_phase); -} -#else -__syscall void __ztest_set_test_result(enum ztest_result new_result); -__syscall void __ztest_set_test_phase(enum ztest_phase new_phase); -#endif - -#ifndef ZTEST_UNITTEST -#include -#endif - -/** - * @brief Fails the test if any of the registered tests did not run. - * - * When registering test suites, a pragma function can be provided to determine WHEN the test should - * run. It is possible that a test suite could be registered but the pragma always prevents it from - * running. In cases where a test should make sure that ALL suites ran at least once, this function - * may be called at the end of test_main(). It will cause the test to fail if any suite was - * registered but never ran. - */ -void ztest_verify_all_test_suites_ran(void); - -/** - * @brief Run a test suite. - * - * Internal implementation. Do not call directly. This will run the full test suite along with some - * checks for fast failures and initialization. - * - * @param name The name of the suite to run. - * @return Negative value if the test suite never ran; otherwise, return the number of failures. - */ -int z_ztest_run_test_suite(const char *name); - -/** - * @brief Returns next test within suite. - * - * @param suite Name of suite to get next test from. - * @param prev Previous unit test acquired from suite, use NULL to return first - * unit test. - * @return struct ztest_unit_test* - */ -struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev); - -/* definitions for use with testing application shared memory */ -#ifdef CONFIG_USERSPACE -#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition) -#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition) -#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition) -extern struct k_mem_partition ztest_mem_partition; -#else -#define ZTEST_DMEM -#define ZTEST_BMEM -#define ZTEST_SECTION .data -#endif - -/** - * @defgroup ztest_test Ztest testing macros - * @ingroup ztest - * - * This module eases the testing process by providing helpful macros and other - * testing structures. - * - * @{ - */ - -/** - * @brief Fail the currently running test. - * - * This is the function called from failed assertions and the like. You - * probably don't need to call it yourself. - */ -void ztest_test_fail(void); - -/** - * @brief Pass the currently running test. - * - * Normally a test passes just by returning without an assertion failure. - * However, if the success case for your test involves a fatal fault, - * you can call this function from k_sys_fatal_error_handler to indicate that - * the test passed before aborting the thread. - */ -void ztest_test_pass(void); - -/** - * @brief Skip the current test. - * - */ -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); \ - static void suite##_##fn( \ - COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \ - static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \ - .test_suite_name = STRINGIFY(suite), \ - .name = STRINGIFY(fn), \ - .test = (_##suite##_##fn##_wrapper), \ - .thread_options = t_options, \ - .stats = &z_ztest_unit_test_stats_##suite##_##fn \ - }; \ - static void _##suite##_##fn##_wrapper(void *wrapper_data) \ - { \ - COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \ - (ARG_UNUSED(wrapper_data); suite##_##fn();)) \ - } \ - static inline void suite##_##fn( \ - COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))) - -#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0) -#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1) - -/** - * @brief Skips the test if config is enabled - * - * Use this macro at the start of your test case, to skip it when - * config is enabled. Useful when your test is still under development. - * - * @param config The Kconfig option used to skip the test. - */ -#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ()) - -/** - * @brief Skips the test if config is not enabled - * - * Use this macro at the start of your test case, to skip it when - * config is not enabled. Useful when your need to skip test if some - * conifiguration option is not enabled. - * - * @param config The Kconfig option used to skip the test (if not enabled). - */ -#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip())) - -/** - * @brief Create and register a new unit test. - * - * Calling this macro will create a new unit test and attach it to the declared `suite`. The `suite` - * does not need to be defined in the same compilation unit. - * - * @param suite The name of the test suite to attach this test - * @param fn The test function to call. - */ -#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0) - -/** - * @brief Define a test function that should run as a user thread - * - * This macro behaves exactly the same as ZTEST, but calls the test function in user space if - * `CONFIG_USERSPACE` was enabled. - * - * @param suite The name of the test suite to attach this test - * @param fn The test function to call. - */ -#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER) - -/** - * @brief Define a test function - * - * This macro behaves exactly the same as ZTEST(), but the function takes an argument for the - * fixture of type `struct suite##_fixture*` named `this`. - * - * @param suite The name of the test suite to attach this test - * @param fn The test function to call. - */ -#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0) - -/** - * @brief Define a test function that should run as a user thread - * - * If CONFIG_USERSPACE is not enabled, this is functionally identical to ZTEST_F(). The test - * function takes a single fixture argument of type `struct suite##_fixture*` named `this`. - * - * @param suite The name of the test suite to attach this test - * @param fn The test function to call. - */ -#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER) - -/** - * @brief Test rule callback function signature - * - * The function signature that can be used to register a test rule's before/after callback. This - * provides access to the test and the fixture data (if provided). - * - * @param test Pointer to the unit test in context - * @param data Pointer to the test's fixture data (may be NULL) - */ -typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data); - -/** @private */ -struct ztest_test_rule { - ztest_rule_cb before_each; - ztest_rule_cb after_each; -}; - -/** - * @brief Define a test rule that will run before/after each unit test. - * - * Functions defined here will run before/after each unit test for every test suite. Along with the - * callback, the test functions are provided a pointer to the test being run, and the data. This - * provides a mechanism for tests to perform custom operations depending on the specific test or - * the data (for example logging may use the test's name). - * - * Ordering: - * - Test rule's `before` function will run before the suite's `before` function. This is done to - * allow the test suite's customization to take precedence over the rule which is applied to all - * suites. - * - Test rule's `after` function is not guaranteed to run in any particular order. - * - * @param name The name for the test rule (must be unique within the compilation unit) - * @param before_each_fn The callback function (ztest_rule_cb) to call before each test - * (may be NULL) - * @param after_each_fn The callback function (ztest_rule_cb) to call after each test (may be NULL) - */ -#define ZTEST_RULE(name, before_each_fn, after_each_fn) \ - static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \ - .before_each = (before_each_fn), \ - .after_each = (after_each_fn), \ - } - -extern struct ztest_test_rule _ztest_test_rule_list_start[]; -extern struct ztest_test_rule _ztest_test_rule_list_end[]; - -/** - * @brief A 'before' function to use in test suites that just need to start 1cpu - * - * Ignores data, and calls z_test_1cpu_start() - * - * @param data The test suite's data - */ -void ztest_simple_1cpu_before(void *data); - -/** - * @brief A 'after' function to use in test suites that just need to stop 1cpu - * - * Ignores data, and calls z_test_1cpu_stop() - * - * @param data The test suite's data - */ -void ztest_simple_1cpu_after(void *data); - -/** - * @brief Run the specified test suite. - * - * @param suite Test suite to run. - */ -#define ztest_run_test_suite(suite) z_ztest_run_test_suite(STRINGIFY(suite)) - -/** - * @brief Structure for architecture specific APIs - * - */ -struct ztest_arch_api { - void (*run_all)(const void *state); - bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite); - bool (*should_test_run)(const char *suite, const char *test); -}; - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */ diff --git a/subsys/testsuite/ztest/src/ztest.c b/subsys/testsuite/ztest/src/ztest.c index 04e16b97f927d3c..b100921ab21ac28 100644 --- a/subsys/testsuite/ztest/src/ztest.c +++ b/subsys/testsuite/ztest/src/ztest.c @@ -4,53 +4,65 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - - #include + #include #ifdef CONFIG_USERSPACE #include #endif -#include #include +#include #ifdef KERNEL static struct k_thread ztest_thread; #endif +static bool failed_expectation; -#ifdef CONFIG_ARCH_POSIX -#include +#ifdef CONFIG_ZTEST_SHUFFLE +#include +#include + +#include +#define NUM_ITER_PER_SUITE CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT +#define NUM_ITER_PER_TEST CONFIG_ZTEST_SHUFFLE_TEST_REPEAT_COUNT +#else +#define NUM_ITER_PER_SUITE 1 +#define NUM_ITER_PER_TEST 1 #endif /* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test */ -ZTEST_DMEM enum { - TEST_PHASE_SETUP, - TEST_PHASE_TEST, - TEST_PHASE_TEARDOWN, - TEST_PHASE_FRAMEWORK -} phase = TEST_PHASE_FRAMEWORK; - -static ZTEST_BMEM int test_status; +/** + * @brief The current status of the test binary + */ +enum ztest_status { + ZTEST_STATUS_OK, + ZTEST_STATUS_HAS_FAILURE, + ZTEST_STATUS_CRITICAL_ERROR +}; /** - * @brief Try to shorten a filename by removing the current directory - * - * This helps to reduce the very long filenames in assertion failures. It - * removes the current directory from the filename and returns the rest. - * This makes assertions a lot more readable, and sometimes they fit on one - * line. - * - * @param file Filename to check - * @returns Shortened filename, or @file if it could not be shortened + * @brief Tracks the current phase that ztest is operating in. */ -const char *__weak ztest_relative_filename(const char *file) +ZTEST_DMEM enum ztest_phase cur_phase = TEST_PHASE_FRAMEWORK; + +static ZTEST_BMEM enum ztest_status test_status = ZTEST_STATUS_OK; + +extern ZTEST_DMEM const struct ztest_arch_api ztest_api; + +static void __ztest_show_suite_summary(void); + +static void end_report(void) { - return file; + __ztest_show_suite_summary(); + if (test_status) { + TC_END_REPORT(TC_FAIL); + } else { + TC_END_REPORT(TC_PASS); + } } -static int cleanup_test(struct unit_test *test) +static int cleanup_test(struct ztest_unit_test *test) { int ret = TC_PASS; int mock_status; @@ -68,12 +80,10 @@ static int cleanup_test(struct unit_test *test) #endif if (!ret && mock_status == 1) { - PRINT("Test %s failed: Unused mock parameter values\n", - test->name); + PRINT("Test %s failed: Unused mock parameter values\n", test->name); ret = TC_FAIL; } else if (!ret && mock_status == 2) { - PRINT("Test %s failed: Unused mock return values\n", - test->name); + PRINT("Test %s failed: Unused mock return values\n", test->name); ret = TC_FAIL; } else { ; @@ -89,6 +99,7 @@ static int cleanup_test(struct unit_test *test) #define CPUHOLD_STACK_SZ (512 + CONFIG_TEST_EXTRA_STACK_SIZE) static struct k_thread cpuhold_threads[MAX_NUM_CPUHOLD]; K_KERNEL_STACK_ARRAY_DEFINE(cpuhold_stacks, MAX_NUM_CPUHOLD, CPUHOLD_STACK_SZ); + static struct k_sem cpuhold_sem; volatile int cpuhold_active; @@ -139,23 +150,21 @@ void z_impl_z_test_1cpu_start(void) unsigned int num_cpus = arch_num_cpus(); cpuhold_active = 1; -#ifdef CONFIG_THREAD_NAME char tname[CONFIG_THREAD_MAX_NAME_LEN]; -#endif + k_sem_init(&cpuhold_sem, 0, 999); /* Spawn N-1 threads to "hold" the other CPUs, waiting for * each to signal us that it's locked and spinning. */ - for (int i = 0; i < num_cpus - 1; i++) { - k_thread_create(&cpuhold_threads[i], - cpuhold_stacks[i], CPUHOLD_STACK_SZ, - (k_thread_entry_t) cpu_hold, NULL, NULL, NULL, - K_HIGHEST_THREAD_PRIO, 0, K_NO_WAIT); -#ifdef CONFIG_THREAD_NAME - snprintk(tname, CONFIG_THREAD_MAX_NAME_LEN, "cpuhold%02d", i); - k_thread_name_set(&cpuhold_threads[i], tname); -#endif + for (int i = 0; i < num_cpus - 1; i++) { + k_thread_create(&cpuhold_threads[i], cpuhold_stacks[i], CPUHOLD_STACK_SZ, + (k_thread_entry_t)cpu_hold, NULL, NULL, NULL, K_HIGHEST_THREAD_PRIO, + 0, K_NO_WAIT); + if (IS_ENABLED(CONFIG_THREAD_NAME)) { + snprintk(tname, CONFIG_THREAD_MAX_NAME_LEN, "cpuhold%02d", i); + k_thread_name_set(&cpuhold_threads[i], tname); + } k_sem_take(&cpuhold_sem, K_FOREVER); } #endif @@ -168,36 +177,107 @@ void z_impl_z_test_1cpu_stop(void) cpuhold_active = 0; - /* Note that NUM_CPUHOLD can be a value that causes coverity - * to flag the following loop as DEADCODE so suppress the warning. - */ - for (int i = 0; i < num_cpus - 1; i++) { + for (int i = 0; i < num_cpus - 1; i++) { k_thread_abort(&cpuhold_threads[i]); } #endif } #ifdef CONFIG_USERSPACE -void z_vrfy_z_test_1cpu_start(void) -{ - z_impl_z_test_1cpu_start(); -} +void z_vrfy_z_test_1cpu_start(void) { z_impl_z_test_1cpu_start(); } #include -void z_vrfy_z_test_1cpu_stop(void) -{ - z_impl_z_test_1cpu_stop(); -} +void z_vrfy_z_test_1cpu_stop(void) { z_impl_z_test_1cpu_stop(); } #include #endif /* CONFIG_USERSPACE */ #endif -static void run_test_functions(struct unit_test *test) +__maybe_unused static void run_test_rules(bool is_before, struct ztest_unit_test *test, void *data) { - phase = TEST_PHASE_SETUP; - test->setup(); - phase = TEST_PHASE_TEST; - test->test(); + for (struct ztest_test_rule *rule = _ztest_test_rule_list_start; + rule < _ztest_test_rule_list_end; ++rule) { + if (is_before && rule->before_each) { + rule->before_each(test, data); + } else if (!is_before && rule->after_each) { + rule->after_each(test, data); + } + } +} + +static void run_test_functions(struct ztest_suite_node *suite, struct ztest_unit_test *test, + void *data) +{ + __ztest_set_test_phase(TEST_PHASE_TEST); + test->test(data); +} + +COND_CODE_1(KERNEL, (ZTEST_BMEM), ()) static enum ztest_result test_result; + +static int get_final_test_result(const struct ztest_unit_test *test, int ret) +{ + enum ztest_expected_result expected_result = -1; + + for (struct ztest_expected_result_entry *expectation = + _ztest_expected_result_entry_list_start; + expectation < _ztest_expected_result_entry_list_end; ++expectation) { + if (strcmp(expectation->test_name, test->name) == 0 && + strcmp(expectation->test_suite_name, test->test_suite_name) == 0) { + expected_result = expectation->expected_result; + break; + } + } + + if (expected_result == ZTEST_EXPECTED_RESULT_FAIL) { + /* Expected a failure: + * - If we got a failure, return TC_PASS + * - Otherwise force a failure + */ + return (ret == TC_FAIL) ? TC_PASS : TC_FAIL; + } + if (expected_result == ZTEST_EXPECTED_RESULT_SKIP) { + /* Expected a skip: + * - If we got a skip, return TC_PASS + * - Otherwise force a failure + */ + return (ret == TC_SKIP) ? TC_PASS : TC_FAIL; + } + /* No expectation was made, no change is needed. */ + return ret; +} + +/** + * @brief Get a friendly name string for a given test phrase. + * + * @param phase an enum ztest_phase value describing the desired test phase + * @returns a string name for `phase` + */ +static inline const char *get_friendly_phase_name(enum ztest_phase phase) +{ + switch (phase) { + case TEST_PHASE_SETUP: + return "setup"; + case TEST_PHASE_BEFORE: + return "before"; + case TEST_PHASE_TEST: + return "test"; + case TEST_PHASE_AFTER: + return "after"; + case TEST_PHASE_TEARDOWN: + return "teardown"; + case TEST_PHASE_FRAMEWORK: + return "framework"; + default: + return "(unknown)"; + } +} + +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 @@ -210,78 +290,95 @@ static void run_test_functions(struct unit_test *test) */ #include /* parasoft-suppress MISRAC2012-RULE_21_4-a MISRAC2012-RULE_21_4-b*/ #include -#include #include +#include #define FAIL_FAST 0 static jmp_buf test_fail; -static jmp_buf test_skip; static jmp_buf test_pass; +static jmp_buf test_skip; static jmp_buf stack_fail; +static jmp_buf test_suite_fail; void ztest_test_fail(void) { - raise(SIGABRT); -} - -void ztest_test_skip(void) -{ - longjmp(test_skip, 1); + switch (cur_phase) { + case TEST_PHASE_SETUP: + PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); + longjmp(test_suite_fail, 1); + case TEST_PHASE_BEFORE: + case TEST_PHASE_TEST: + PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); + longjmp(test_fail, 1); + case TEST_PHASE_AFTER: + case TEST_PHASE_TEARDOWN: + case TEST_PHASE_FRAMEWORK: + PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + longjmp(stack_fail, 1); + } } void ztest_test_pass(void) { - longjmp(test_pass, 1); + if (cur_phase == TEST_PHASE_TEST) { + longjmp(test_pass, 1); + } + PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + longjmp(stack_fail, 1); } -static void handle_signal(int sig) +void ztest_test_skip(void) { - static const char *const phase_str[] = { - "setup", - "unit test", - "teardown", - }; - - PRINT(" %s", strsignal(sig)); - switch (phase) { + switch (cur_phase) { case TEST_PHASE_SETUP: + case TEST_PHASE_BEFORE: case TEST_PHASE_TEST: - case TEST_PHASE_TEARDOWN: - PRINT(" at %s function\n", phase_str[phase]); - longjmp(test_fail, 1); - case TEST_PHASE_FRAMEWORK: - PRINT("\n"); + longjmp(test_skip, 1); + default: + PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); longjmp(stack_fail, 1); } } -static void init_testing(void) +void ztest_test_expect_fail(void) { - signal(SIGABRT, handle_signal); - signal(SIGSEGV, handle_signal); + failed_expectation = true; - if (setjmp(stack_fail)) { - PRINT("TESTSUITE crashed."); - exit(1); + switch (cur_phase) { + case TEST_PHASE_SETUP: + PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); + break; + case TEST_PHASE_BEFORE: + case TEST_PHASE_TEST: + PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); + break; + case TEST_PHASE_AFTER: + case TEST_PHASE_TEARDOWN: + case TEST_PHASE_FRAMEWORK: + PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + longjmp(stack_fail, 1); } } -static int run_test(struct unit_test *test) +static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data) { int ret = TC_PASS; - int skip = 0; TC_START(test->name); - get_start_time_cyc(); + __ztest_set_test_phase(TEST_PHASE_BEFORE); - if (setjmp(test_fail)) { + if (test_result == ZTEST_RESULT_SUITE_FAIL) { ret = TC_FAIL; goto out; } - if (setjmp(test_skip)) { - skip = 1; + if (setjmp(test_fail)) { + ret = TC_FAIL; goto out; } @@ -290,15 +387,36 @@ static int run_test(struct unit_test *test) goto out; } - run_test_functions(test); + if (setjmp(test_skip)) { + ret = TC_SKIP; + goto out; + } + + run_test_rules(/*is_before=*/true, test, data); + if (suite->before) { + suite->before(data); + } + run_test_functions(suite, test, data); out: + if (failed_expectation) { + failed_expectation = false; + ret = TC_FAIL; + } + + __ztest_set_test_phase(TEST_PHASE_AFTER); + if (test_result != ZTEST_RESULT_SUITE_FAIL) { + if (suite->after != NULL) { + suite->after(data); + } + run_test_rules(/*is_before=*/false, test, data); + } + __ztest_set_test_phase(TEST_PHASE_FRAMEWORK); ret |= cleanup_test(test); - get_test_duration_ms(); - if (skip) { - Z_TC_END_RESULT(TC_SKIP, test->name); - } else { - Z_TC_END_RESULT(ret, test->name); + 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; @@ -315,9 +433,7 @@ static int run_test(struct unit_test *test) #define FAIL_FAST 0 #endif -K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACK_SIZE + - CONFIG_TEST_EXTRA_STACK_SIZE); -static ZTEST_BMEM int test_result; +K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE); static void test_finalize(void) { @@ -329,91 +445,174 @@ static void test_finalize(void) void ztest_test_fail(void) { - test_result = -1; - test_finalize(); + switch (cur_phase) { + case TEST_PHASE_SETUP: + __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL); + break; + case TEST_PHASE_BEFORE: + case TEST_PHASE_TEST: + __ztest_set_test_result(ZTEST_RESULT_FAIL); + test_finalize(); + break; + default: + PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + test_status = ZTEST_STATUS_CRITICAL_ERROR; + break; + } } void ztest_test_pass(void) { - test_result = 0; - test_finalize(); + switch (cur_phase) { + case TEST_PHASE_TEST: + __ztest_set_test_result(ZTEST_RESULT_PASS); + test_finalize(); + break; + default: + PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + test_status = ZTEST_STATUS_CRITICAL_ERROR; + if (cur_phase == TEST_PHASE_BEFORE) { + test_finalize(); + } + } } void ztest_test_skip(void) { - test_result = -2; - test_finalize(); + switch (cur_phase) { + case TEST_PHASE_SETUP: + __ztest_set_test_result(ZTEST_RESULT_SUITE_SKIP); + break; + case TEST_PHASE_BEFORE: + case TEST_PHASE_TEST: + __ztest_set_test_result(ZTEST_RESULT_SKIP); + test_finalize(); + break; + default: + PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n", + get_friendly_phase_name(cur_phase)); + test_status = ZTEST_STATUS_CRITICAL_ERROR; + break; + } } -static void init_testing(void) +void ztest_test_expect_fail(void) { - k_object_access_all_grant(&ztest_thread); + failed_expectation = true; } -static void test_cb(void *a, void *dummy2, void *dummy) +void ztest_simple_1cpu_before(void *data) { - struct unit_test *test = (struct unit_test *)a; + ARG_UNUSED(data); + z_test_1cpu_start(); +} - ARG_UNUSED(dummy2); - ARG_UNUSED(dummy); +void ztest_simple_1cpu_after(void *data) +{ + ARG_UNUSED(data); + z_test_1cpu_stop(); +} - test_result = 1; - run_test_functions(test); - test_result = 0; +static void test_cb(void *a, void *b, void *c) +{ + struct ztest_suite_node *suite = a; + struct ztest_unit_test *test = b; + const bool config_user_mode = FIELD_GET(K_USER, test->thread_options) != 0; + + if (!IS_ENABLED(CONFIG_USERSPACE) || !k_is_user_context()) { + __ztest_set_test_result(ZTEST_RESULT_PENDING); + run_test_rules(/*is_before=*/true, test, /*data=*/c); + if (suite->before) { + suite->before(/*data=*/c); + } + if (IS_ENABLED(CONFIG_USERSPACE) && config_user_mode) { + k_thread_user_mode_enter(test_cb, a, b, c); + } + } + run_test_functions(suite, test, c); + __ztest_set_test_result(ZTEST_RESULT_PASS); } -static int run_test(struct unit_test *test) +static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data) { int ret = TC_PASS; +#if CONFIG_ZTEST_TEST_DELAY_MS > 0 + k_busy_wait(CONFIG_ZTEST_TEST_DELAY_MS * USEC_PER_MSEC); +#endif TC_START(test->name); - get_start_time_cyc(); + __ztest_set_test_phase(TEST_PHASE_BEFORE); + + /* If the suite's setup function marked us as skipped, don't bother + * running the tests. + */ if (IS_ENABLED(CONFIG_MULTITHREADING)) { + get_start_time_cyc(); k_thread_create(&ztest_thread, ztest_thread_stack, K_THREAD_STACK_SIZEOF(ztest_thread_stack), - (k_thread_entry_t) test_cb, (struct unit_test *)test, - NULL, NULL, CONFIG_ZTEST_THREAD_PRIORITY, - test->thread_options | K_INHERIT_PERMS, - K_FOREVER); + (k_thread_entry_t)test_cb, suite, test, data, + CONFIG_ZTEST_THREAD_PRIORITY, + K_INHERIT_PERMS, K_FOREVER); - k_thread_access_grant(&ztest_thread, test); + k_thread_access_grant(&ztest_thread, suite, test, suite->stats); if (test->name != NULL) { k_thread_name_set(&ztest_thread, test->name); } - k_thread_start(&ztest_thread); - k_thread_join(&ztest_thread, K_FOREVER); + /* Only start the thread if we're not skipping the suite */ + if (test_result != ZTEST_RESULT_SUITE_SKIP && + test_result != ZTEST_RESULT_SUITE_FAIL) { + k_thread_start(&ztest_thread); + k_thread_join(&ztest_thread, K_FOREVER); + } + } else if (test_result != ZTEST_RESULT_SUITE_SKIP && + test_result != ZTEST_RESULT_SUITE_FAIL) { + __ztest_set_test_result(ZTEST_RESULT_PENDING); + get_start_time_cyc(); + run_test_rules(/*is_before=*/true, test, data); + if (suite->before) { + suite->before(data); + } + run_test_functions(suite, test, data); + } - } else { - test_result = 1; - run_test_functions(test); + __ztest_set_test_phase(TEST_PHASE_AFTER); + if (suite->after != NULL) { + suite->after(data); } + run_test_rules(/*is_before=*/false, test, data); + get_test_duration_ms(); + if (tc_spend_time > test->stats->duration_worst_ms) { + test->stats->duration_worst_ms = tc_spend_time; + } - phase = TEST_PHASE_TEARDOWN; - test->teardown(); - phase = TEST_PHASE_FRAMEWORK; + __ztest_set_test_phase(TEST_PHASE_FRAMEWORK); /* Flush all logs in case deferred mode and default logging thread are used. */ while (IS_ENABLED(CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST) && - IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && - log_data_pending()) { + IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && log_data_pending()) { k_msleep(100); } - if (test_result == -1) { + if (test_result == ZTEST_RESULT_FAIL || test_result == ZTEST_RESULT_SUITE_FAIL || + failed_expectation) { ret = TC_FAIL; + failed_expectation = false; + } else if (test_result == ZTEST_RESULT_SKIP || test_result == ZTEST_RESULT_SUITE_SKIP) { + ret = TC_SKIP; } - if (!test_result || !FAIL_FAST) { + if (test_result == ZTEST_RESULT_PASS || !FAIL_FAST) { ret |= cleanup_test(test); } - get_test_duration_ms(); - if (test_result == -2) { - Z_TC_END_RESULT(TC_SKIP, test->name); - } else { - Z_TC_END_RESULT(ret, test->name); + 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; @@ -421,63 +620,330 @@ static int run_test(struct unit_test *test) #endif /* !KERNEL */ -int z_ztest_run_test_suite(const char *name, struct unit_test *suite) +static struct ztest_suite_node *ztest_find_test_suite(const char *name) { + struct ztest_suite_node *node; + + for (node = _ztest_suite_node_list_start; node < _ztest_suite_node_list_end; ++node) { + if (strcmp(name, node->name) == 0) { + return node; + } + } + + return NULL; +} + +struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev) +{ + struct ztest_unit_test *test = (prev == NULL) ? _ztest_unit_test_list_start : prev + 1; + + for (; test < _ztest_unit_test_list_end; ++test) { + if (strcmp(suite, test->test_suite_name) == 0) { + return test; + } + } + return NULL; +} + +#ifdef CONFIG_ZTEST_SHUFFLE +static void z_ztest_shuffle(void *dest[], intptr_t start, size_t num_items, size_t element_size) +{ + void *tmp; + + /* Initialize dest array */ + for (size_t i = 0; i < num_items; ++i) { + dest[i] = (void *)(start + (i * element_size)); + } + + /* Shuffle dest array */ + for (size_t i = num_items - 1; i > 0; i--) { + int j = sys_rand32_get() % (i + 1); + + if (i != j) { + tmp = dest[j]; + dest[j] = dest[i]; + dest[i] = tmp; + } + } +} +#endif /* CONFIG_ZTEST_SHUFFLE */ + +static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite) +{ + struct ztest_unit_test *test = NULL; + void *data = NULL; int fail = 0; + int tc_result = TC_PASS; - if (test_status < 0) { + if (FAIL_FAST && test_status != ZTEST_STATUS_OK) { return test_status; } - init_testing(); + if (suite == NULL) { + test_status = ZTEST_STATUS_CRITICAL_ERROR; + return -1; + } + +#ifndef KERNEL + if (setjmp(stack_fail)) { + PRINT("TESTSUITE crashed.\n"); + test_status = ZTEST_STATUS_CRITICAL_ERROR; + end_report(); + exit(1); + } +#else + k_object_access_all_grant(&ztest_thread); +#endif - TC_SUITE_START(name); - while (suite->test) { - fail += run_test(suite); - suite++; + TC_SUITE_START(suite->name); + current_test_failed_assumption = false; + __ztest_set_test_result(ZTEST_RESULT_PENDING); + __ztest_set_test_phase(TEST_PHASE_SETUP); +#ifndef KERNEL + if (setjmp(test_suite_fail)) { + __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL); + } +#endif + if (test_result != ZTEST_RESULT_SUITE_FAIL && suite->setup != NULL) { + data = suite->setup(); + } - if (fail && FAIL_FAST) { - break; + for (int i = 0; i < NUM_ITER_PER_TEST; i++) { + fail = 0; + +#ifdef CONFIG_ZTEST_SHUFFLE + struct ztest_unit_test *tests_to_run[ZTEST_TEST_COUNT]; + + memset(tests_to_run, 0, ZTEST_TEST_COUNT * sizeof(struct ztest_unit_test *)); + z_ztest_shuffle((void **)tests_to_run, (intptr_t)_ztest_unit_test_list_start, + ZTEST_TEST_COUNT, sizeof(struct ztest_unit_test)); + for (size_t j = 0; j < ZTEST_TEST_COUNT; ++j) { + test = tests_to_run[j]; + /* Make sure that the test belongs to this suite */ + if (strcmp(suite->name, test->test_suite_name) != 0) { + continue; + } + if (ztest_api.should_test_run(suite->name, test->name)) { + test->stats->run_count++; + tc_result = run_test(suite, test, data); + if (tc_result == TC_PASS) { + test->stats->pass_count++; + } else if (tc_result == TC_SKIP) { + test->stats->skip_count++; + } else if (tc_result == TC_FAIL) { + test->stats->fail_count++; + } + if (tc_result == TC_FAIL) { + fail++; + } + } + + if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) { + break; + } + } +#else + while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) { + if (ztest_api.should_test_run(suite->name, test->name)) { + test->stats->run_count++; + tc_result = run_test(suite, test, data); + if (tc_result == TC_PASS) { + test->stats->pass_count++; + } else if (tc_result == TC_SKIP) { + test->stats->skip_count++; + } else if (tc_result == TC_FAIL) { + test->stats->fail_count++; + } + + if (tc_result == TC_FAIL) { + fail++; + } + } + + if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) { + break; + } + } +#endif + + if (test_status == ZTEST_STATUS_OK && fail != 0) { + test_status = ZTEST_STATUS_HAS_FAILURE; } } - TC_SUITE_END(name, (fail > 0 ? TC_FAIL : TC_PASS)); - test_status = (test_status || fail) ? 1 : 0; + TC_SUITE_END(suite->name, (fail > 0 ? TC_FAIL : TC_PASS)); + __ztest_set_test_phase(TEST_PHASE_TEARDOWN); + if (suite->teardown != NULL) { + suite->teardown(data); + } return fail; } -void end_report(void) +int z_ztest_run_test_suite(const char *name) { - if (test_status) { - TC_END_REPORT(TC_FAIL); - } else { - TC_END_REPORT(TC_PASS); - } + return z_ztest_run_test_suite_ptr(ztest_find_test_suite(name)); } #ifdef CONFIG_USERSPACE K_APPMEM_PARTITION_DEFINE(ztest_mem_partition); #endif -int ztest_run_registered_test_suites(const void *state) +static void __ztest_init_unit_test_result_for_suite(struct ztest_suite_node *suite) { - struct ztest_suite_node *ptr; - int count = 0; + struct ztest_unit_test *test = NULL; + + while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) { + test->stats->run_count = 0; + test->stats->skip_count = 0; + test->stats->fail_count = 0; + test->stats->pass_count = 0; + test->stats->duration_worst_ms = 0; + } +} - for (ptr = _ztest_suite_node_list_start; ptr < _ztest_suite_node_list_end; ++ptr) { - struct ztest_suite_stats *stats = ptr->stats; - bool should_run = true; +static void flush_log(void) +{ + if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) { + while (log_data_pending()) { + k_sleep(K_MSEC(10)); + } + k_sleep(K_MSEC(10)); + } else { + while (LOG_PROCESS()) { + } + } +} - if (ptr->predicate != NULL) { - should_run = ptr->predicate(state); - } else { - /* If pragma is NULL, only run this test once. */ - should_run = stats->run_count == 0; +/* Show one line summary for a test suite. + */ +static void __ztest_show_suite_summary_oneline(struct ztest_suite_node *suite) +{ + int distinct_pass = 0, distinct_fail = 0, distinct_skip = 0, distinct_total = 0; + int effective_total = 0; + int expanded_pass = 0, expanded_passrate = 0; + int passrate_major = 0, passrate_minor = 0, passrate_tail = 0; + int suite_result = TC_PASS; + + struct ztest_unit_test *test = NULL; + unsigned int suite_duration_worst_ms = 0; + + /** summary of disctinct run */ + while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) { + distinct_total++; + suite_duration_worst_ms += test->stats->duration_worst_ms; + if (test->stats->skip_count == test->stats->run_count) { + distinct_skip++; + } else if (test->stats->pass_count == test->stats->run_count) { + distinct_pass++; + } else { + distinct_fail++; } + } - if (should_run) { - int fail = z_ztest_run_test_suite(ptr->name, ptr->suite); + if (distinct_skip == distinct_total) { + suite_result = TC_SKIP; + passrate_major = passrate_minor = 0; + } else { + suite_result = (distinct_fail > 0) ? TC_FAIL : TC_PASS; + effective_total = distinct_total - distinct_skip; + expanded_pass = distinct_pass * 100000; + expanded_passrate = expanded_pass / effective_total; + passrate_major = expanded_passrate / 1000; + passrate_minor = (expanded_passrate - passrate_major * 1000) / 10; + passrate_tail = expanded_passrate - passrate_major * 1000 - passrate_minor * 10; + if (passrate_tail >= 5) { /* rounding */ + passrate_minor++; + } + } + + TC_SUMMARY_PRINT("SUITE %s - %3d.%02d%% [%s]: pass = %d, fail = %d, " + "skip = %d, total = %d duration = %u.%03u seconds\n", + TC_RESULT_TO_STR(suite_result), + passrate_major, passrate_minor, + suite->name, distinct_pass, distinct_fail, + distinct_skip, distinct_total, + suite_duration_worst_ms / 1000, suite_duration_worst_ms % 1000); + flush_log(); +} + +static void __ztest_show_suite_summary_verbose(struct ztest_suite_node *suite) +{ + struct ztest_unit_test *test = NULL; + int tc_result = TC_PASS; + int flush_frequency = 0; + + if (IS_ENABLED(CONFIG_ZTEST_VERBOSE_SUMMARY) == 0) { + return; + } + + while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) { + if (test->stats->skip_count == test->stats->run_count) { + tc_result = TC_SKIP; + } else if (test->stats->pass_count == test->stats->run_count) { + tc_result = TC_PASS; + } else if (test->stats->pass_count == 0) { + tc_result = TC_FAIL; + } else { + tc_result = TC_FLAKY; + } + + if (tc_result == TC_FLAKY) { + TC_SUMMARY_PRINT(" - %s - [%s.%s] - (Failed %d of %d attempts)" + " - duration = %u.%03u seconds\n", + TC_RESULT_TO_STR(tc_result), + test->test_suite_name, test->name, + test->stats->run_count - test->stats->pass_count, + test->stats->run_count, + test->stats->duration_worst_ms / 1000, + test->stats->duration_worst_ms % 1000); + } else { + TC_SUMMARY_PRINT(" - %s - [%s.%s] duration = %u.%03u seconds\n", + TC_RESULT_TO_STR(tc_result), + test->test_suite_name, test->name, + test->stats->duration_worst_ms / 1000, + test->stats->duration_worst_ms % 1000); + } + + if (flush_frequency % 3 == 0) { + /** Reduce the flush frequencey a bit to speed up the output */ + flush_log(); + } + flush_frequency++; + } + TC_SUMMARY_PRINT("\n"); + flush_log(); +} + +static void __ztest_show_suite_summary(void) +{ + if (IS_ENABLED(CONFIG_ZTEST_SUMMARY) == 0) { + return; + } + /* Flush the log a lot to ensure that no summary content + * is dropped if it goes through the logging subsystem. + */ + flush_log(); + TC_SUMMARY_PRINT("\n------ TESTSUITE SUMMARY START ------\n\n"); + flush_log(); + for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start; + ptr < _ztest_suite_node_list_end; ++ptr) { + + __ztest_show_suite_summary_oneline(ptr); + __ztest_show_suite_summary_verbose(ptr); + } + TC_SUMMARY_PRINT("------ TESTSUITE SUMMARY END ------\n\n"); + flush_log(); +} + +static int __ztest_run_test_suite(struct ztest_suite_node *ptr, const void *state) +{ + struct ztest_suite_stats *stats = ptr->stats; + int count = 0; + + for (int i = 0; i < NUM_ITER_PER_SUITE; i++) { + if (ztest_api.should_suite_run(state, ptr)) { + int fail = z_ztest_run_test_suite_ptr(ptr); count++; stats->run_count++; @@ -490,27 +956,121 @@ int ztest_run_registered_test_suites(const void *state) return count; } -void ztest_verify_all_registered_test_suites_ran(void) +int z_impl_ztest_run_test_suites(const void *state) +{ + int count = 0; + + if (test_status == ZTEST_STATUS_CRITICAL_ERROR) { + return count; + } + +#ifdef CONFIG_ZTEST_SHUFFLE + struct ztest_suite_node *suites_to_run[ZTEST_SUITE_COUNT]; + + memset(suites_to_run, 0, ZTEST_SUITE_COUNT * sizeof(struct ztest_suite_node *)); + z_ztest_shuffle((void **)suites_to_run, (intptr_t)_ztest_suite_node_list_start, + ZTEST_SUITE_COUNT, sizeof(struct ztest_suite_node)); + for (size_t i = 0; i < ZTEST_SUITE_COUNT; ++i) { + __ztest_init_unit_test_result_for_suite(suites_to_run[i]); + } + for (size_t i = 0; i < ZTEST_SUITE_COUNT; ++i) { + count += __ztest_run_test_suite(suites_to_run[i], state); + /* Stop running tests if we have a critical error or if we have a failure and + * FAIL_FAST was set + */ + if (test_status == ZTEST_STATUS_CRITICAL_ERROR || + (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) { + break; + } + } +#else + for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start; + ptr < _ztest_suite_node_list_end; ++ptr) { + __ztest_init_unit_test_result_for_suite(ptr); + count += __ztest_run_test_suite(ptr, state); + /* Stop running tests if we have a critical error or if we have a failure and + * FAIL_FAST was set + */ + if (test_status == ZTEST_STATUS_CRITICAL_ERROR || + (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) { + break; + } + } +#endif + + return count; +} + +void z_impl___ztest_set_test_result(enum ztest_result new_result) +{ + test_result = new_result; +} + +void z_impl___ztest_set_test_phase(enum ztest_phase new_phase) +{ + cur_phase = new_phase; +} + +#ifdef CONFIG_USERSPACE +void z_vrfy___ztest_set_test_result(enum ztest_result new_result) +{ + z_impl___ztest_set_test_result(new_result); +} +#include + +void z_vrfy___ztest_set_test_phase(enum ztest_phase new_phase) +{ + z_impl___ztest_set_test_phase(new_phase); +} +#include +#endif /* CONFIG_USERSPACE */ + +void ztest_verify_all_test_suites_ran(void) { bool all_tests_run = true; - struct ztest_suite_node *ptr; + struct ztest_suite_node *suite; + struct ztest_unit_test *test; + + if (IS_ENABLED(CONFIG_ZTEST_VERIFY_RUN_ALL)) { + for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end; + ++suite) { + if (suite->stats->run_count < 1) { + PRINT("ERROR: Test suite '%s' did not run.\n", suite->name); + all_tests_run = false; + } + } - for (ptr = _ztest_suite_node_list_start; ptr < _ztest_suite_node_list_end; ++ptr) { - if (ptr->stats->run_count < 1) { - PRINT("ERROR: Test '%s' did not run.\n", ptr->name); - all_tests_run = false; + for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) { + suite = ztest_find_test_suite(test->test_suite_name); + if (suite == NULL) { + PRINT("ERROR: Test '%s' assigned to test suite '%s' which doesn't " + "exist\n", + test->name, test->test_suite_name); + all_tests_run = false; + } + } + + if (!all_tests_run) { + test_status = ZTEST_STATUS_HAS_FAILURE; } } - if (!all_tests_run) { - test_status = 1; + for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) { + if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count != + test->stats->run_count) { + PRINT("Bad stats for %s.%s\n", test->test_suite_name, test->name); + test_status = 1; + } } } +void ztest_run_all(const void *state) { ztest_api.run_all(state); } + void __weak test_main(void) { - ztest_run_registered_test_suites(NULL); - ztest_verify_all_registered_test_suites_ran(); + ztest_run_all(NULL); + + ztest_verify_all_test_suites_ran(); } #ifndef KERNEL @@ -519,45 +1079,41 @@ int main(void) z_init_mock(); test_main(); end_report(); +#ifdef CONFIG_ZTEST_NO_YIELD + /* + * Rather than yielding to idle thread, keep the part awake so debugger can + * still access it, since some SOCs cannot be debugged in low power states. + */ + uint32_t key = irq_lock(); + while (1) { + ; /* Spin */ + } + irq_unlock(key); +#endif return test_status; } #else int main(void) { #ifdef CONFIG_USERSPACE - int ret; - /* Partition containing globals tagged with ZTEST_DMEM and ZTEST_BMEM * macros. Any variables that user code may reference need to be * placed in this partition if no other memory domain configuration * is made. */ - ret = k_mem_domain_add_partition(&k_mem_domain_default, - &ztest_mem_partition); - if (ret != 0) { - PRINT("ERROR: failed to add ztest_mem_partition to mem domain (%d)\n", - ret); - k_oops(); - } + k_mem_domain_add_partition(&k_mem_domain_default, &ztest_mem_partition); #ifdef Z_MALLOC_PARTITION_EXISTS /* Allow access to malloc() memory */ - if (z_malloc_partition.size != 0) { - ret = k_mem_domain_add_partition(&k_mem_domain_default, - &z_malloc_partition); - if (ret != 0) { - PRINT("ERROR: failed to add z_malloc_partition" - " to mem domain (%d)\n", - ret); - k_oops(); - } - } + k_mem_domain_add_partition(&k_mem_domain_default, &z_malloc_partition); #endif #endif /* CONFIG_USERSPACE */ z_init_mock(); test_main(); end_report(); + flush_log(); + LOG_PANIC(); if (IS_ENABLED(CONFIG_ZTEST_RETEST_IF_PASSED)) { static __noinit struct { uint32_t magic; @@ -571,8 +1127,7 @@ int main(void) } state.boots += 1; if (test_status == 0) { - PRINT("Reset board #%u to test again\n", - state.boots); + PRINT("Reset board #%u to test again\n", state.boots); k_msleep(10); sys_reboot(SYS_REBOOT_COLD); } else { diff --git a/subsys/testsuite/ztest/src/ztest_posix.c b/subsys/testsuite/ztest/src/ztest_posix.c index fbfb3ed41377105..d2c1857c04cd449 100644 --- a/subsys/testsuite/ztest/src/ztest_posix.c +++ b/subsys/testsuite/ztest/src/ztest_posix.c @@ -9,7 +9,7 @@ #include "cmdline.h" /* native_posix command line options header */ #include "soc.h" #include -#include +#include #include "nsi_host_trampolines.h" static const char *test_args; @@ -47,7 +47,7 @@ NATIVE_TASK(add_test_filter_option, PRE_BOOT_1, 10); * This makes assertions a lot more readable, and sometimes they fit on one * line. * - * Overrides implementation in ztest_new.c + * Overrides implementation in ztest.c * * @param file Filename to check * @returns Shortened filename, or @file if it could not be shortened @@ -176,7 +176,7 @@ static bool z_ztest_testargs_contains(const char *suite_name, const char *test_n * @brief Determines if the test case should run based on test cases listed * in the command line argument. * - * Overrides implementation in ztest_new.c + * Overrides implementation in ztest.c * * @param suite - name of test suite * @param test - name of unit test @@ -197,7 +197,7 @@ bool z_ztest_should_test_run(const char *suite, const char *test) * @brief Determines if the test suite should run based on test cases listed * in the command line argument. * - * Overrides implementation in ztest_new.c + * Overrides implementation in ztest.c * * @param state The current state of the machine as it relates to the test * executable. diff --git a/tests/bluetooth/hci_uart_async/src/test_hci_uart_async.c b/tests/bluetooth/hci_uart_async/src/test_hci_uart_async.c index 1996aba177ac34f..08ea8bc1a79bc02 100644 --- a/tests/bluetooth/hci_uart_async/src/test_hci_uart_async.c +++ b/tests/bluetooth/hci_uart_async/src/test_hci_uart_async.c @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include diff --git a/tests/kernel/spinlock/src/spinlock_error_case.c b/tests/kernel/spinlock/src/spinlock_error_case.c index f40bc86f89d30c8..b69e12a0d741c10 100644 --- a/tests/kernel/spinlock/src/spinlock_error_case.c +++ b/tests/kernel/spinlock/src/spinlock_error_case.c @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include #include diff --git a/tests/subsys/emul/src/main.c b/tests/subsys/emul/src/main.c index 7c18b6000f86f43..6ac1091827dbfb1 100644 --- a/tests/subsys/emul/src/main.c +++ b/tests/subsys/emul/src/main.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #define TEST_ACCEL DT_NODELABEL(test_bmi)