Skip to content

Commit 9f70611

Browse files
author
Yuval Peress
committed
ztest: Update ztest with more powerful testing APIs
1. Test suites in prior ztest serve no purpose other than logical ordering of tests into a named-group. Move the construct of setup and teardown into the test suite and away from individual tests. Additionally, add the constructs of before/after to the test suites. This model more closely resembels other testing frameworks such as gTest and Junit. 2. Test can be added to a suite by using ZTEST() or ZTEST_F() where _F stands for fixture. In the case where _F is used, the argument `this` will be provided with the type `struct suite_name##_fixture*`. Again, this models other modern testing frameworks and allows the test to directly access the already set up data related to the test suite. 3. Add the concept of test rules (from Junit). Rules are similar to the before/after functions of the test suites but are global and run on all suites. An example of a test rule can be to check that nothing was logged to ERROR. The rule can cause the test to fail if anything was logged to ERROR during an integration test. Another example would be a rule that verifies that tests ran within some defined timeout. Signed-off-by: Yuval Peress <[email protected]>
1 parent e2588d6 commit 9f70611

File tree

22 files changed

+1500
-348
lines changed

22 files changed

+1500
-348
lines changed

include/linker/common-ram.ld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,8 @@
141141

142142
#if defined(CONFIG_ZTEST)
143143
ITERABLE_SECTION_RAM(ztest_suite_node, 4)
144+
#if defined(CONFIG_ZTEST_NEW_API)
145+
ITERABLE_SECTION_RAM(ztest_unit_test, 4)
146+
ITERABLE_SECTION_RAM(ztest_test_rule, 4)
147+
#endif /* CONFIG_ZTEST_NEW_API */
144148
#endif /* CONFIG_ZTEST */
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_NEW_API=y

samples/subsys/testsuite/integration/src/main.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
#include <ztest.h>
88

9+
10+
ZTEST_SUITE(framework_tests, NULL, NULL, NULL, NULL, NULL);
11+
912
/**
1013
* @brief Test Asserts
1114
*
1215
* This test verifies various assert macros provided by ztest.
1316
*
1417
*/
15-
static void test_assert(void)
18+
ZTEST(framework_tests, test_assert)
1619
{
1720
zassert_true(1, "1 was false");
1821
zassert_false(0, "0 was true");
@@ -21,12 +24,3 @@ static void test_assert(void)
2124
zassert_equal(1, 1, "1 was not equal to 1");
2225
zassert_equal_ptr(NULL, NULL, "NULL was not equal to NULL");
2326
}
24-
25-
void test_main(void)
26-
{
27-
ztest_test_suite(framework_tests,
28-
ztest_unit_test(test_assert)
29-
);
30-
31-
ztest_run_test_suite(framework_tests);
32-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_NEW_API=y
23
CONFIG_IDLE_STACK_SIZE=4096

samples/subsys/testsuite/pytest/src/main.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <ztest.h>
7-
void test_pytest(void)
8-
{
9-
TC_PRINT("Hello world\n");
10-
}
117

12-
void test_main(void)
13-
{
14-
ztest_test_suite(test_pytest,
15-
ztest_unit_test(test_pytest)
16-
);
8+
ZTEST_SUITE(test_pytest, NULL, NULL, NULL, NULL, NULL);
179

18-
ztest_run_test_suite(test_pytest);
10+
ZTEST(test_pytest, test_pytest)
11+
{
12+
TC_PRINT("Hello world\n");
1913
}

subsys/testsuite/include/ztest.ld

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,17 @@ SECTIONS
1212
KEEP(*(SORT_BY_NAME(._ztest_suite_node.static.*)))
1313
_ztest_suite_node_list_end = .;
1414
}
15+
.data.ztest_unit_test_area : ALIGN(4)
16+
{
17+
_ztest_unit_test_list_start = .;
18+
KEEP(*(SORT_BY_NAME(._ztest_unit_test.static.*)))
19+
_ztest_unit_test_list_end = .;
20+
}
21+
.data.ztest_test_rule_area : ALIGN(4)
22+
{
23+
_ztest_test_rule_list_start = .;
24+
KEEP(*(SORT_BY_NAME(._ztest_test_rule.static.*)))
25+
_ztest_test_rule_list_end = .;
26+
}
1527
}
1628
INSERT AFTER .data;

subsys/testsuite/ztest/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ zephyr_include_directories(
66
)
77

88
zephyr_library()
9-
zephyr_library_sources( src/ztest.c)
10-
zephyr_library_sources( src/ztest_error_hook.c)
11-
zephyr_library_sources_ifdef(CONFIG_ZTEST_MOCKING src/ztest_mock.c)
12-
zephyr_library_sources_ifdef(CONFIG_ZTRESS src/ztress.c)
9+
zephyr_library_sources_ifndef(CONFIG_ZTEST_NEW_API src/ztest.c)
10+
zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_new.c)
11+
zephyr_library_sources( src/ztest_error_hook.c)
12+
zephyr_library_sources_ifdef(CONFIG_ZTEST_NEW_API src/ztest_rules.c)
13+
zephyr_library_sources_ifdef(CONFIG_ZTEST_MOCKING src/ztest_mock.c)
14+
zephyr_library_sources_ifdef(CONFIG_ZTRESS src/ztress.c)

subsys/testsuite/ztest/Kconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ config ZTEST
1010

1111
if ZTEST
1212

13+
config ZTEST_NEW_API
14+
bool "Use the new Ztest API"
15+
help
16+
Enables the new Ztest APIs for creating suites and unit tests in
17+
separate compilational units as well as the new 'rules' API.
18+
1319
config ZTEST_STACKSIZE
1420
int "Test function thread stack size"
1521
default 2048 if COVERAGE_GCOV
@@ -73,6 +79,20 @@ config ZTEST_ASSERT_HOOK
7379
error test case. Remember to add ignore_fault tag in yaml file when
7480
using twister to run testing.
7581

82+
if ZTEST_NEW_API
83+
84+
menu "ztest provided rules"
85+
86+
config ZTEST_RULE_1CPU
87+
bool "Run all the tests on a single CPU"
88+
help
89+
This rule will call z_test_1cpu_start before each unit test and
90+
ztest_1cpu_stop after each test.
91+
92+
endmenu
93+
94+
endif # ZTEST_NEW_API
95+
7696
endif # ZTEST
7797

7898
config ZTEST_MOCKING

0 commit comments

Comments
 (0)