13#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
14#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
21#if defined(CONFIG_USERSPACE)
22#define __USERSPACE_FLAGS (K_USER)
24#define __USERSPACE_FLAGS (0)
57#define __ZTEST_EXPECT(_suite_name, _test_name, expectation) \
58 static const STRUCT_SECTION_ITERABLE( \
59 ztest_expected_result_entry, \
60 UTIL_CAT(UTIL_CAT(z_ztest_expected_result_, _suite_name), _test_name)) = { \
61 .test_suite_name = STRINGIFY(_suite_name), \
62 .test_name = STRINGIFY(_test_name), \
63 .expected_result = expectation, \
79#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
80 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
95#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
96 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
110#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
142typedef void *(*ztest_suite_setup_t)(void);
203#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
219#define ZTEST_SUITE(SUITE_NAME, PREDICATE, setup_fn, before_fn, after_fn, teardown_fn) \
220 struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME); \
221 static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \
222 UTIL_CAT(z_ztest_test_node_, SUITE_NAME)) = { \
223 .name = STRINGIFY(SUITE_NAME), \
224 .setup = (setup_fn), \
225 .before = (before_fn), \
226 .after = (after_fn), \
227 .teardown = (teardown_fn), \
228 .predicate = PREDICATE, \
229 .stats = &UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME), \
278int z_impl_ztest_run_test_suites(
const void *
state,
bool shuffle,
279 int suite_iter,
int case_iter);
282 int suite_iter,
int case_iter)
284 return z_impl_ztest_run_test_suites(
state, shuffle, suite_iter, case_iter);
289 int suite_iter,
int case_iter);
293void z_impl___ztest_set_test_result(
enum ztest_result new_result);
294static inline void __ztest_set_test_result(
enum ztest_result new_result)
296 z_impl___ztest_set_test_result(new_result);
299void z_impl___ztest_set_test_phase(
enum ztest_phase new_phase);
300static inline void __ztest_set_test_phase(
enum ztest_phase new_phase)
302 z_impl___ztest_set_test_phase(new_phase);
305__syscall
void __ztest_set_test_result(
enum ztest_result new_result);
306__syscall
void __ztest_set_test_phase(
enum ztest_phase new_phase);
333int z_ztest_run_test_suite(
const char *
name,
bool shuffle,
int suite_iter,
int case_iter);
346#ifdef CONFIG_USERSPACE
347#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
348#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
349#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
354#define ZTEST_SECTION .data
394#define Z_TEST(suite, fn, t_options, use_fixture) \
395 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
396 static void _##suite##_##fn##_wrapper(void *data); \
397 static void suite##_##fn( \
398 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \
399 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
400 .test_suite_name = STRINGIFY(suite), \
401 .name = STRINGIFY(fn), \
402 .test = (_##suite##_##fn##_wrapper), \
403 .thread_options = t_options, \
404 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
406 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
408 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
409 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
411 static inline void suite##_##fn( \
412 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
414#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0)
415#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1)
425#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
436#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
447#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
458#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
469#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
480#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
494struct ztest_test_rule {
518#define ZTEST_RULE(name, before_each_fn, after_each_fn) \
519 static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \
520 .before_each = (before_each_fn), \
521 .after_each = (after_each_fn), \
524extern struct ztest_test_rule _ztest_test_rule_list_start[];
525extern struct ztest_test_rule _ztest_test_rule_list_end[];
553#define ztest_run_test_suite(suite, shuffle, suite_iter, case_iter) \
554 z_ztest_run_test_suite(STRINGIFY(suite), shuffle, suite_iter, case_iter)
561 void (*
run_all)(
const void *
state,
bool shuffle,
int suite_iter,
int case_iter);
570__syscall
void z_test_1cpu_start(
void);
571__syscall
void z_test_1cpu_stop(
void);
579#ifndef ZTEST_UNITTEST
580#include <syscalls/ztest_test.h>
void ztest_test_pass(void)
Pass the currently running test.
void ztest_skip_failed_assumption(void)
void(* ztest_rule_cb)(const struct ztest_unit_test *test, void *data)
Test rule callback function signature.
Definition: ztest_test.h:491
void ztest_simple_1cpu_after(void *data)
A 'after' function to use in test suites that just need to stop 1cpu.
void ztest_simple_1cpu_before(void *data)
A 'before' function to use in test suites that just need to start 1cpu.
void ztest_test_fail(void)
Fail the currently running test.
void ztest_test_skip(void)
Skip the current test.
state
Definition: parser_state.h:29
#define bool
Definition: stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
Memory Partition.
Definition: mem_domain.h:55
Structure for architecture specific APIs.
Definition: ztest_test.h:560
void(* run_all)(const void *state, bool shuffle, int suite_iter, int case_iter)
Definition: ztest_test.h:561
bool(* should_suite_run)(const void *state, struct ztest_suite_node *suite)
Definition: ztest_test.h:562
bool(* should_test_run)(const char *suite, const char *test)
Definition: ztest_test.h:563
A single expectation entry allowing tests to fail/skip and be considered passing.
Definition: ztest_test.h:48
const char * test_suite_name
The test suite's name for the expectation.
Definition: ztest_test.h:49
enum ztest_expected_result expected_result
The expectation.
Definition: ztest_test.h:51
const char * test_name
The test's name for the expectation.
Definition: ztest_test.h:50
A single node of test suite.
Definition: ztest_test.h:178
const ztest_suite_before_t before
Before function.
Definition: ztest_test.h:186
const ztest_suite_setup_t setup
Setup function.
Definition: ztest_test.h:183
struct ztest_suite_stats *const stats
Stats.
Definition: ztest_test.h:198
const ztest_suite_after_t after
After function.
Definition: ztest_test.h:189
const char *const name
The name of the test suite.
Definition: ztest_test.h:180
const ztest_suite_teardown_t teardown
Teardown function.
Definition: ztest_test.h:192
const ztest_suite_predicate_t predicate
Optional predicate filter.
Definition: ztest_test.h:195
Stats about a ztest suite.
Definition: ztest_test.h:115
uint32_t skip_count
The number of times that the suite was skipped.
Definition: ztest_test.h:119
uint32_t fail_count
The number of times that the suite failed.
Definition: ztest_test.h:121
uint32_t run_count
The number of times that the suite ran.
Definition: ztest_test.h:117
Definition: ztest_test.h:124
uint32_t run_count
The number of times that the test ran.
Definition: ztest_test.h:126
uint32_t pass_count
The number of times that the test passed.
Definition: ztest_test.h:132
uint32_t skip_count
The number of times that the test was skipped.
Definition: ztest_test.h:128
uint32_t duration_worst_ms
The longest duration of the test across multiple times.
Definition: ztest_test.h:134
uint32_t fail_count
The number of times that the test failed.
Definition: ztest_test.h:130
Definition: ztest_test.h:98
const char * name
Definition: ztest_test.h:100
struct ztest_unit_test_stats *const stats
Stats.
Definition: ztest_test.h:105
void(* test)(void *data)
Definition: ztest_test.h:101
uint32_t thread_options
Definition: ztest_test.h:102
const char * test_suite_name
Definition: ztest_test.h:99
void *(* ztest_suite_setup_t)(void)
Setup function to run before running this suite.
Definition: ztest_test.h:142
void(* ztest_suite_after_t)(void *fixture)
Function to run after each test in this suite.
Definition: ztest_test.h:156
void ztest_verify_all_test_suites_ran(void)
Fails the test if any of the registered tests did not run.
struct k_mem_partition ztest_mem_partition
ztest_phase
Each enum member represents a distinct phase of execution for the test binary.
Definition: ztest_test.h:258
@ TEST_PHASE_TEST
Definition: ztest_test.h:261
@ TEST_PHASE_AFTER
Definition: ztest_test.h:262
@ TEST_PHASE_BEFORE
Definition: ztest_test.h:260
@ TEST_PHASE_SETUP
Definition: ztest_test.h:259
@ TEST_PHASE_TEARDOWN
Definition: ztest_test.h:263
@ TEST_PHASE_FRAMEWORK
Definition: ztest_test.h:264
void(* ztest_suite_teardown_t)(void *fixture)
Teardown function to run after running this suite.
Definition: ztest_test.h:163
void sys_clock_tick_set(uint64_t tick)
int ztest_run_test_suites(const void *state, bool shuffle, int suite_iter, int case_iter)
Run the registered unit tests which return true from their predicate function.
ztest_result
The result of the current running test.
Definition: ztest_test.h:245
@ ZTEST_RESULT_SKIP
Definition: ztest_test.h:249
@ ZTEST_RESULT_PENDING
Definition: ztest_test.h:246
@ ZTEST_RESULT_PASS
Definition: ztest_test.h:247
@ ZTEST_RESULT_FAIL
Definition: ztest_test.h:248
@ ZTEST_RESULT_SUITE_FAIL
Definition: ztest_test.h:251
@ ZTEST_RESULT_SUITE_SKIP
Definition: ztest_test.h:250
void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter)
Default entry point for running or listing registered unit tests.
void(* ztest_suite_before_t)(void *fixture)
Function to run before each test in this suite.
Definition: ztest_test.h:149
bool(* ztest_suite_predicate_t)(const void *global_state)
An optional predicate function to determine if the test should run.
Definition: ztest_test.h:172
ztest_expected_result
The expected result of a test.
Definition: ztest_test.h:37
@ ZTEST_EXPECTED_RESULT_FAIL
Expect a test to fail.
Definition: ztest_test.h:38
@ ZTEST_EXPECTED_RESULT_SKIP
Expect a test to pass.
Definition: ztest_test.h:39