Zephyr API Documentation 4.4.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
12
13#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
14#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
15
17#include <zephyr/init.h>
19#include <zephyr/sys/util.h>
20#include <stdbool.h>
21
22
32
33#if defined(CONFIG_USERSPACE)
34#define __USERSPACE_FLAGS (K_USER)
35#else
36#define __USERSPACE_FLAGS (0)
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
53
65
66extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[];
67extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_end[];
68
69#define __ZTEST_EXPECT(_suite_name, _test_name, expectation) \
70 static const STRUCT_SECTION_ITERABLE( \
71 ztest_expected_result_entry, \
72 UTIL_CAT(UTIL_CAT(z_ztest_expected_result_, _suite_name), _test_name)) = { \
73 .test_suite_name = STRINGIFY(_suite_name), \
74 .test_name = STRINGIFY(_test_name), \
75 .expected_result = expectation, \
76 }
77
91#define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
92 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
93
107#define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
108 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
109
111 const char *test_suite_name;
112 const char *name;
113 void (*test)(void *data);
115
118};
119
125 const void *values;
127 size_t count;
129 size_t elem_size;
131 const char *(*name_cb)(size_t index, const void *value);
143 void (*value_at_cb)(size_t index, void *out);
154 void (*setup_cb)(void);
155};
156
162 const char *test_suite_name;
164 const char *test_name;
166 const char *instance_name;
169};
170
171extern struct ztest_unit_test _ztest_unit_test_list_start[];
172extern struct ztest_unit_test _ztest_unit_test_list_end[];
173extern struct ztest_param_inst _ztest_param_inst_list_start[];
174extern struct ztest_param_inst _ztest_param_inst_list_end[];
176#define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
178#define ZTEST_PARAM_INST_COUNT (_ztest_param_inst_list_end - _ztest_param_inst_list_start)
179
191
204
210typedef void *(*ztest_suite_setup_t)(void);
211
217typedef void (*ztest_suite_before_t)(void *fixture);
218
224typedef void (*ztest_suite_after_t)(void *fixture);
225
231typedef void (*ztest_suite_teardown_t)(void *fixture);
232
240typedef bool (*ztest_suite_predicate_t)(const void *global_state);
241
268
269extern struct ztest_suite_node _ztest_suite_node_list_start[];
270extern struct ztest_suite_node _ztest_suite_node_list_end[];
271
273#define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
274
289#define ZTEST_SUITE(SUITE_NAME, PREDICATE, setup_fn, before_fn, after_fn, teardown_fn) \
290 struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME); \
291 static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \
292 UTIL_CAT(z_ztest_test_node_, SUITE_NAME)) = { \
293 .name = STRINGIFY(SUITE_NAME), \
294 .setup = (setup_fn), \
295 .before = (before_fn), \
296 .after = (after_fn), \
297 .teardown = (teardown_fn), \
298 .predicate = PREDICATE, \
299 .stats = &UTIL_CAT(z_ztest_suite_node_stats_, SUITE_NAME), \
300 }
301
309void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter);
310
323
336
346
347#ifdef ZTEST_UNITTEST
348int z_impl_ztest_run_test_suites(const void *state, bool shuffle,
349 int suite_iter, int case_iter);
350
351static inline int ztest_run_test_suites(const void *state, bool shuffle,
352 int suite_iter, int case_iter)
353{
354 return z_impl_ztest_run_test_suites(state, shuffle, suite_iter, case_iter);
355}
356
357#else
358__syscall int ztest_run_test_suites(const void *state, bool shuffle,
359 int suite_iter, int case_iter);
360#endif
361
362#ifdef ZTEST_UNITTEST
363void z_impl___ztest_set_test_result(enum ztest_result new_result);
364static inline void __ztest_set_test_result(enum ztest_result new_result)
365{
366 z_impl___ztest_set_test_result(new_result);
367}
368
369void z_impl___ztest_set_test_phase(enum ztest_phase new_phase);
370static inline void __ztest_set_test_phase(enum ztest_phase new_phase)
371{
372 z_impl___ztest_set_test_phase(new_phase);
373}
374#else
375__syscall void __ztest_set_test_result(enum ztest_result new_result);
376__syscall void __ztest_set_test_phase(enum ztest_phase new_phase);
377#endif
378
379
390
404int z_ztest_run_test_suite(const char *name, bool shuffle, int suite_iter,
405 int case_iter, void *param);
406
415struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev);
416
417/* definitions for use with testing application shared memory */
418#ifdef CONFIG_USERSPACE
422#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
426#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
430#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
432#else
433#define ZTEST_DMEM
434#define ZTEST_BMEM
435#define ZTEST_SECTION .data
436#endif
437
445
455
461
462
464
470const void *ztest_get_current_param(void);
471
478
485
490
494#define ZTEST_GET_PARAM_PTR(type) ((const type *)ztest_get_current_param())
495
499#define ZTEST_GET_PARAM(type) (*ZTEST_GET_PARAM_PTR(type))
500
512#define ZTEST_DEFINE_PARAM_VALUES(name_, type_, ...) \
513 static const type_ z_ztest_param_arr_##name_[] = {__VA_ARGS__}; \
514 static const struct ztest_param_values name_ = { \
515 .values = z_ztest_param_arr_##name_, \
516 .count = ARRAY_SIZE(z_ztest_param_arr_##name_), \
517 .elem_size = sizeof(type_), \
518 .name_cb = NULL, \
519 .value_at_cb = NULL, \
520 .setup_cb = NULL, \
521 }
522
529#define ZTEST_DEFINE_PARAM_VALUES_ARRAY(name_, array_) \
530 static const struct ztest_param_values name_ = { \
531 .values = (array_), \
532 .count = ARRAY_SIZE(array_), \
533 .elem_size = sizeof((array_)[0]), \
534 .name_cb = NULL, \
535 .value_at_cb = NULL, \
536 .setup_cb = NULL, \
537 }
538
560#define ZTEST_DEFINE_PARAM_RANGE(name_, type_, begin_, end_, step_) \
561 BUILD_ASSERT((end_) > (begin_), "ZTEST_DEFINE_PARAM_RANGE: end must be > begin"); \
562 BUILD_ASSERT((step_) > 0, "ZTEST_DEFINE_PARAM_RANGE: step must be > 0"); \
563 BUILD_ASSERT(sizeof(type_) <= 16U, \
564 "ZTEST_DEFINE_PARAM_RANGE: type too large for scratch buffer"); \
565 static void z_ztest_range_value_at_##name_(size_t z_idx_, void *z_out_) \
566 { \
567 type_ z_val_ = (type_)(begin_) + (type_)(step_) * (type_)(z_idx_); \
568 *(type_ *)z_out_ = z_val_; \
569 } \
570 static const struct ztest_param_values name_ = { \
571 .values = NULL, \
572 .count = (size_t)(((end_) - (begin_) - 1) / (step_) + 1), \
573 .elem_size = sizeof(type_), \
574 .name_cb = NULL, \
575 .value_at_cb = z_ztest_range_value_at_##name_, \
576 .setup_cb = NULL, \
577 }
578
601#define ZTEST_DEFINE_PARAM_GENERATOR(name_, type_, count_, gen_cb_) \
602 BUILD_ASSERT(sizeof(type_) <= 16U, \
603 "ZTEST_DEFINE_PARAM_GENERATOR: type too large for scratch buffer"); \
604 static const struct ztest_param_values name_ = { \
605 .values = NULL, \
606 .count = (count_), \
607 .elem_size = sizeof(type_), \
608 .name_cb = NULL, \
609 .value_at_cb = (gen_cb_), \
610 .setup_cb = NULL, \
611 }
612
642#define ZTEST_DEFINE_PARAM_GENERATOR_WITH_SETUP(name_, type_, count_, setup_cb_, gen_cb_) \
643 BUILD_ASSERT(sizeof(type_) <= 16U, \
644 "ZTEST_DEFINE_PARAM_GENERATOR_WITH_SETUP: type too large"); \
645 static const struct ztest_param_values name_ = { \
646 .values = NULL, \
647 .count = (count_), \
648 .elem_size = sizeof(type_), \
649 .name_cb = NULL, \
650 .value_at_cb = (gen_cb_), \
651 .setup_cb = (setup_cb_), \
652 }
653
673/* Note: all parameters carry a trailing underscore to avoid name collisions
674 * with the struct fields of ztest_param_inst (e.g. a parameter named
675 * 'instance_name' would cause the preprocessor to replace the token inside
676 * the '.instance_name' designator, producing the invalid '.ints').
677 */
678#define ZTEST_INSTANTIATE_TEST_SUITE_P(inst_, suite_, fn_, param_values_name_) \
679 static const STRUCT_SECTION_ITERABLE( \
680 ztest_param_inst, \
681 UTIL_CAT(UTIL_CAT(z_ztest_param_inst_, suite_), \
682 UTIL_CAT(_, UTIL_CAT(fn_, UTIL_CAT(_, inst_))))) = { \
683 .test_suite_name = STRINGIFY(suite_), \
684 .test_name = STRINGIFY(fn_), \
685 .instance_name = STRINGIFY(inst_), \
686 .values = &(param_values_name_), \
687 }
688
689#define Z_TEST_P(suite, fn, t_options) \
690 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
691 static void _##suite##_##fn##_wrapper(void *data); \
692 /* data carries the suite fixture from setup(); use */ \
693 /* ztest_get_current_param() / ZTEST_GET_PARAM() for the value. */ \
694 static void suite##_##fn(void *data); \
695 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
696 .test_suite_name = STRINGIFY(suite), \
697 .name = STRINGIFY(fn), \
698 .test = (_##suite##_##fn##_wrapper), \
699 .thread_options = t_options, \
700 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
701 }; \
702 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
703 { \
704 suite##_##fn(wrapper_data); \
705 } \
706 static inline void suite##_##fn(void *data)
707
708
709#define ZTEST_P(suite, fn) Z_TEST_P(suite, fn, 0)
710
723#define ZTEST_USER_P(suite, fn) Z_TEST_P(suite, fn, K_USER)
724
725#define Z_TEST(suite, fn, t_options, use_fixture) \
726 struct ztest_unit_test_stats z_ztest_unit_test_stats_##suite##_##fn; \
727 static void _##suite##_##fn##_wrapper(void *data); \
728 static void suite##_##fn( \
729 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \
730 static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test__##suite##__##fn) = { \
731 .test_suite_name = STRINGIFY(suite), \
732 .name = STRINGIFY(fn), \
733 .test = (_##suite##_##fn##_wrapper), \
734 .thread_options = t_options, \
735 .stats = &z_ztest_unit_test_stats_##suite##_##fn \
736 }; \
737 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
738 { \
739 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
740 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
741 } \
742 static inline void suite##_##fn( \
743 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
744
745#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0)
746#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1)
747
756#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
757
767#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
768
778#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
779
789#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
790
800#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
801
811#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
812
822typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data);
823
825struct ztest_test_rule {
826 ztest_rule_cb before_each;
827 ztest_rule_cb after_each;
828};
829
849#define ZTEST_RULE(name, before_each_fn, after_each_fn) \
850 static STRUCT_SECTION_ITERABLE(ztest_test_rule, z_ztest_test_rule_##name) = { \
851 .before_each = (before_each_fn), \
852 .after_each = (after_each_fn), \
853 }
854
855extern struct ztest_test_rule _ztest_test_rule_list_start[];
856extern struct ztest_test_rule _ztest_test_rule_list_end[];
857
866
874void ztest_simple_1cpu_after(void *data);
875
885#define ztest_run_test_suite(suite, shuffle, suite_iter, case_iter, param) \
886 z_ztest_run_test_suite(STRINGIFY(suite), shuffle, suite_iter, case_iter, param)
887
893 void (*run_all)(const void *state, bool shuffle, int suite_iter, int case_iter);
894 bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite);
895 bool (*should_test_run)(const char *suite, const char *test);
896};
897
901
902__syscall void z_test_1cpu_start(void);
903__syscall void z_test_1cpu_stop(void);
904
905__syscall void sys_clock_tick_set(uint64_t tick);
906
907#ifdef __cplusplus
908}
909#endif
910
911#ifndef ZTEST_UNITTEST
912#include <zephyr/syscalls/ztest_test.h>
913#endif
914
915#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */
void *(* ztest_suite_setup_t)(void)
Setup function to run before running this suite.
Definition ztest_test.h:210
void ztest_test_pass(void)
Pass the currently running test.
size_t ztest_get_current_param_index(void)
Returns index of currently active value-parameterized test argument.
void(* ztest_suite_after_t)(void *fixture)
Function to run after each test in this suite.
Definition ztest_test.h:224
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:822
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
size_t ztest_get_current_param_size(void)
Returns size of current parameter element in bytes.
ztest_phase
Each enum member represents a distinct phase of execution for the test binary.
Definition ztest_test.h:328
void(* ztest_suite_teardown_t)(void *fixture)
Teardown function to run after running this suite.
Definition ztest_test.h:231
const void * ztest_get_current_param(void)
Returns currently active value-parameterized test argument.
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.
bool ztest_has_current_param(void)
Returns true when currently running a value-parameterized test instance.
ztest_result
The result of the current running test.
Definition ztest_test.h:315
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:217
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:240
void ztest_test_skip(void)
Skip the current test.
ztest_expected_result
The expected result of a test.
Definition ztest_test.h:49
@ TEST_PHASE_TEST
Definition ztest_test.h:331
@ TEST_PHASE_AFTER
Definition ztest_test.h:332
@ TEST_PHASE_BEFORE
Definition ztest_test.h:330
@ TEST_PHASE_SETUP
Definition ztest_test.h:329
@ TEST_PHASE_TEARDOWN
Definition ztest_test.h:333
@ TEST_PHASE_FRAMEWORK
Definition ztest_test.h:334
@ ZTEST_RESULT_SKIP
Definition ztest_test.h:319
@ ZTEST_RESULT_PENDING
Definition ztest_test.h:316
@ ZTEST_RESULT_PASS
Definition ztest_test.h:317
@ ZTEST_RESULT_FAIL
Definition ztest_test.h:318
@ ZTEST_RESULT_SUITE_FAIL
Definition ztest_test.h:321
@ ZTEST_RESULT_SUITE_SKIP
Definition ztest_test.h:320
@ ZTEST_EXPECTED_RESULT_FAIL
Expect a test to fail.
Definition ztest_test.h:50
@ ZTEST_EXPECTED_RESULT_SKIP
Expect a test to pass.
Definition ztest_test.h:51
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:892
void(* run_all)(const void *state, bool shuffle, int suite_iter, int case_iter)
Definition ztest_test.h:893
bool(* should_suite_run)(const void *state, struct ztest_suite_node *suite)
Definition ztest_test.h:894
bool(* should_test_run)(const char *suite, const char *test)
Definition ztest_test.h:895
A single expectation entry allowing tests to fail/skip and be considered passing.
Definition ztest_test.h:60
const char * test_suite_name
The test suite's name for the expectation.
Definition ztest_test.h:61
enum ztest_expected_result expected_result
The expectation.
Definition ztest_test.h:63
const char * test_name
The test's name for the expectation.
Definition ztest_test.h:62
Registration entry mapping a test to a parameter value set.
Definition ztest_test.h:160
const char * instance_name
Human-readable label for this particular instantiation.
Definition ztest_test.h:166
const char * test_suite_name
Name of the test suite this instantiation belongs to.
Definition ztest_test.h:162
const char * test_name
Name of the parameterized test function.
Definition ztest_test.h:164
const struct ztest_param_values * values
Parameter value set to iterate over for this instantiation.
Definition ztest_test.h:168
Parameter value set for value-parameterized tests.
Definition ztest_test.h:123
const void * values
Pointer to the array of parameter values (NULL when value_at_cb is set).
Definition ztest_test.h:125
size_t count
Number of parameter values (or invocations when value_at_cb is set).
Definition ztest_test.h:127
void(* setup_cb)(void)
Optional one-time setup called before the dispatch loop.
Definition ztest_test.h:154
void(* value_at_cb)(size_t index, void *out)
Optional value generator for range-based or runtime parameters.
Definition ztest_test.h:143
size_t elem_size
Size in bytes of each individual parameter value.
Definition ztest_test.h:129
A single node of test suite.
Definition ztest_test.h:246
const ztest_suite_before_t before
Before function.
Definition ztest_test.h:254
const ztest_suite_setup_t setup
Setup function.
Definition ztest_test.h:251
struct ztest_suite_stats *const stats
Stats.
Definition ztest_test.h:266
const ztest_suite_after_t after
After function.
Definition ztest_test.h:257
const char *const name
The name of the test suite.
Definition ztest_test.h:248
const ztest_suite_teardown_t teardown
Teardown function.
Definition ztest_test.h:260
const ztest_suite_predicate_t predicate
Optional predicate filter.
Definition ztest_test.h:263
Stats about a ztest suite.
Definition ztest_test.h:183
uint32_t skip_count
The number of times that the suite was skipped.
Definition ztest_test.h:187
uint32_t fail_count
The number of times that the suite failed.
Definition ztest_test.h:189
uint32_t run_count
The number of times that the suite ran.
Definition ztest_test.h:185
Definition ztest_test.h:192
uint32_t run_count
The number of times that the test ran.
Definition ztest_test.h:194
uint32_t pass_count
The number of times that the test passed.
Definition ztest_test.h:200
uint32_t skip_count
The number of times that the test was skipped.
Definition ztest_test.h:196
uint32_t duration_worst_ms
The longest duration of the test across multiple times.
Definition ztest_test.h:202
uint32_t fail_count
The number of times that the test failed.
Definition ztest_test.h:198
Definition ztest_test.h:110
const char * name
Definition ztest_test.h:112
struct ztest_unit_test_stats *const stats
Stats.
Definition ztest_test.h:117
void(* test)(void *data)
Definition ztest_test.h:113
uint32_t thread_options
Definition ztest_test.h:114
const char * test_suite_name
Definition ztest_test.h:111
Iterable sections helpers.
Misc utilities.
void sys_clock_tick_set(uint64_t tick)