Zephyr Project API 3.7.0
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
347int z_ztest_run_test_suite(const char *name, bool shuffle, int suite_iter, int case_iter);
348
357struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev);
358
359/* definitions for use with testing application shared memory */
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)
374#else
375#define ZTEST_DMEM
376#define ZTEST_BMEM
377#define ZTEST_SECTION .data
378#endif
379
387
397
403
404
406
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 \
418 }; \
419 static void _##suite##_##fn##_wrapper(void *wrapper_data) \
420 { \
421 COND_CODE_1(use_fixture, (suite##_##fn((struct suite##_fixture *)wrapper_data);), \
422 (ARG_UNUSED(wrapper_data); suite##_##fn();)) \
423 } \
424 static inline void suite##_##fn( \
425 COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
426
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)
429
438#define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
439
449#define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
450
460#define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
461
471#define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
472
482#define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
483
493#define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
494
504typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data);
505
507struct ztest_test_rule {
508 ztest_rule_cb before_each;
509 ztest_rule_cb after_each;
510};
511
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), \
535 }
536
537extern struct ztest_test_rule _ztest_test_rule_list_start[];
538extern struct ztest_test_rule _ztest_test_rule_list_end[];
539
548
557
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)
568
574 void (*run_all)(const void *state, bool shuffle, int suite_iter, int case_iter);
575 bool (*should_suite_run)(const void *state, struct ztest_suite_node *suite);
576 bool (*should_test_run)(const char *suite, const char *test);
577};
578
583__syscall void z_test_1cpu_start(void);
584__syscall void z_test_1cpu_stop(void);
585
586__syscall void sys_clock_tick_set(uint64_t tick);
587
588#ifdef __cplusplus
589}
590#endif
591
592#ifndef ZTEST_UNITTEST
593#include <zephyr/syscalls/ztest_test.h>
594#endif
595
596#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */
static struct shared_irq_fixture fixture
Definition dynamic_shared_irq.c:17
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
static fdata_t data[2]
Definition test_fifo_contexts.c:15
void sys_clock_tick_set(uint64_t tick)