Zephyr API Documentation 4.0.0-rc2
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
32#if defined(CONFIG_USERSPACE)
33#define __USERSPACE_FLAGS (K_USER)
34#else
35#define __USERSPACE_FLAGS (0)
36#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
52
64
65extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[];
66extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_end[];
67
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, \
75 }
76
90#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
91 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
92
106#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
107 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
108
110 const char *test_suite_name;
111 const char *name;
112 void (*test)(void *data);
114
117};
118
119extern struct ztest_unit_test _ztest_unit_test_list_start[];
120extern struct ztest_unit_test _ztest_unit_test_list_end[];
122#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
123
135
148
154typedef void *(*ztest_suite_setup_t)(void);
155
161typedef void (*ztest_suite_before_t)(void *fixture);
162
168typedef void (*ztest_suite_after_t)(void *fixture);
169
175typedef void (*ztest_suite_teardown_t)(void *fixture);
176
184typedef bool (*ztest_suite_predicate_t)(const void *global_state);
185
212
213extern struct ztest_suite_node _ztest_suite_node_list_start[];
214extern struct ztest_suite_node _ztest_suite_node_list_end[];
215
217#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
218
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), \
244 }
253void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter);
254
280
291#ifdef ZTEST_UNITTEST
292int z_impl_ztest_run_test_suites(const void *state, bool shuffle,
293 int suite_iter, int case_iter);
294
295static inline int ztest_run_test_suites(const void *state, bool shuffle,
296 int suite_iter, int case_iter)
297{
298 return z_impl_ztest_run_test_suites(state, shuffle, suite_iter, case_iter);
299}
300
301#else
302__syscall int ztest_run_test_suites(const void *state, bool shuffle,
303 int suite_iter, int case_iter);
304#endif
305
306#ifdef ZTEST_UNITTEST
307void z_impl___ztest_set_test_result(enum ztest_result new_result);
308static inline void __ztest_set_test_result(enum ztest_result new_result)
309{
310 z_impl___ztest_set_test_result(new_result);
311}
312
313void z_impl___ztest_set_test_phase(enum ztest_phase new_phase);
314static inline void __ztest_set_test_phase(enum ztest_phase new_phase)
315{
316 z_impl___ztest_set_test_phase(new_phase);
317}
318#else
319__syscall void __ztest_set_test_result(enum ztest_result new_result);
320__syscall void __ztest_set_test_phase(enum ztest_phase new_phase);
321#endif
322
323
334
348int z_ztest_run_test_suite(const char *name, bool shuffle, int suite_iter,
349 int case_iter, void *param);
350
359struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev);
360
361/* definitions for use with testing application shared memory */
362#ifdef CONFIG_USERSPACE
366#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
370#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
374#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
376#else
377#define ZTEST_DMEM
378#define ZTEST_BMEM
379#define ZTEST_SECTION .data
380#endif
381
389
399
405
406
408
409#define Z_TEST_P(suite, fn, t_options) \
410 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
411 static void _##suite##_##fn##_wrapper(void *data); \
412 static void suite##_##fn(void *data); \
413 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
414 .test_suite_name = STRINGIFY(suite), \
415 .name = STRINGIFY(fn), \
416 .test = (_##suite##_##fn##_wrapper), \
417 .thread_options = t_options, \
418 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
419 }; \
420 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
421 { \
422 suite##_##fn(wrapper_data); \
423 } \
424 static inline void suite##_##fn(void *data)
425
426
427#define ZTEST_P(suite, fn) Z_TEST_P(suite, fn, 0)
428
429#define Z_TEST(suite, fn, t_options, use_fixture) \
430 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
431 static void _##suite##_##fn##_wrapper(void *data); \
432 static void suite##_##fn( \
433 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \
434 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
435 .test_suite_name = STRINGIFY(suite), \
436 .name = STRINGIFY(fn), \
437 .test = (_##suite##_##fn##_wrapper), \
438 .thread_options = t_options, \
439 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
440 }; \
441 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
442 { \
443 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
444 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
445 } \
446 static inline void suite##_##fn( \
447 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
448
449#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0)
450#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1)
451
460#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
461
471#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
472
482#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
483
493#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
494
504#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
505
515#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
516
526typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data);
527
529struct ztest_test_rule {
530 ztest_rule_cb before_each;
531 ztest_rule_cb after_each;
532};
533
553#define ZTEST_RULE(name, before_each_fn, after_each_fn) \
554 static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \
555 .before_each = (before_each_fn), \
556 .after_each = (after_each_fn), \
557 }
558
559extern struct ztest_test_rule _ztest_test_rule_list_start[];
560extern struct ztest_test_rule _ztest_test_rule_list_end[];
561
570
578void ztest_simple_1cpu_after(void *data);
579
589#define ztest_run_test_suite(suite, shuffle, suite_iter, case_iter, param) \
590 z_ztest_run_test_suite(STRINGIFY(suite), shuffle, suite_iter, case_iter, param)
591
597 void (*run_all)(const void *state, bool shuffle, int suite_iter, int case_iter);
598 bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite);
599 bool (*should_test_run)(const char *suite, const char *test);
600};
601
606__syscall void z_test_1cpu_start(void);
607__syscall void z_test_1cpu_stop(void);
608
609__syscall void sys_clock_tick_set(uint64_t tick);
610
611#ifdef __cplusplus
612}
613#endif
614
615#ifndef ZTEST_UNITTEST
616#include <zephyr/syscalls/ztest_test.h>
617#endif
618
619#endif /* ZEPHYR_TESTSUITE_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:526
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:596
void(* run_all)(const void *state, bool shuffle, int suite_iter, int case_iter)
Definition ztest_test.h:597
bool(* should_suite_run)(const void *state, struct ztest_suite_node *suite)
Definition ztest_test.h:598
bool(* should_test_run)(const char *suite, const char *test)
Definition ztest_test.h:599
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)