From 7d78889aa9586023dfc0ff25fc4a0fd2235a42a7 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Fri, 20 Feb 2026 09:15:55 -0600 Subject: [PATCH] Chore/unit-test-cmake-and-ztest-time-duration (#1240) * Fixed cmake for unit test so that indivisual build and test works from vscode * Fixed the time duration in the ztest used for unit testing. --- test/CMakeLists.txt | 4 ++-- test/ztest/include/zephyr/tc_util.h | 22 ++++++++-------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 99537e07..61649aea 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -268,8 +268,8 @@ foreach(testdir IN ITEMS ${testdirs}) --target test_${basename} ) add_test(test_${basename} ${testdir}/test_${basename}) - set_tests_properties(test_${basename} PROPERTIES FIXTURES_REQUIRED test_fixture) - set_tests_properties(build_${basename} PROPERTIES FIXTURES_SETUP test_fixture) + set_tests_properties(test_${basename} PROPERTIES FIXTURES_REQUIRED fixture_${basename}) + set_tests_properties(build_${basename} PROPERTIES FIXTURES_SETUP fixture_${basename}) endforeach() message(STATUS "BACNET: using cmake:....................\"${CMAKE_VERSION}\"") diff --git a/test/ztest/include/zephyr/tc_util.h b/test/ztest/include/zephyr/tc_util.h index 451e9637..86dc5da1 100644 --- a/test/ztest/include/zephyr/tc_util.h +++ b/test/ztest/include/zephyr/tc_util.h @@ -12,6 +12,7 @@ #include #include +#include #ifdef CONFIG_SHELL #include #endif @@ -92,9 +93,7 @@ static inline const char *TC_RESULT_TO_STR(int result) } } -#if 0 static uint32_t tc_start_time; -#endif static uint32_t tc_spend_time; static inline void get_start_time_cyc(void) @@ -103,22 +102,17 @@ static inline void get_start_time_cyc(void) * TC_START() in their code. But the caller thread cannot be * in userspace. */ -#if 0 - if (!k_is_user_context()) { - tc_start_time = k_cycle_get_32(); - } -#endif + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + tc_start_time = (uint32_t)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000); } static inline void test_time_ms(void) { -#if 0 - uint32_t spend_cycle = k_cycle_get_32() - tc_start_time; - - tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle); -#else - tc_spend_time = 0; -#endif + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + uint32_t now = (uint32_t)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000); + tc_spend_time = now - tc_start_time; } #ifndef TC_ERROR