Skip to content

Commit

Permalink
Move get_current_test
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Aug 7, 2022
1 parent 18900dc commit f99cee1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
41 changes: 2 additions & 39 deletions 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

Expand All @@ -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__)
Expand Down Expand Up @@ -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
Expand All @@ -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}"

Expand Down
41 changes: 41 additions & 0 deletions 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 "",
)

0 comments on commit f99cee1

Please sign in to comment.