unittest: support ztest_new_api

Add support for the new Ztest API when using unittest.

Fixes #47420

Signed-off-by: Yuval Peress <peress@google.com>
This commit is contained in:
Yuval Peress 2022-07-06 12:16:21 -06:00 committed by Carles Cufí
commit 10feb693f5
6 changed files with 69 additions and 24 deletions

View file

@ -88,10 +88,20 @@ if(LIBS)
message(FATAL_ERROR "This variable is not supported, see SOURCES instead")
endif()
target_sources(testbinary PRIVATE
${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest.c
${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c
)
if(CONFIG_ZTEST_NEW_API)
add_definitions( -DCONFIG_ZTEST_NEW_API=y )
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_compile_definitions(testbinary PRIVATE ZTEST_UNITTEST)

View file

@ -165,7 +165,7 @@ static inline void test_time_ms(void)
#ifndef TC_SUITE_END
#define TC_SUITE_END(name, result) \
do { \
if (result == TC_PASS) { \
if (result != TC_FAIL) { \
TC_PRINT("TESTSUITE %s succeeded\n", name); \
} else { \
TC_PRINT("TESTSUITE %s failed.\n", name); \

View file

@ -136,9 +136,18 @@ void ztest_run_all(const void *state);
* @param state The current state of the machine as it relates to the test executable.
* @return The number of tests that ran.
*/
__syscall int ztest_run_test_suites(const void *state);
#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);
#include <syscalls/ztest_test_new.h>
#endif
/**
* @brief Fails the test if any of the registered tests did not run.
@ -390,6 +399,4 @@ struct ztest_arch_api {
}
#endif
#include <syscalls/ztest_test_new.h>
#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "ztest_test_new.h"
#include <ztest.h>
/**
* @brief Try to shorten a filename by removing the current directory

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/app_memory/app_memdomain.h>
#include <ztest.h>
#include <zephyr/app_memory/app_memdomain.h>
#ifdef CONFIG_USERSPACE
#include <zephyr/sys/libc-hooks.h>
#endif
@ -186,7 +186,7 @@ void z_vrfy_z_test_1cpu_stop(void) { z_impl_z_test_1cpu_stop(); }
#endif /* CONFIG_USERSPACE */
#endif
static void run_test_rules(bool is_before, struct ztest_unit_test *test, void *data)
__maybe_unused static void run_test_rules(bool is_before, struct ztest_unit_test *test, void *data)
{
for (struct ztest_test_rule *rule = _ztest_test_rule_list_start;
rule < _ztest_test_rule_list_end; ++rule) {
@ -205,6 +205,15 @@ static void run_test_functions(struct ztest_suite_node *suite, struct ztest_unit
test->test(data);
}
enum ztest_result {
ZTEST_RESULT_PENDING,
ZTEST_RESULT_PASS,
ZTEST_RESULT_FAIL,
ZTEST_RESULT_SKIP,
ZTEST_RESULT_SUITE_SKIP,
};
COND_CODE_1(KERNEL, (ZTEST_BMEM), ()) static enum ztest_result test_result;
#ifndef KERNEL
/* Static code analysis tool can raise a violation that the standard header
@ -222,12 +231,15 @@ static void run_test_functions(struct ztest_suite_node *suite, struct ztest_unit
static jmp_buf test_fail;
static jmp_buf test_pass;
static jmp_buf test_skip;
static jmp_buf stack_fail;
void ztest_test_fail(void) { raise(SIGABRT); }
void ztest_test_pass(void) { longjmp(test_pass, 1); }
void ztest_test_skip(void) { longjmp(test_skip, 1); }
/**
* @brief Get a friendly name string for a given test phrase.
*
@ -298,9 +310,22 @@ static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test
goto out;
}
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:
ret |= cleanup_test(test);
if (suite->after != NULL) {
suite->after(data);
}
run_test_rules(/*is_before=*/false, test, data);
Z_TC_END_RESULT(ret, test->name);
return ret;
@ -319,15 +344,6 @@ out:
K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE);
enum ztest_result {
ZTEST_RESULT_PENDING,
ZTEST_RESULT_PASS,
ZTEST_RESULT_FAIL,
ZTEST_RESULT_SKIP,
ZTEST_RESULT_SUITE_SKIP,
};
static ZTEST_BMEM enum ztest_result test_result;
static void test_finalize(void)
{
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
@ -536,7 +552,9 @@ static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite)
continue;
}
if (ztest_api.should_test_run(suite->name, test->name)) {
fail += run_test(suite, test, data);
if (run_test(suite, test, data) == TC_FAIL) {
fail++;
}
}
if (fail && FAIL_FAST) {
@ -546,7 +564,9 @@ static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite)
#else
while (((test = z_ztest_get_next_test(suite->name, test)) != NULL)) {
if (ztest_api.should_test_run(suite->name, test->name)) {
fail += run_test(suite, test, data);
if (run_test(suite, test, data) == TC_FAIL) {
fail++;
}
}
if (fail && FAIL_FAST) {

View file

@ -2,7 +2,15 @@
cmake_minimum_required(VERSION 3.20.0)
if(BOARD STREQUAL unit_testing)
list(APPEND SOURCES src/main_deprecated.c)
# Use the new ztest API
set(CONFIG_ZTEST_NEW_API TRUE CACHE BOOL "" FORCE)
# Add the sources
list(APPEND SOURCES src/main.c)
# This test suite depends on having this CONFIG_ value, so define it
add_definitions( -DCONFIG_BUGxxxxx )
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(base)