diff --git a/subsys/testsuite/Kconfig b/subsys/testsuite/Kconfig index 235e74060a5..9e50a1c4203 100644 --- a/subsys/testsuite/Kconfig +++ b/subsys/testsuite/Kconfig @@ -87,6 +87,11 @@ config TEST_LOGGING_DEFAULTS logging configuration, or no logging at all, disable this in the project-level defconfig. +config TEST_LOGGING_FLUSH_AFTER_TEST + bool "When enabled logs are flushed after each test case" + default y + depends on MULTITHREADING + config TEST_ENABLE_USERSPACE bool depends on TEST_USERSPACE diff --git a/subsys/testsuite/ztest/src/ztest.c b/subsys/testsuite/ztest/src/ztest.c index 2bd75e19794..5c0a2c9b2ff 100644 --- a/subsys/testsuite/ztest/src/ztest.c +++ b/subsys/testsuite/ztest/src/ztest.c @@ -11,6 +11,7 @@ #include #endif #include +#include #ifdef KERNEL static struct k_thread ztest_thread; @@ -368,15 +369,22 @@ static int run_test(struct unit_test *test) } k_thread_start(&ztest_thread); k_thread_join(&ztest_thread, K_FOREVER); + } else { test_result = 1; run_test_functions(test); } + phase = TEST_PHASE_TEARDOWN; test->teardown(); phase = TEST_PHASE_FRAMEWORK; + /* Flush all logs in case deferred mode is used. */ + while (IS_ENABLED(CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST) && log_data_pending()) { + k_msleep(100); + } + if (test_result == -1) { ret = TC_FAIL; }