1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 *
10 * @brief Zephyr testing framework _test.
11 */
12
13 #ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
14 #define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
15
16 #include <zephyr/app_memory/app_memdomain.h>
17 #include <zephyr/init.h>
18 #include <zephyr/sys/iterable_sections.h>
19 #include <stdbool.h>
20
21
22 /**
23 * @defgroup ztest_test Ztest testing macros
24 * @ingroup ztest
25 *
26 * This module eases the testing process by providing helpful macros and other
27 * testing structures.
28 *
29 * @{
30 */
31
32 #if defined(CONFIG_USERSPACE)
33 #define __USERSPACE_FLAGS (K_USER)
34 #else
35 #define __USERSPACE_FLAGS (0)
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 /**
43 * @brief The expected result of a test.
44 *
45 * @see ZTEST_EXPECT_FAIL
46 * @see ZTEST_EXPECT_SKIP
47 */
48 enum ztest_expected_result {
49 ZTEST_EXPECTED_RESULT_FAIL = 0, /**< Expect a test to fail */
50 ZTEST_EXPECTED_RESULT_SKIP, /**< Expect a test to pass */
51 };
52
53 /**
54 * @brief A single expectation entry allowing tests to fail/skip and be considered passing.
55 *
56 * @see ZTEST_EXPECT_FAIL
57 * @see ZTEST_EXPECT_SKIP
58 */
59 struct ztest_expected_result_entry {
60 const char *test_suite_name; /**< The test suite's name for the expectation */
61 const char *test_name; /**< The test's name for the expectation */
62 enum ztest_expected_result expected_result; /**< The expectation */
63 };
64
65 extern struct ztest_expected_result_entry _ztest_expected_result_entry_list_start[];
66 extern 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
77 /**
78 * @brief Expect a test to fail (mark it passing if it failed)
79 *
80 * Adding this macro to your logic will allow the failing test to be considered passing, example:
81 *
82 * ZTEST_EXPECT_FAIL(my_suite, test_x);
83 * ZTEST(my_suite, text_x) {
84 * zassert_true(false, NULL);
85 * }
86 *
87 * @param _suite_name The name of the suite
88 * @param _test_name The name of the test
89 */
90 #define ZTEST_EXPECT_FAIL(_suite_name, _test_name) \
91 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_FAIL)
92
93 /**
94 * @brief Expect a test to skip (mark it passing if it failed)
95 *
96 * Adding this macro to your logic will allow the failing test to be considered passing, example:
97 *
98 * ZTEST_EXPECT_SKIP(my_suite, test_x);
99 * ZTEST(my_suite, text_x) {
100 * zassume_true(false, NULL);
101 * }
102 *
103 * @param _suite_name The name of the suite
104 * @param _test_name The name of the test
105 */
106 #define ZTEST_EXPECT_SKIP(_suite_name, _test_name) \
107 __ZTEST_EXPECT(_suite_name, _test_name, ZTEST_EXPECTED_RESULT_SKIP)
108
109 struct ztest_unit_test {
110 const char *test_suite_name;
111 const char *name;
112 void (*test)(void *data);
113 uint32_t thread_options;
114
115 /** Stats */
116 struct ztest_unit_test_stats *const stats;
117 };
118
119 extern struct ztest_unit_test _ztest_unit_test_list_start[];
120 extern struct ztest_unit_test _ztest_unit_test_list_end[];
121 /** Number of registered unit tests */
122 #define ZTEST_TEST_COUNT (_ztest_unit_test_list_end - _ztest_unit_test_list_start)
123
124 /**
125 * Stats about a ztest suite
126 */
127 struct ztest_suite_stats {
128 /** The number of times that the suite ran */
129 uint32_t run_count;
130 /** The number of times that the suite was skipped */
131 uint32_t skip_count;
132 /** The number of times that the suite failed */
133 uint32_t fail_count;
134 };
135
136 struct ztest_unit_test_stats {
137 /** The number of times that the test ran */
138 uint32_t run_count;
139 /** The number of times that the test was skipped */
140 uint32_t skip_count;
141 /** The number of times that the test failed */
142 uint32_t fail_count;
143 /** The number of times that the test passed */
144 uint32_t pass_count;
145 /** The longest duration of the test across multiple times */
146 uint32_t duration_worst_ms;
147 };
148
149 /**
150 * Setup function to run before running this suite
151 *
152 * @return Pointer to the data structure that will be used throughout this test suite
153 */
154 typedef void *(*ztest_suite_setup_t)(void);
155
156 /**
157 * Function to run before each test in this suite
158 *
159 * @param fixture The test suite's fixture returned from setup()
160 */
161 typedef void (*ztest_suite_before_t)(void *fixture);
162
163 /**
164 * Function to run after each test in this suite
165 *
166 * @param fixture The test suite's fixture returned from setup()
167 */
168 typedef void (*ztest_suite_after_t)(void *fixture);
169
170 /**
171 * Teardown function to run after running this suite
172 *
173 * @param fixture The test suite's data returned from setup()
174 */
175 typedef void (*ztest_suite_teardown_t)(void *fixture);
176
177 /**
178 * An optional predicate function to determine if the test should run. If NULL, then the
179 * test will only run once on the first attempt.
180 *
181 * @param global_state The current state of the test application.
182 * @return True if the suite should be run; false to skip.
183 */
184 typedef bool (*ztest_suite_predicate_t)(const void *global_state);
185
186 /**
187 * A single node of test suite. Each node should be added to a single linker section which will
188 * allow ztest_run_test_suites() to iterate over the various nodes.
189 */
190 struct ztest_suite_node {
191 /** The name of the test suite. */
192 const char *const name;
193
194 /** Setup function */
195 const ztest_suite_setup_t setup;
196
197 /** Before function */
198 const ztest_suite_before_t before;
199
200 /** After function */
201 const ztest_suite_after_t after;
202
203 /** Teardown function */
204 const ztest_suite_teardown_t teardown;
205
206 /** Optional predicate filter */
207 const ztest_suite_predicate_t predicate;
208
209 /** Stats */
210 struct ztest_suite_stats *const stats;
211 };
212
213 extern struct ztest_suite_node _ztest_suite_node_list_start[];
214 extern struct ztest_suite_node _ztest_suite_node_list_end[];
215
216 /** Number of registered test suites */
217 #define ZTEST_SUITE_COUNT (_ztest_suite_node_list_end - _ztest_suite_node_list_start)
218
219 /**
220 * Create and register a ztest suite. Using this macro creates a new test suite.
221 * It then creates a struct ztest_suite_node in a specific linker section.
222 *
223 * Tests can then be run by calling ztest_run_test_suites(const void *state) by passing
224 * in the current state. See the documentation for ztest_run_test_suites for more info.
225 *
226 * @param SUITE_NAME The name of the suite
227 * @param PREDICATE A function to test against the state and determine if the test should run.
228 * @param setup_fn The setup function to call before running this test suite
229 * @param before_fn The function to call before each unit test in this suite
230 * @param after_fn The function to call after each unit test in this suite
231 * @param teardown_fn The function to call after running all the tests in this suite
232 */
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 }
245 /**
246 * Default entry point for running or listing registered unit tests.
247 *
248 * @param state The current state of the machine as it relates to the test executable.
249 * @param shuffle Shuffle tests
250 * @param suite_iter Test suite repetitions.
251 * @param case_iter Test case repetitions.
252 */
253 void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter);
254
255 /**
256 * The result of the current running test. It's possible that the setup function sets the result
257 * to ZTEST_RESULT_SUITE_* which will apply the failure/skip to every test in the suite.
258 */
259 enum ztest_result {
260 ZTEST_RESULT_PENDING,
261 ZTEST_RESULT_PASS,
262 ZTEST_RESULT_FAIL,
263 ZTEST_RESULT_SKIP,
264 ZTEST_RESULT_SUITE_SKIP,
265 ZTEST_RESULT_SUITE_FAIL,
266 };
267 /**
268 * Each enum member represents a distinct phase of execution for the test binary.
269 * TEST_PHASE_FRAMEWORK is active when internal ztest code is executing; the rest refer to
270 * corresponding phases of user test code.
271 */
272 enum ztest_phase {
273 TEST_PHASE_SETUP,
274 TEST_PHASE_BEFORE,
275 TEST_PHASE_TEST,
276 TEST_PHASE_AFTER,
277 TEST_PHASE_TEARDOWN,
278 TEST_PHASE_FRAMEWORK,
279 };
280
281 /**
282 * Run the registered unit tests which return true from their predicate function.
283 *
284 * @param state The current state of the machine as it relates to the test executable.
285 * @param shuffle Shuffle tests
286 * @param suite_iter Test suite repetitions.
287 * @param case_iter Test case repetitions.
288 * @return The number of tests that ran.
289 */
290
291 #ifdef ZTEST_UNITTEST
292 int z_impl_ztest_run_test_suites(const void *state, bool shuffle,
293 int suite_iter, int case_iter);
294
ztest_run_test_suites(const void * state,bool shuffle,int suite_iter,int case_iter)295 static 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
307 void z_impl___ztest_set_test_result(enum ztest_result new_result);
__ztest_set_test_result(enum ztest_result new_result)308 static inline void __ztest_set_test_result(enum ztest_result new_result)
309 {
310 z_impl___ztest_set_test_result(new_result);
311 }
312
313 void z_impl___ztest_set_test_phase(enum ztest_phase new_phase);
__ztest_set_test_phase(enum ztest_phase new_phase)314 static 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
324 /**
325 * @brief Fails the test if any of the registered tests did not run.
326 *
327 * When registering test suites, a pragma function can be provided to determine WHEN the test should
328 * run. It is possible that a test suite could be registered but the pragma always prevents it from
329 * running. In cases where a test should make sure that ALL suites ran at least once, this function
330 * may be called at the end of test_main(). It will cause the test to fail if any suite was
331 * registered but never ran.
332 */
333 void ztest_verify_all_test_suites_ran(void);
334
335 /**
336 * @brief Run a test suite.
337 *
338 * Internal implementation. Do not call directly. This will run the full test suite along with some
339 * checks for fast failures and initialization.
340 *
341 * @param name The name of the suite to run.
342 * @param shuffle Shuffle tests
343 * @param suite_iter Test suite repetitions.
344 * @param case_iter Test case repetitions.
345 * @return Negative value if the test suite never ran; otherwise, return the number of failures.
346 */
347 int z_ztest_run_test_suite(const char *name, bool shuffle, int suite_iter, int case_iter);
348
349 /**
350 * @brief Returns next test within suite.
351 *
352 * @param suite Name of suite to get next test from.
353 * @param prev Previous unit test acquired from suite, use NULL to return first
354 * unit test.
355 * @return struct ztest_unit_test*
356 */
357 struct 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
361 /**
362 * @brief Make data section used by Ztest userspace accessible
363 */
364 #define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
365 /**
366 * @brief Make bss section used by Ztest userspace accessible
367 */
368 #define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
369 /**
370 * @brief Ztest data section for accessing data from userspace
371 */
372 #define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
373 extern struct k_mem_partition ztest_mem_partition;
374 #else
375 #define ZTEST_DMEM
376 #define ZTEST_BMEM
377 #define ZTEST_SECTION .data
378 #endif
379
380 /**
381 * @brief Fail the currently running test.
382 *
383 * This is the function called from failed assertions and the like. You
384 * probably don't need to call it yourself.
385 */
386 void ztest_test_fail(void);
387
388 /**
389 * @brief Pass the currently running test.
390 *
391 * Normally a test passes just by returning without an assertion failure.
392 * However, if the success case for your test involves a fatal fault,
393 * you can call this function from k_sys_fatal_error_handler to indicate that
394 * the test passed before aborting the thread.
395 */
396 void ztest_test_pass(void);
397
398 /**
399 * @brief Skip the current test.
400 *
401 */
402 void ztest_test_skip(void);
403
404
405 void ztest_skip_failed_assumption(void);
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
430 /**
431 * @brief Skips the test if config is enabled
432 *
433 * Use this macro at the start of your test case, to skip it when
434 * config is enabled. Useful when your test is still under development.
435 *
436 * @param config The Kconfig option used to skip the test.
437 */
438 #define Z_TEST_SKIP_IFDEF(config) COND_CODE_1(config, (ztest_test_skip()), ())
439
440 /**
441 * @brief Skips the test if config is not enabled
442 *
443 * Use this macro at the start of your test case, to skip it when
444 * config is not enabled. Useful when your need to skip test if some
445 * conifiguration option is not enabled.
446 *
447 * @param config The Kconfig option used to skip the test (if not enabled).
448 */
449 #define Z_TEST_SKIP_IFNDEF(config) COND_CODE_1(config, (), (ztest_test_skip()))
450
451 /**
452 * @brief Create and register a new unit test.
453 *
454 * Calling this macro will create a new unit test and attach it to the declared `suite`. The `suite`
455 * does not need to be defined in the same compilation unit.
456 *
457 * @param suite The name of the test suite to attach this test
458 * @param fn The test function to call.
459 */
460 #define ZTEST(suite, fn) Z_ZTEST(suite, fn, 0)
461
462 /**
463 * @brief Define a test function that should run as a user thread
464 *
465 * This macro behaves exactly the same as ZTEST, but calls the test function in user space if
466 * `CONFIG_USERSPACE` was enabled.
467 *
468 * @param suite The name of the test suite to attach this test
469 * @param fn The test function to call.
470 */
471 #define ZTEST_USER(suite, fn) Z_ZTEST(suite, fn, K_USER)
472
473 /**
474 * @brief Define a test function
475 *
476 * This macro behaves exactly the same as ZTEST(), but the function takes an argument for the
477 * fixture of type `struct suite##_fixture*` named `fixture`.
478 *
479 * @param suite The name of the test suite to attach this test
480 * @param fn The test function to call.
481 */
482 #define ZTEST_F(suite, fn) Z_ZTEST_F(suite, fn, 0)
483
484 /**
485 * @brief Define a test function that should run as a user thread
486 *
487 * If CONFIG_USERSPACE is not enabled, this is functionally identical to ZTEST_F(). The test
488 * function takes a single fixture argument of type `struct suite##_fixture*` named `fixture`.
489 *
490 * @param suite The name of the test suite to attach this test
491 * @param fn The test function to call.
492 */
493 #define ZTEST_USER_F(suite, fn) Z_ZTEST_F(suite, fn, K_USER)
494
495 /**
496 * @brief Test rule callback function signature
497 *
498 * The function signature that can be used to register a test rule's before/after callback. This
499 * provides access to the test and the fixture data (if provided).
500 *
501 * @param test Pointer to the unit test in context
502 * @param data Pointer to the test's fixture data (may be NULL)
503 */
504 typedef void (*ztest_rule_cb)(const struct ztest_unit_test *test, void *data);
505
506 /** @private */
507 struct ztest_test_rule {
508 ztest_rule_cb before_each;
509 ztest_rule_cb after_each;
510 };
511
512 /**
513 * @brief Define a test rule that will run before/after each unit test.
514 *
515 * Functions defined here will run before/after each unit test for every test suite. Along with the
516 * callback, the test functions are provided a pointer to the test being run, and the data. This
517 * provides a mechanism for tests to perform custom operations depending on the specific test or
518 * the data (for example logging may use the test's name).
519 *
520 * Ordering:
521 * - Test rule's `before` function will run before the suite's `before` function. This is done to
522 * allow the test suite's customization to take precedence over the rule which is applied to all
523 * suites.
524 * - Test rule's `after` function is not guaranteed to run in any particular order.
525 *
526 * @param name The name for the test rule (must be unique within the compilation unit)
527 * @param before_each_fn The callback function (ztest_rule_cb) to call before each test
528 * (may be NULL)
529 * @param after_each_fn The callback function (ztest_rule_cb) to call after each test (may be NULL)
530 */
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
537 extern struct ztest_test_rule _ztest_test_rule_list_start[];
538 extern struct ztest_test_rule _ztest_test_rule_list_end[];
539
540 /**
541 * @brief A 'before' function to use in test suites that just need to start 1cpu
542 *
543 * Ignores data, and calls z_test_1cpu_start()
544 *
545 * @param data The test suite's data
546 */
547 void ztest_simple_1cpu_before(void *data);
548
549 /**
550 * @brief A 'after' function to use in test suites that just need to stop 1cpu
551 *
552 * Ignores data, and calls z_test_1cpu_stop()
553 *
554 * @param data The test suite's data
555 */
556 void ztest_simple_1cpu_after(void *data);
557
558 /**
559 * @brief Run the specified test suite.
560 *
561 * @param suite Test suite to run.
562 * @param shuffle Shuffle tests
563 * @param suite_iter Test suite repetitions.
564 * @param case_iter Test case repetitions.
565 */
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
569 /**
570 * @brief Structure for architecture specific APIs
571 *
572 */
573 struct ztest_arch_api {
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
579 /**
580 * @}
581 */
582
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_ */
597