From 5edc356d96864e6544eb31bda3e85961aca125d2 Mon Sep 17 00:00:00 2001 From: Declan Snyder Date: Wed, 17 Apr 2024 17:10:37 -0500 Subject: [PATCH] twister: Make --list-tests respect tag filter Make the --list-tests and --tests-tree options outputs respect the --tag and --exclude-tag options, so that only the tests for the specified tags are listed. Also update the TestPlan report testcases for this change. Signed-off-by: Declan Snyder --- scripts/pylib/twister/twisterlib/testplan.py | 31 ++++++++++++++++---- scripts/tests/twister/test_testplan.py | 4 +-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/scripts/pylib/twister/twisterlib/testplan.py b/scripts/pylib/twister/twisterlib/testplan.py index 1ef5dce7f4..e8d1c384e3 100755 --- a/scripts/pylib/twister/twisterlib/testplan.py +++ b/scripts/pylib/twister/twisterlib/testplan.py @@ -349,13 +349,13 @@ class TestPlan: print("- {}".format(t)) def report_test_tree(self): - all_tests = self.get_all_tests() + tests_list = self.get_tests_list() testsuite = Node("Testsuite") samples = Node("Samples", parent=testsuite) tests = Node("Tests", parent=testsuite) - for test in sorted(all_tests): + for test in sorted(tests_list): if test.startswith("sample."): sec = test.split(".") area = find(samples, lambda node: node.name == sec[1] and node.parent == samples) @@ -379,13 +379,12 @@ class TestPlan: print("%s%s" % (pre, node.name)) def report_test_list(self): - cnt = 0 - all_tests = self.get_all_tests() + tests_list = self.get_tests_list() - for test in sorted(all_tests): + cnt = 0 + for test in sorted(tests_list): cnt = cnt + 1 print(" - {}".format(test)) - print("{} total.".format(cnt)) def config(self): @@ -481,6 +480,26 @@ class TestPlan: return testcases + def get_tests_list(self): + testcases = [] + if tag_filter := self.options.tag: + for _, ts in self.testsuites.items(): + if ts.tags.intersection(tag_filter): + for case in ts.testcases: + testcases.append(case.name) + else: + for _, ts in self.testsuites.items(): + for case in ts.testcases: + testcases.append(case.name) + + if exclude_tag := self.options.exclude_tag: + for _, ts in self.testsuites.items(): + if ts.tags.intersection(exclude_tag): + for case in ts.testcases: + if case.name in testcases: + testcases.remove(case.name) + return testcases + def add_testsuites(self, testsuite_filter=[]): for root in self.env.test_roots: root = os.path.abspath(root) diff --git a/scripts/tests/twister/test_testplan.py b/scripts/tests/twister/test_testplan.py index a069c899a7..600cde4c1a 100644 --- a/scripts/tests/twister/test_testplan.py +++ b/scripts/tests/twister/test_testplan.py @@ -973,7 +973,7 @@ def test_testplan_report_tag_list(capfd): def test_testplan_report_test_tree(capfd): testplan = TestPlan(env=mock.Mock()) - testplan.get_all_tests = mock.Mock( + testplan.get_tests_list = mock.Mock( return_value=['1.dummy.case.1', '1.dummy.case.2', '2.dummy.case.1', '2.dummy.case.2', '3.dummy.case.1', '3.dummy.case.2', @@ -1031,7 +1031,7 @@ Testsuite def test_testplan_report_test_list(capfd): testplan = TestPlan(env=mock.Mock()) - testplan.get_all_tests = mock.Mock( + testplan.get_tests_list = mock.Mock( return_value=['4.dummy.case.1', '4.dummy.case.2', '3.dummy.case.2', '2.dummy.case.2', '1.dummy.case.1', '1.dummy.case.2',