Skip to content

Commit dc3237a

Browse files
committed
twister: support filtering tests using regex
Use --test-pattern to filter scenarios using regular expressions, for example: ./scripts/twister --test-pattern '^kernel.semaphore.*' -v will run all those test using this identifier pattern, and nothing else. Multiple patterns are possible. Signed-off-by: Anas Nashif <anas.nashif@intel.com>
1 parent 322da1d commit dc3237a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

scripts/pylib/twister/twisterlib/environment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ def add_parse_arguments(parser = None) -> argparse.ArgumentParser:
253253
timeout would be multiplication of test timeout value, board-level timeout multiplier
254254
and global timeout multiplier (this parameter)""")
255255

256+
test_xor_subtest.add_argument(
257+
"--test-pattern", action="append",
258+
help="""Run only the tests matching the specified pattern. The pattern
259+
can include regular expressions.
260+
""")
261+
256262
test_xor_subtest.add_argument(
257263
"-s", "--test", "--scenario", action="append", type = norm_path,
258264
help="""Run only the specified test suite scenario. These are named by

scripts/pylib/twister/twisterlib/testplan.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def discover(self):
190190

191191
self.add_configurations()
192192
num = self.add_testsuites(testsuite_filter=self.run_individual_testsuite)
193+
193194
if num == 0:
194195
raise TwisterRuntimeError("No testsuites found at the specified location...")
195196
if self.load_errors:
@@ -202,6 +203,20 @@ def discover(self):
202203
for _, ts in self.testsuites.items():
203204
self.scenarios.append(ts.id)
204205

206+
if self.options.test_pattern:
207+
logger.info(
208+
"Running tests matching the specified patterns: %s",
209+
', '.join(self.options.test_pattern)
210+
)
211+
# If test_pattern is used, we run all tests that match the pattern.
212+
# The pattern can be a regular expression.
213+
self.run_individual_testsuite = []
214+
for pattern in self.options.test_pattern:
215+
r = re.compile(pattern)
216+
for ts in self.testsuites.values():
217+
if r.search(ts.id):
218+
self.run_individual_testsuite.append(ts.id)
219+
205220
self.report_duplicates()
206221
self.parse_configuration(config_file=self.env.test_config)
207222

scripts/tests/twister/test_testplan.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ def test_testplan_discover(
563563

564564
testplan = TestPlan(env=mock.Mock())
565565
testplan.options = mock.Mock(
566+
test_pattern=[],
566567
test='ts1',
567568
quarantine_list=[tmp_path / qf for qf in ql],
568569
quarantine_verify=qv,

0 commit comments

Comments
 (0)