Skip to content

Commit 04b49fa

Browse files
majunkierkartben
authored andcommitted
twister: harness_config extended by test retries in testcase.yaml
Extend Twister to allow defining repeat counts for specific scenarios by harness_config. Signed-off-by: Mateusz Junkier <mateusz.junkier@intel.com>
1 parent 958ef22 commit 04b49fa

File tree

8 files changed

+154
-1
lines changed

8 files changed

+154
-1
lines changed

doc/develop/test/twister.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,18 @@ harness_config: <harness configuration options>
604604
Only one fixture can be defined per test scenario and the fixture name has to
605605
be unique across all tests in the test suite.
606606

607+
ztest_suite_repeat: <int> (default 1)
608+
This parameter specifies the number of times the entire test suite should be repeated.
609+
610+
ztest_test_repeat: <int> (default 1)
611+
This parameter specifies the number of times each individual test within the test suite
612+
should be repeated.
613+
614+
ztest_test_shuffle: <True|False> (default False)
615+
This parameter indicates whether the order of the tests within the test suite should
616+
be shuffled. When set to ``true``, the tests will be executed in a random order.
617+
618+
607619

608620
The following is an example yaml file with robot harness_config options.
609621

scripts/pylib/twister/twisterlib/testinstance.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ def __init__(self, testsuite, platform, toolchain, outdir):
6666
self.build_time = 0
6767
self.retries = 0
6868
self.toolchain = toolchain
69-
7069
self.name = os.path.join(platform.name, toolchain, testsuite.name)
7170
self.dut = None
71+
self.suite_repeat = None
72+
self.test_repeat = None
73+
self.test_shuffle = None
7274

