From dc3237ab09b96cb0977df790ed20fa1969fa2f90 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Sat, 12 Jul 2025 11:55:02 -0400 Subject: [PATCH] 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 --- scripts/pylib/twister/twisterlib/environment.py | 6 ++++++ scripts/pylib/twister/twisterlib/testplan.py | 15 +++++++++++++++ scripts/tests/twister/test_testplan.py | 1 + 3 files changed, 22 insertions(+) diff --git a/scripts/pylib/twister/twisterlib/environment.py b/scripts/pylib/twister/twisterlib/environment.py index f956f853c2b50..b9b52839547f1 100644 --- a/scripts/pylib/twister/twisterlib/environment.py +++ b/scripts/pylib/twister/twisterlib/environment.py @@ -253,6 +253,12 @@ def add_parse_arguments(parser = None) -> argparse.ArgumentParser: timeout would be multiplication of test timeout value, board-level timeout multiplier and global timeout multiplier (this parameter)""") + test_xor_subtest.add_argument( + "--test-pattern", action="append", + help="""Run only the tests matching the specified pattern. The pattern + can include regular expressions. + """) + test_xor_subtest.add_argument( "-s", "--test", "--scenario", action="append", type = norm_path, help="""Run only the specified test suite scenario. These are named by diff --git a/scripts/pylib/twister/twisterlib/testplan.py b/scripts/pylib/twister/twisterlib/testplan.py index 6398eae2d2965..baec1fa77c679 100755 --- a/scripts/pylib/twister/twisterlib/testplan.py +++ b/scripts/pylib/twister/twisterlib/testplan.py @@ -190,6 +190,7 @@ def discover(self): self.add_configurations() num = self.add_testsuites(testsuite_filter=self.run_individual_testsuite) + if num == 0: raise TwisterRuntimeError("No testsuites found at the specified location...") if self.load_errors: @@ -202,6 +203,20 @@ def discover(self): for _, ts in self.testsuites.items(): self.scenarios.append(ts.id) + if self.options.test_pattern: + logger.info( + "Running tests matching the specified patterns: %s", + ', '.join(self.options.test_pattern) + ) + # If test_pattern is used, we run all tests that match the pattern. + # The pattern can be a regular expression. + self.run_individual_testsuite = [] + for pattern in self.options.test_pattern: + r = re.compile(pattern) + for ts in self.testsuites.values(): + if r.search(ts.id): + self.run_individual_testsuite.append(ts.id) + self.report_duplicates() self.parse_configuration(config_file=self.env.test_config) diff --git a/scripts/tests/twister/test_testplan.py b/scripts/tests/twister/test_testplan.py index 6fcde49159d7c..4b6b99b804f50 100644 --- a/scripts/tests/twister/test_testplan.py +++ b/scripts/tests/twister/test_testplan.py @@ -563,6 +563,7 @@ def test_testplan_discover( testplan = TestPlan(env=mock.Mock()) testplan.options = mock.Mock( + test_pattern=[], test='ts1', quarantine_list=[tmp_path / qf for qf in ql], quarantine_verify=qv,