13#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
14#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
32#if defined(CONFIG_USERSPACE)
33#define __USERSPACE_FLAGS (K_USER)
35#define __USERSPACE_FLAGS (0)
68#define __ZTEST_EXPECT(_suite_name, _test_name, expectation) \
69 static const STRUCT_SECTION_ITERABLE( \
70 ztest_expected_result_entry, \
71 UTIL_CAT(UTIL_CAT(z_ztest_expected_result_, _suite_name), _test_name)) = { \
72 .test_suite_name = STRINGIFY(_suite_name), \
73 .test_name = STRINGIFY(_test_name), \
74 .expected_result = expectation, \
90#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
91 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
106#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
107 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
122#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
154typedef void *(*ztest_suite_setup_t)(void);
217#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
233#define ZTEST_SUITE(SUITE_NAME, PREDICATE, setup_fn, before_fn, after_fn, teardown_fn) \
234 struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME); \
235 static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \
236 UTIL_CAT(z_ztest_test_node_, SUITE_NAME)) = { \
237 .name = STRINGIFY(SUITE_NAME), \
238 .setup = (setup_fn), \
239 .before = (before_fn), \
240 .after = (after_fn), \
241 .teardown = (teardown_fn), \
242 .predicate = PREDICATE, \
243 .stats = &UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME), \
292int z_impl_ztest_run_test_suites(
const void *
state,
bool shuffle,
293 int suite_iter,
int case_iter);
296 int suite_iter,
int case_iter)
298 return z_impl_ztest_run_test_suites(
state, shuffle, suite_iter, case_iter);
303 int suite_iter,
int case_iter);
307void z_impl___ztest_set_test_result(
enum ztest_result new_result);
308static inline void __ztest_set_test_result(
enum ztest_result new_result)
310 z_impl___ztest_set_test_result(new_result);
313void z_impl___ztest_set_test_phase(
enum ztest_phase new_phase);
314static inline void __ztest_set_test_phase(
enum ztest_phase new_phase)
316 z_impl___ztest_set_test_phase(new_phase);
319__syscall
void __ztest_set_test_result(
enum ztest_result new_result);
320__syscall
void __ztest_set_test_phase(
enum ztest_phase new_phase);
347int z_ztest_run_test_suite(
const char *
name,
bool shuffle,
int suite_iter,
int case_iter);
360#ifdef CONFIG_USERSPACE
364#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
368#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
372#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
377#define ZTEST_SECTION .data
407#define Z_TEST(suite, fn, t_options, use_fixture) \
408 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
409 static void _##suite##_##fn##_wrapper(void *data); \
410 static void suite##_##fn( \
411 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \
412 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
413 .test_suite_name = STRINGIFY(suite), \
414 .name = STRINGIFY(fn), \
415 .test = (_##suite##_##fn##_wrapper), \
416 .thread_options = t_options, \
417 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
419 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
421 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
422 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
424 static inline void suite##_##fn( \
425 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
427#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0)
428#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1)
438#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
449#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
460#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
471#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
482#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
493#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
507struct ztest_test_rule {
531#define ZTEST_RULE(name, before_each_fn, after_each_fn) \
532 static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \
533 .before_each = (before_each_fn), \
534 .after_each = (after_each_fn), \
537extern struct ztest_test_rule _ztest_test_rule_list_start[];
538extern struct ztest_test_rule _ztest_test_rule_list_end[];
566#define ztest_run_test_suite(suite, shuffle, suite_iter, case_iter) \
567 z_ztest_run_test_suite(STRINGIFY(suite), shuffle, suite_iter, case_iter)
574 void (*
run_all)(
const void *
state,
bool shuffle,
int suite_iter,
int case_iter);
583__syscall
void z_test_1cpu_start(
void);
584__syscall
void z_test_1cpu_stop(
void);
592#ifndef ZTEST_UNITTEST
593#include <zephyr/syscalls/ztest_test.h>
void *(* ztest_suite_setup_t)(void)
Setup function to run before running this suite.
Definition: ztest_test.h:154
void ztest_test_pass(void)
Pass the currently running test.
void(* ztest_suite_after_t)(void *fixture)
Function to run after each test in this suite.
Definition: ztest_test.h:168
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:504
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:272
void(* ztest_suite_teardown_t)(void *fixture)
Teardown function to run after running this suite.
Definition: ztest_test.h:175
void ztest_simple_1cpu_after(void *data)
A 'after' function to use in test suites that just need to stop 1cpu.
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:259
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_simple_1cpu_before(void *data)
A 'before' function to use in test suites that just need to start 1cpu.
void(* ztest_suite_before_t)(void *fixture)
Function to run before each test in this suite.
Definition: ztest_test.h:161
void ztest_test_fail(void)
Fail the currently running test.
bool(* ztest_suite_predicate_t)(const void *global_state)
An optional predicate function to determine if the test should run.
Definition: ztest_test.h:184
void ztest_test_skip(void)
Skip the current test.
ztest_expected_result
The expected result of a test.
Definition: ztest_test.h:48
@ TEST_PHASE_TEST
Definition: ztest_test.h:275
@ TEST_PHASE_AFTER
Definition: ztest_test.h:276
@ TEST_PHASE_BEFORE
Definition: ztest_test.h:274
@ TEST_PHASE_SETUP
Definition: ztest_test.h:273
@ TEST_PHASE_TEARDOWN
Definition: ztest_test.h:277
@ TEST_PHASE_FRAMEWORK
Definition: ztest_test.h:278
@ ZTEST_RESULT_SKIP
Definition: ztest_test.h:263
@ ZTEST_RESULT_PENDING
Definition: ztest_test.h:260
@ ZTEST_RESULT_PASS
Definition: ztest_test.h:261
@ ZTEST_RESULT_FAIL
Definition: ztest_test.h:262
@ ZTEST_RESULT_SUITE_FAIL
Definition: ztest_test.h:265
@ ZTEST_RESULT_SUITE_SKIP
Definition: ztest_test.h:264
@ ZTEST_EXPECTED_RESULT_FAIL
Expect a test to fail.
Definition: ztest_test.h:49
@ ZTEST_EXPECTED_RESULT_SKIP
Expect a test to pass.
Definition: ztest_test.h:50
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:573
void(* run_all)(const void *state, bool shuffle, int suite_iter, int case_iter)
Definition: ztest_test.h:574
bool(* should_suite_run)(const void *state, struct ztest_suite_node *suite)
Definition: ztest_test.h:575
bool(* should_test_run)(const char *suite, const char *test)
Definition: ztest_test.h:576
A single expectation entry allowing tests to fail/skip and be considered passing.
Definition: ztest_test.h:59
const char * test_suite_name
The test suite's name for the expectation.
Definition: ztest_test.h:60
enum ztest_expected_result expected_result
The expectation.
Definition: ztest_test.h:62
const char * test_name
The test's name for the expectation.
Definition: ztest_test.h:61
A single node of test suite.
Definition: ztest_test.h:190
const ztest_suite_before_t before
Before function.
Definition: ztest_test.h:198
const ztest_suite_setup_t setup
Setup function.
Definition: ztest_test.h:195
struct ztest_suite_stats *const stats
Stats.
Definition: ztest_test.h:210
const ztest_suite_after_t after
After function.
Definition: ztest_test.h:201
const char *const name
The name of the test suite.
Definition: ztest_test.h:192
const ztest_suite_teardown_t teardown
Teardown function.
Definition: ztest_test.h:204
const ztest_suite_predicate_t predicate
Optional predicate filter.
Definition: ztest_test.h:207
Stats about a ztest suite.
Definition: ztest_test.h:127
uint32_t skip_count
The number of times that the suite was skipped.
Definition: ztest_test.h:131
uint32_t fail_count
The number of times that the suite failed.
Definition: ztest_test.h:133
uint32_t run_count
The number of times that the suite ran.
Definition: ztest_test.h:129
Definition: ztest_test.h:136
uint32_t run_count
The number of times that the test ran.
Definition: ztest_test.h:138
uint32_t pass_count
The number of times that the test passed.
Definition: ztest_test.h:144
uint32_t skip_count
The number of times that the test was skipped.
Definition: ztest_test.h:140
uint32_t duration_worst_ms
The longest duration of the test across multiple times.
Definition: ztest_test.h:146
uint32_t fail_count
The number of times that the test failed.
Definition: ztest_test.h:142
Definition: ztest_test.h:109
const char * name
Definition: ztest_test.h:111
struct ztest_unit_test_stats *const stats
Stats.
Definition: ztest_test.h:116
void(* test)(void *data)
Definition: ztest_test.h:112
uint32_t thread_options
Definition: ztest_test.h:113
const char * test_suite_name
Definition: ztest_test.h:110
void sys_clock_tick_set(uint64_t tick)