From 83b19a2fd9bb18ac996f6ec4d8d701a410665041 Mon Sep 17 00:00:00 2001 From: Carolina Lopes Date: Mon, 11 Dec 2023 12:24:17 +0000 Subject: [PATCH] Apply code review suggestions --- .../models/python_test_parser.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test_collections/sdk_tests/support/python_testing/models/python_test_parser.py b/test_collections/sdk_tests/support/python_testing/models/python_test_parser.py index cbc87564..eac9eeaa 100644 --- a/test_collections/sdk_tests/support/python_testing/models/python_test_parser.py +++ b/test_collections/sdk_tests/support/python_testing/models/python_test_parser.py @@ -15,7 +15,7 @@ # import ast from pathlib import Path -from typing import List +from typing import List, Optional from test_collections.sdk_tests.support.models.matter_test_models import ( MatterTestStep, @@ -78,26 +78,20 @@ def __parse_test_case_from_class( tc_steps = [] tc_pics = [] - try: - desc_method = next(m for m in methods if desc_method_name in m.name) + desc_method = __get_method_by_name(desc_method_name, methods) + if desc_method: tc_desc = desc_method.body[BODY_INDEX].value.value # type: ignore - except StopIteration: # Raised when `next` doesn't find a matching method - pass - try: - steps_method = next(m for m in methods if steps_method_name in m.name) + # If the python test does not implement the steps template method, + # the test case will be presented in UI and the whole test case will be + # executed as one step + steps_method = __get_method_by_name(steps_method_name, methods) + if steps_method: tc_steps = __retrieve_steps(steps_method) - except StopIteration: # Raised when `next` doesn't find a matching method - # If the python test does not implement the steps template method, - # the test case will be presented in UI and the whole test case will be - # executed as one step - pass - try: - pics_method = next(m for m in methods if pics_method_name in m.name) + pics_method = __get_method_by_name(pics_method_name, methods) + if pics_method: tc_pics = __retrieve_pics(pics_method) - except StopIteration: # Raised when `next` doesn't find a matching method - pass return PythonTest( name=tc_name, @@ -110,6 +104,12 @@ def __parse_test_case_from_class( ) +def __get_method_by_name( + name: str, methods: list[ast.FunctionDef] +) -> Optional[ast.FunctionDef]: + return next((m for m in methods if name in m.name), None) + + def __retrieve_steps(method: ast.FunctionDef) -> List[MatterTestStep]: python_steps: List[MatterTestStep] = [] for step in method.body[BODY_INDEX].value.elts: # type: ignore