7375
if testsuite.detailed_test_id:
7476
self.build_dir = os.path.join(
@@ -372,6 +374,23 @@ def create_overlay(
372374

373375
content = "\n".join(new_config_list)
374376

377+
378+
if self.testsuite.harness_config:
379+
self.suite_repeat = self.testsuite.harness_config.get('ztest_suite_repeat', None)
380+
self.test_repeat = self.testsuite.harness_config.get('ztest_test_repeat', None)
381+
self.test_shuffle = self.testsuite.harness_config.get('ztest_test_shuffle', False)
382+
383+
384+
# Use suite_repeat and test_repeat values
385+
if self.suite_repeat or self.test_repeat or self.test_shuffle:
386+
content +="\nCONFIG_ZTEST_REPEAT=y"
387+
if self.suite_repeat:
388+
content += f"\nCONFIG_ZTEST_SUITE_REPEAT_COUNT={self.suite_repeat}"
389+
if self.test_repeat:
390+
content += f"\nCONFIG_ZTEST_TEST_REPEAT_COUNT={self.test_repeat}"
391+
if self.test_shuffle:
392+
content +="\nCONFIG_ZTEST_SHUFFLE=y"
393+
375394
if enable_coverage:
376395
for cp in coverage_platform:
377396
if cp in platform.aliases:

scripts/schemas/twister/testsuite-schema.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ schema;scenario-schema:
186186
"bsim_exe_name":
187187
type: str
188188
required: false
189+
"ztest_suite_repeat":
190+
type: int
191+
required: false
192+
"ztest_test_repeat":
193+
type: int
194+
required: false
195+
"ztest_test_shuffle":
196+
type: bool
197+
required: false
189198
"min_ram":
190199
type: int
191200
required: false

scripts/tests/twister/test_testinstance.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,38 @@ def test_testinstance_get_buildlog_file(tmp_path, testinstance, create_build_log
651651

652652
if expected_error is None:
653653
assert res == str(build_log)
654+
655+
656+
TESTDATA_9 = [
657+
(
658+
{'ztest_suite_repeat': 5, 'ztest_test_repeat': 10, 'ztest_test_shuffle': True},
659+
'\nCONFIG_ZTEST_REPEAT=y\nCONFIG_ZTEST_SUITE_REPEAT_COUNT=5\nCONFIG_ZTEST_TEST_REPEAT_COUNT=10\nCONFIG_ZTEST_SHUFFLE=y'
660+
),
661+
(
662+
{'ztest_suite_repeat': 3},
663+
'\nCONFIG_ZTEST_REPEAT=y\nCONFIG_ZTEST_SUITE_REPEAT_COUNT=3'
664+
),
665+
(
666+
{'ztest_test_repeat': 7},
667+
'\nCONFIG_ZTEST_REPEAT=y\nCONFIG_ZTEST_TEST_REPEAT_COUNT=7'
668+
),
669+
(
670+
{'ztest_test_shuffle': True},
671+
'\nCONFIG_ZTEST_REPEAT=y\nCONFIG_ZTEST_SHUFFLE=y'
672+
),
673+
(
674+
{},
675+
''
676+
),
677+
]
678+
679+
@pytest.mark.parametrize('harness_config, expected_content', TESTDATA_9)
680+
def test_create_overlay_with_harness_config(class_testplan, all_testsuites_dict, platforms_list, harness_config, expected_content):
681+
testsuite_path = 'scripts/tests/twister/test_data/testsuites/samples/test_app/sample_test.app'
682+
class_testplan.testsuites = all_testsuites_dict
683+
testsuite = class_testplan.testsuites.get(testsuite_path)
684+
testsuite.harness_config = harness_config
685+
class_testplan.platforms = platforms_list
686+
platform = class_testplan.get_platform("demo_board_2")
687+
testinstance = TestInstance(testsuite, platform,'zephyr', class_testplan.env.outdir)
688+
assert testinstance.create_overlay(platform) == expected_content

tests/ztest/repeat/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(repeat)
6+
7+
target_sources(app PRIVATE src/main.c)

tests/ztest/repeat/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_SUITE_REPEAT_COUNT=3
3+
CONFIG_ZTEST_TEST_REPEAT_COUNT=2

tests/ztest/repeat/src/main.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2024 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr/ztest.h>
7+
8+
extern struct ztest_suite_stats UTIL_CAT(z_ztest_suite_node_stats_, testsuite);
9+
struct ztest_suite_stats *suite_stats = &UTIL_CAT(z_ztest_suite_node_stats_, testsuite);
10+
extern struct ztest_unit_test_stats z_ztest_unit_test_stats_testsuite_test_repeating;
11+
struct ztest_unit_test_stats *case_stats =
12+
&z_ztest_unit_test_stats_testsuite_test_repeating;
13+
14+
static struct test_data {
15+
int suite_run;
16+
int case_run;
17+
} expected = {0, 0};
18+
19+
ZTEST(testsuite, test_repeating)
20+
{
21+
expected.case_run++;
22+
23+
if (case_stats->run_count > 0) {
24+
zassert_true(case_stats->run_count == expected.case_run);
25+
}
26+
27+
if (suite_stats->run_count > 0) {
28+
zassert_true(suite_stats->run_count + 1 == expected.suite_run);
29+
}
30+
}
31+
32+
static void *repeat_setup(void)
33+
{
34+
expected.suite_run++;
35+
return NULL;
36+
}
37+
38+
ZTEST_SUITE(testsuite, NULL, repeat_setup, NULL, NULL, NULL);

tests/ztest/repeat/testcase.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
tests:
2+
testing.ztest.repeat_suite_regex:
3+
platform_allow: qemu_x86
4+
harness: console
5+
harness_config:
6+
ztest_suite_repeat: 3
7+
ztest_test_repeat: 2
8+
type: multi_line
9+
regex:
10+
- "Running TESTSUITE testsuite"
11+
- "Running TESTSUITE testsuite"
12+
- "Running TESTSUITE testsuite"
13+
14+
testing.ztest.repeat_testcase_regex:
15+
platform_allow: qemu_x86
16+
harness: console
17+
harness_config:
18+
ztest_suite_repeat: 1
19+
ztest_test_repeat: 5
20+
type: multi_line
21+
regex:
22+
- "START - test_repeating"
23+
- "START - test_repeating"
24+
- "START - test_repeating"
25+
- "START - test_repeating"
26+
- "START - test_repeating"
27+
28+
testing.ztest.repeat_regex:
29+
platform_allow: qemu_x86
30+
harness: ztest

0 commit comments

Comments
 (0)