From f99cee17b2849b1ef2ca0b888e22f49a4b7aecd3 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Sun, 7 Aug 2022 22:54:43 +0200 Subject: [PATCH] Move `get_current_test` --- cardano_node_tests/tests/common.py | 41 ++---------------------- cardano_node_tests/utils/pytest_utils.py | 41 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 cardano_node_tests/utils/pytest_utils.py diff --git a/cardano_node_tests/tests/common.py b/cardano_node_tests/tests/common.py index 13264b644..0a6b3a684 100644 --- a/cardano_node_tests/tests/common.py +++ b/cardano_node_tests/tests/common.py @@ -1,10 +1,6 @@ import logging -import os -import re import time -from pathlib import Path from typing import Any -from typing import NamedTuple from typing import Set from typing import Tuple @@ -15,6 +11,7 @@ from cardano_node_tests.utils import cluster_nodes from cardano_node_tests.utils import clusterlib_utils from cardano_node_tests.utils import configuration +from cardano_node_tests.utils import pytest_utils from cardano_node_tests.utils.versions import VERSIONS LOGGER = logging.getLogger(__name__) @@ -92,18 +89,6 @@ EPOCH_STOP_SEC_LEDGER_STATE = -200 -class PytestTest(NamedTuple): - test_function: str - test_file: Path - full: str - test_class: str = "" - test_params: str = "" - stage: str = "" - - def __bool__(self) -> bool: - return bool(self.test_function) - - def hypothesis_settings(max_examples: int = 100) -> Any: # pylint: disable=import-outside-toplevel import hypothesis @@ -118,34 +103,12 @@ def hypothesis_settings(max_examples: int = 100) -> Any: ) -def get_current_test() -> PytestTest: - """Get components (test file, test name, etc.) of current pytest test.""" - curr_test = os.environ.get("PYTEST_CURRENT_TEST") or "" - if not curr_test: - return PytestTest(test_function="", test_file=Path("/nonexistent"), full="") - - reg = re.search( - r"(^.*/test_\w+\.py)(?:::)?(Test\w+)?::(test_\w+)(\[.+\])? *\(?(\w+)?", curr_test - ) - if not reg: - raise AssertionError(f"Failed to match '{curr_test}'") - - return PytestTest( - test_function=reg.group(3), - test_file=Path(reg.group(1)), - full=curr_test, - test_class=reg.group(2) or "", - test_params=reg.group(4) or "", - stage=reg.group(5) or "", - ) - - def get_test_id(cluster_obj: clusterlib.ClusterLib) -> str: """Return unique test ID - function name + assigned cluster instance + random string. Log the test ID into cluster manager log file. """ - curr_test = get_current_test() + curr_test = pytest_utils.get_current_test() rand_str = clusterlib.get_rand_str(3) test_id = f"{curr_test.test_function}_ci{cluster_obj.cluster_id}_{rand_str}" diff --git a/cardano_node_tests/utils/pytest_utils.py b/cardano_node_tests/utils/pytest_utils.py new file mode 100644 index 000000000..c2c5d3f7a --- /dev/null +++ b/cardano_node_tests/utils/pytest_utils.py @@ -0,0 +1,41 @@ +import logging +import os +import re +from pathlib import Path +from typing import NamedTuple + +LOGGER = logging.getLogger(__name__) + + +class PytestTest(NamedTuple): + test_function: str + test_file: Path + full: str + test_class: str = "" + test_params: str = "" + stage: str = "" + + def __bool__(self) -> bool: + return bool(self.test_function) + + +def get_current_test() -> PytestTest: + """Get components (test file, test name, etc.) of current pytest test.""" + curr_test = os.environ.get("PYTEST_CURRENT_TEST") or "" + if not curr_test: + return PytestTest(test_function="", test_file=Path("/nonexistent"), full="") + + reg = re.search( + r"(^.*/test_\w+\.py)(?:::)?(Test\w+)?::(test_\w+)(\[.+\])? *\(?(\w+)?", curr_test + ) + if not reg: + raise AssertionError(f"Failed to match '{curr_test}'") + + return PytestTest( + test_function=reg.group(3), + test_file=Path(reg.group(1)), + full=curr_test, + test_class=reg.group(2) or "", + test_params=reg.group(4) or "", + stage=reg.group(5) or "", + )