Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ztest_test.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
14#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
15
17#include <zephyr/init.h>
19#include <stdbool.h>
20
21#if defined(CONFIG_USERSPACE)
22#define __USERSPACE_FLAGS (K_USER)
23#else
24#define __USERSPACE_FLAGS (0)
25#endif
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
40};
41
49 const char *test_suite_name;
50 const char *test_name;
52};
53
54extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[];
55extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_end[];
56
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, \
64 }
65
79#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
80 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
81
95#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
96 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
97
99 const char *test_suite_name;
100 const char *name;
101 void (*test)(void *data);
103
106};
107
108extern struct ztest_unit_test _ztest_unit_test_list_start[];
109extern struct ztest_unit_test _ztest_unit_test_list_end[];
110#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
111
122};
123
135};
136
142typedef void *(*ztest_suite_setup_t)(void);
143
149typedef void (*ztest_suite_before_t)(void *fixture);
150
156typedef void (*ztest_suite_after_t)(void *fixture);
157
163typedef void (*ztest_suite_teardown_t)(void *fixture);
164
172typedef bool (*ztest_suite_predicate_t)(const void *global_state);
173
180 const char *const name;
181
184
187
190
193
196
199};
200
201extern struct ztest_suite_node _ztest_suite_node_list_start[];
202extern struct ztest_suite_node _ztest_suite_node_list_end[];
203#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
204
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), \
230 }
239void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter);
240
252};
265};
266
277#ifdef ZTEST_UNITTEST
278int z_impl_ztest_run_test_suites(const void *state, bool shuffle,
279 int suite_iter, int case_iter);
280
281static inline int ztest_run_test_suites(const void *state, bool shuffle,
282 int suite_iter, int case_iter)
283{
284 return z_impl_ztest_run_test_suites(state, shuffle, suite_iter, case_iter);
285}
286
287#else
288__syscall int ztest_run_test_suites(const void *state, bool shuffle,
289 int suite_iter, int case_iter);
290#endif
291
292#ifdef ZTEST_UNITTEST
293void z_impl___ztest_set_test_result(enum ztest_result new_result);
294static inline void __ztest_set_test_result(enum ztest_result new_result)
295{
296 z_impl___ztest_set_test_result(new_result);
297}
298
299void z_impl___ztest_set_test_phase(enum ztest_phase new_phase);
300static inline void __ztest_set_test_phase(enum ztest_phase new_phase)
301{
302 z_impl___ztest_set_test_phase(new_phase);
303}
304#else
305__syscall void __ztest_set_test_result(enum ztest_result new_result);
306__syscall void __ztest_set_test_phase(enum ztest_phase new_phase);
307#endif
308
309
320
333int z_ztest_run_test_suite(const char *name, bool shuffle, int suite_iter, int case_iter);
334
343struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev);
344
345/* definitions for use with testing application shared memory */
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)
351#else
352#define ZTEST_DMEM
353#define ZTEST_BMEM
354#define ZTEST_SECTION .data
355#endif
356
374
384
390
391
393
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 \
405 }; \
406 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
407 { \
408 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
409 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
410 } \
411 static inline void suite##_##fn( \
412 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
413
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)
416
425#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
426
436#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
437
447#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
448
458#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
459
469#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
470
480#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
481
491typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data);
492
494struct ztest_test_rule {
495 ztest_rule_cb before_each;
496 ztest_rule_cb after_each;
497};
498
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), \
522 }
523
524extern struct ztest_test_rule _ztest_test_rule_list_start[];
525extern struct ztest_test_rule _ztest_test_rule_list_end[];
526
535
543void ztest_simple_1cpu_after(void *data);
544
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)
555
561 void (*run_all)(const void *state, bool shuffle, int suite_iter, int case_iter);
562 bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite);
563 bool (*should_test_run)(const char *suite, const char *test);
564};
565
570__syscall void z_test_1cpu_start(void);
571__syscall void z_test_1cpu_stop(void);
572
573__syscall void sys_clock_tick_set(uint64_t tick);
574
575#ifdef __cplusplus
576}
577#endif
578
579#ifndef ZTEST_UNITTEST
580#include <syscalls/ztest_test.h>
581#endif
582
583#endif /* ZEPHYR_TESTSUITE_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