Skip to content

Commit f3202b1

Browse files
authored
Move MockTestRunner to chip testing package (project-chip#37170)
* Move MockTestRunner to chip testing package * Fix script dir path * Remove unused import
1 parent 2b020e0 commit f3202b1

File tree

10 files changed

+47
-34
lines changed

10 files changed

+47
-34
lines changed

src/python_testing/matter_testing_infrastructure/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pw_python_package("chip-testing-module") {
4343
"chip/testing/matter_testing.py",
4444
"chip/testing/metadata.py",
4545
"chip/testing/pics.py",
46+
"chip/testing/runner.py",
4647
"chip/testing/spec_parsing.py",
4748
"chip/testing/taglist_and_topology_test.py",
4849
"chip/testing/tasks.py",

src/python_testing/test_testing/MockTestRunner.py src/python_testing/matter_testing_infrastructure/chip/testing/runner.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import asyncio
1919
import importlib
20-
import os
2120
import sys
2221
from pathlib import Path
2322
from unittest.mock import MagicMock
@@ -33,11 +32,12 @@ async def __call__(self, *args, **kwargs):
3332

3433
class MockTestRunner():
3534

36-
def __init__(self, filename: str, classname: str, test: str, endpoint: int = None, pics: dict[str, bool] = None, paa_trust_store_path=None):
35+
def __init__(self, abs_filename: str, classname: str, test: str, endpoint: int = None,
36+
pics: dict[str, bool] = None, paa_trust_store_path=None):
3737
self.kvs_storage = 'kvs_admin.json'
3838
self.config = MatterTestConfig(endpoint=endpoint, paa_trust_store_path=paa_trust_store_path,
3939
pics=pics, storage_path=self.kvs_storage)
40-
self.set_test(filename, classname, test)
40+
self.set_test(abs_filename, classname, test)
4141

4242
self.set_test_config(self.config)
4343

@@ -48,17 +48,16 @@ def __init__(self, filename: str, classname: str, test: str, endpoint: int = Non
4848
catTags=self.config.controller_cat_tags
4949
)
5050

51-
def set_test(self, filename: str, classname: str, test: str):
51+
def set_test(self, abs_filename: str, classname: str, test: str):
5252
self.test = test
5353
self.config.tests = [self.test]
5454

55-
module_name = Path(os.path.basename(filename)).stem
56-
5755
try:
58-
module = importlib.import_module(module_name)
56+
filename_path = Path(abs_filename)
57+
module = importlib.import_module(filename_path.stem)
5958
except ModuleNotFoundError:
60-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
61-
module = importlib.import_module(module_name)
59+
sys.path.append(str(filename_path.parent.resolve()))
60+
module = importlib.import_module(filename_path.stem)
6261

6362
self.test_class = getattr(module, classname)
6463

@@ -72,7 +71,7 @@ def set_test_config(self, test_config: MatterTestConfig = MatterTestConfig()):
7271
def Shutdown(self):
7372
self.stack.Shutdown()
7473

75-
def run_test_with_mock_read(self, read_cache: Attribute.AsyncReadTransaction.ReadResponse, hooks=None):
74+
def run_test_with_mock_read(self, read_cache: Attribute.AsyncReadTransaction.ReadResponse, hooks=None):
7675
self.default_controller.Read = AsyncMock(return_value=read_cache)
7776
# This doesn't need to do anything since we are overriding the read anyway
7877
self.default_controller.FindOrEstablishPASESession = AsyncMock(return_value=None)

src/python_testing/test_testing/TestDecorators.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
# You will get step_* calls as appropriate in between the test_start and test_stop calls if the test is not skipped.
2727

2828
import sys
29+
from pathlib import Path
2930
from typing import Optional
3031

3132
import chip.clusters as Clusters
3233
from chip.clusters import Attribute
3334
from chip.testing.matter_testing import (MatterBaseTest, MatterTestConfig, async_test_body, has_attribute, has_cluster, has_feature,
3435
run_if_endpoint_matches, run_on_singleton_matching_endpoint, should_run_test_on_endpoint)
36+
from chip.testing.runner import MockTestRunner
3537
from mobly import asserts
36-
from MockTestRunner import MockTestRunner
3738

3839

3940
def get_clusters(endpoints: list[int]) -> Attribute.AsyncReadTransaction.ReadResponse:
@@ -216,7 +217,8 @@ async def test_no_run_on_singleton_matching_endpoint(self):
216217
def main():
217218
failures = []
218219
hooks = DecoratorTestRunnerHooks()
219-
test_runner = MockTestRunner('TestDecorators.py', 'TestDecorators', 'test_checkers')
220+
test_runner = MockTestRunner(Path(__file__).parent / 'TestDecorators.py',
221+
'TestDecorators', 'test_checkers')
220222
read_resp = get_clusters([0, 1])
221223
ok = test_runner.run_test_with_mock_read(read_resp, hooks)
222224
if not ok:

src/python_testing/test_testing/common_icdm_data.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
import string
2020
from dataclasses import dataclass
21+
from pathlib import Path
2122

2223
import chip.clusters as Clusters
2324
from chip.clusters import Attribute
24-
from MockTestRunner import MockTestRunner
25+
from chip.testing.runner import MockTestRunner
2526

2627
c = Clusters.IcdManagement
2728
attr = c.Attributes
@@ -54,8 +55,8 @@ def test_spec_to_attribute_cache(test_icdm: ICDMData) -> Attribute.AsyncReadTran
5455

5556

5657
def run_tests(pics, label, test_cases, test_name):
57-
test_runner = MockTestRunner(
58-
label, label, test_name, 0, pics)
58+
test_runner = MockTestRunner(Path(__file__).parent / f"../{label}.py",
59+
label, test_name, 0, pics)
5960
failures = []
6061
for idx, t in enumerate(test_cases):
6162
ok = test_runner.run_test_with_mock_read(

src/python_testing/test_testing/test_IDM_10_4.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
# limitations under the License.
1717
#
1818

19-
import os
2019
import sys
20+
from pathlib import Path
2121

2222
import chip.clusters as Clusters
2323
from chip.clusters import Attribute
2424
from chip.testing.pics import parse_pics_xml
25-
from MockTestRunner import MockTestRunner
25+
from chip.testing.runner import MockTestRunner
2626

2727
# Reachable attribute is off in the pics file
2828
# MaxPathsPerInvoke is not include in the pics file
@@ -80,10 +80,12 @@ def create_read(include_reachable: bool = False, include_max_paths: bool = False
8080

8181
def main():
8282
# TODO: add the same test for commands and features
83-
script_dir = os.path.dirname(os.path.realpath(__file__))
84-
with open(f'{script_dir}/example_pics_xml_basic_info.xml') as f:
83+
84+
script_dir = Path(__file__).resolve().parent
85+
with open(script_dir / 'example_pics_xml_basic_info.xml') as f:
8586
pics = parse_pics_xml(f.read())
86-
test_runner = MockTestRunner('TC_pics_checker.py', 'TC_PICS_Checker', 'test_TC_IDM_10_4', 0, pics)
87+
test_runner = MockTestRunner(script_dir / '../TC_pics_checker.py',
88+
'TC_PICS_Checker', 'test_TC_IDM_10_4', 0, pics)
8789
failures = []
8890

8991
# Success, include vendor ID, which IS in the pics file, and neither of the incorrect ones

src/python_testing/test_testing/test_TC_CCNTL_2_2.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import asyncio
2020
import base64
2121
import os
22-
import pathlib
2322
import sys
2423
import typing
24+
from pathlib import Path
2525

2626
import chip.clusters as Clusters
2727
import click
2828
from chip import ChipDeviceCtrl
2929
from chip.clusters import Attribute
3030
from chip.interaction_model import InteractionModelError, Status
31-
from MockTestRunner import AsyncMock, MockTestRunner
31+
from chip.testing.runner import AsyncMock, MockTestRunner
3232

3333
try:
3434
from chip.testing.matter_testing import MatterTestConfig, get_default_paa_trust_store, run_tests_no_exit
@@ -175,13 +175,14 @@ def run_test_with_mock(self, dynamic_invoke_return: typing.Callable, dynamic_eve
175175
@click.command()
176176
@click.argument('th_server_app', type=click.Path(exists=True))
177177
def main(th_server_app: str):
178-
root = os.path.abspath(os.path.join(pathlib.Path(__file__).resolve().parent, '..', '..', '..'))
178+
root = os.path.abspath(os.path.join(Path(__file__).resolve().parent, '..', '..', '..'))
179179
print(f'root = {root}')
180180
paa_path = get_default_paa_trust_store(root)
181181
print(f'paa = {paa_path}')
182182

183183
pics = {"PICS_SDK_CI_ONLY": True}
184-
test_runner = MyMock('TC_CCTRL_2_2', 'TC_CCTRL_2_2', 'test_TC_CCTRL_2_2', paa_trust_store_path=paa_path, pics=pics)
184+
test_runner = MyMock(Path(__file__).parent / '../TC_CCTRL_2_2.py',
185+
'TC_CCTRL_2_2', 'test_TC_CCTRL_2_2', paa_trust_store_path=paa_path, pics=pics)
185186
config = MatterTestConfig()
186187
config.global_test_params = {'th_server_app_path': th_server_app}
187188
test_runner.set_test_config(config)

src/python_testing/test_testing/test_TC_DGGEN_3_2.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
import sys
2020
from dataclasses import dataclass
21+
from pathlib import Path
2122

2223
import chip.clusters as Clusters
2324
from chip.clusters import Attribute
24-
from MockTestRunner import MockTestRunner
25+
from chip.testing.runner import MockTestRunner
2526

2627

2728
@dataclass
@@ -51,7 +52,8 @@ def test_spec_to_attribute_cache(test_spec: TestSpec) -> Attribute.AsyncReadTran
5152

5253

5354
def main():
54-
test_runner = MockTestRunner('TC_DGGEN_3_2', 'TC_DGGEN_3_2', 'test_TC_DGGEN_3_2', 0)
55+
test_runner = MockTestRunner(Path(__file__).parent / '../TC_DGGEN_3_2.py',
56+
'TC_DGGEN_3_2', 'test_TC_DGGEN_3_2', 0)
5557
failures = []
5658
for idx, t in enumerate(TEST_CASES):
5759
ok = test_runner.run_test_with_mock_read(test_spec_to_attribute_cache(t)) == t.expect_pass

src/python_testing/test_testing/test_TC_MCORE_FS_1_1.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import asyncio
2020
import base64
2121
import os
22-
import pathlib
2322
import sys
2423
import typing
24+
from pathlib import Path
2525

2626
import chip.clusters as Clusters
2727
import click
2828
from chip import ChipDeviceCtrl
2929
from chip.clusters import Attribute
3030
from chip.interaction_model import InteractionModelError, Status
31-
from MockTestRunner import AsyncMock, MockTestRunner
31+
from chip.testing.runner import AsyncMock, MockTestRunner
3232

3333
try:
3434
from chip.testing.matter_testing import MatterTestConfig, get_default_paa_trust_store, run_tests_no_exit
@@ -146,13 +146,14 @@ def run_test_with_mock(self, dynamic_invoke_return: typing.Callable, dynamic_eve
146146
@click.command()
147147
@click.argument('th_server_app', type=click.Path(exists=True))
148148
def main(th_server_app: str):
149-
root = os.path.abspath(os.path.join(pathlib.Path(__file__).resolve().parent, '..', '..', '..'))
149+
root = os.path.abspath(os.path.join(Path(__file__).resolve().parent, '..', '..', '..'))
150150
print(f'root = {root}')
151151
paa_path = get_default_paa_trust_store(root)
152152
print(f'paa = {paa_path}')
153153

154154
pics = {"PICS_SDK_CI_ONLY": True}
155-
test_runner = MyMock('TC_MCORE_FS_1_1', 'TC_MCORE_FS_1_1', 'test_TC_MCORE_FS_1_1', paa_trust_store_path=paa_path, pics=pics)
155+
test_runner = MyMock(Path(__file__).parent / '../TC_MCORE_FS_1_1.py',
156+
'TC_MCORE_FS_1_1', 'test_TC_MCORE_FS_1_1', paa_trust_store_path=paa_path, pics=pics)
156157
config = MatterTestConfig()
157158
config.user_params = {'th_server_app_path': th_server_app}
158159
test_runner.set_test_config(config)

src/python_testing/test_testing/test_TC_SC_7_1.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
#
1818

1919
import sys
20+
from pathlib import Path
2021
from random import randbytes
2122

2223
import chip.clusters as Clusters
2324
from chip.clusters import Attribute
2425
from chip.testing.matter_testing import MatterTestConfig
25-
from MockTestRunner import MockTestRunner
26+
from chip.testing.runner import MockTestRunner
2627

2728

2829
def read_trusted_root(filled: bool) -> Attribute.AsyncReadTransaction.ReadResponse:
@@ -43,7 +44,8 @@ def main():
4344
manual_3333_20202021 = '31693312339'
4445
manual_2222_20202024 = '20055212333'
4546

46-
test_runner = MockTestRunner('TC_SC_7_1', 'TC_SC_7_1', 'test_TC_SC_7_1', 0)
47+
test_runner = MockTestRunner(Path(__file__).parent / '../TC_SC_7_1.py',
48+
'TC_SC_7_1', 'test_TC_SC_7_1', 0)
4749
failures = []
4850

4951
# Tests with no code specified should fail

src/python_testing/test_testing/test_TC_TMP_2_1.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import sys
2020
import typing
2121
from dataclasses import dataclass
22+
from pathlib import Path
2223

2324
import chip.clusters as Clusters
2425
from chip.clusters import Attribute
2526
from chip.clusters.Types import NullValue
26-
from MockTestRunner import MockTestRunner
27+
from chip.testing.runner import MockTestRunner
2728

2829

2930
@dataclass
@@ -160,7 +161,8 @@ def test_spec_to_attribute_cache(test_spec: TestSpec) -> Attribute.AsyncReadTran
160161

161162

162163
def main():
163-
test_runner = MockTestRunner('TC_TMP_2_1', 'TC_TMP_2_1', 'test_TC_TMP_2_1', 1)
164+
test_runner = MockTestRunner(Path(__file__).parent / '../TC_TMP_2_1.py',
165+
'TC_TMP_2_1', 'test_TC_TMP_2_1', 1)
164166
failures = []
165167
for idx, t in enumerate(TEST_CASES):
166168
ok = test_runner.run_test_with_mock_read(test_spec_to_attribute_cache(t)) == t.expect_pass

0 commit comments

Comments
 (0)