-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
609 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# | ||
# Copyright (c) 2023 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
from test_collections.sdk_tests.support.python_testing.models.python_test_folder import ( | ||
PythonTestFolder, | ||
) | ||
|
||
test_python_path = Path("/test/python") | ||
|
||
|
||
def test_python_folder_version() -> None: | ||
version_file_content = "python_test_version" | ||
|
||
# We mock open to read version_file_content and Path exists to ignore that we're | ||
# testing with a fake path | ||
with mock.patch( | ||
"test_collections.sdk_tests.support.python_testing.models.python_test_folder.open", | ||
new=mock.mock_open(read_data=version_file_content), | ||
), mock.patch.object(target=Path, attribute="exists", return_value=True) as _: | ||
python_test_folder = PythonTestFolder(test_python_path) | ||
|
||
assert python_test_folder.version == version_file_content | ||
|
||
|
||
def test_python_folder_version_missing() -> None: | ||
expected_version = "Unknown" | ||
with mock.patch.object(target=Path, attribute="exists", return_value=False) as _: | ||
python_folder = PythonTestFolder(test_python_path) | ||
assert python_folder.version == expected_version | ||
|
||
|
||
def test_python_folder_filename_pattern() -> None: | ||
"""Test PythonTestFolder will search for files with filename pattern.""" | ||
with mock.patch.object(target=Path, attribute="glob") as path_glob: | ||
# Default file_name_patter: * | ||
python_folder = PythonTestFolder(test_python_path) | ||
_ = python_folder.python_file_paths() | ||
path_glob.assert_called_once_with("*.py") | ||
|
||
path_glob.reset_mock() | ||
pattern = "TC_*" | ||
python_test_folder = PythonTestFolder(test_python_path, filename_pattern=pattern) | ||
_ = python_test_folder.python_file_paths() | ||
path_glob.assert_called_once_with(f"{pattern}.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Copyright (c) 2023 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
from test_collections.sdk_tests.support.python_testing.models.python_test_parser import ( | ||
PythonParserException, | ||
parse_python_test, | ||
) | ||
|
||
sample_invalid_python_file_content = """ | ||
class TC_Sample(MatterBaseTest): | ||
def steps_TC_Sample(self) -> list[TestStep]: | ||
steps = [ | ||
TestStep(1, "Commissioning, already done", is_commissioning=True), | ||
TestStep(2, "Second step"), | ||
TestStep(3, "Third step"), | ||
] | ||
return steps | ||
@async_test_body | ||
async def test_steps_TC_Sample(self): | ||
print("Test execution") | ||
""" | ||
|
||
sample_python_file_content = """ | ||
class TC_Sample(MatterBaseTest): | ||
def desc_TC_Sample(self) -> str: | ||
return "Sample TC Description" | ||
def steps_TC_Sample(self) -> list[TestStep]: | ||
steps = [ | ||
TestStep(1, "Commissioning, already done", is_commissioning=True), | ||
TestStep(2, "Second step"), | ||
TestStep(3, "Third step"), | ||
] | ||
return steps | ||
@async_test_body | ||
async def test_steps_TC_Sample(self): | ||
print("Test execution") | ||
""" | ||
|
||
|
||
def test_python_file_parser_throws_pythonparserexception() -> None: | ||
file_path = Path("/test/file.py") | ||
|
||
with mock.patch( | ||
"test_collections.sdk_tests.support.python_testing.models.python_test_parser.open", | ||
new=mock.mock_open(read_data=sample_invalid_python_file_content), | ||
): | ||
try: | ||
parse_python_test(file_path) | ||
except PythonParserException as e: | ||
assert "Test Case file does not have methods desc_file or steps_file" == str(e) | ||
|
||
|
||
def test_python_file_parser() -> None: | ||
file_path = Path("/test/file.py") | ||
|
||
# We mock builtin `open` method to read sample python file content, | ||
# to avoid having to load a real file. | ||
with mock.patch( | ||
"test_collections.sdk_tests.support.python_testing.models.python_test_parser.open", | ||
new=mock.mock_open(read_data=sample_python_file_content), | ||
) as file_open: | ||
test = parse_python_test(file_path) | ||
|
||
file_open.assert_called_once_with(file_path, "r") | ||
assert test.path == file_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
unit-test-python-version |
51 changes: 51 additions & 0 deletions
51
app/tests/python_tests/test_python_script/UnitTest_TC_ACL_1_1_Automated.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
# | ||
# Copyright (c) 2023 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# Copyright (c) 2021 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# type: ignore | ||
# Ignore mypy type check for this file | ||
|
||
""" | ||
This is just a sample test case that should come from SDK. | ||
It should not compile or run. | ||
""" | ||
class TC_Sample(MatterBaseTest): | ||
|
||
def desc_TC_Sample(self) -> str: | ||
return "Sample TC Description" | ||
|
||
def steps_TC_Sample(self) -> list[TestStep]: | ||
steps = [ | ||
TestStep(1, "Commissioning, already done", is_commissioning=True), | ||
TestStep(2, "Second step"), | ||
TestStep(3, "Third step"), | ||
] | ||
return steps | ||
|
||
@async_test_body | ||
async def test_steps_TC_Sample(self): | ||
print("Test execution") |
Oops, something went wrong.