Extract `getfixturedefs` compatibility function
This commit is contained in:
parent
cc86d99763
commit
67d708f6f6
|
@ -552,4 +552,4 @@ test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = ">=3.8"
|
python-versions = ">=3.8"
|
||||||
content-hash = "5b7935aa2b2d579d4fdb8361b414d37fff142a359332d859ed1513209ca3b1a6"
|
content-hash = "b40d47067f444deec4964404014795593f1b602f8a2f6376279bb5a27d5e18be"
|
||||||
|
|
|
@ -40,6 +40,7 @@ parse = "*"
|
||||||
parse-type = "*"
|
parse-type = "*"
|
||||||
pytest = ">=6.2.0"
|
pytest = ">=6.2.0"
|
||||||
typing-extensions = "*"
|
typing-extensions = "*"
|
||||||
|
packaging = "*"
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
tox = ">=4.11.3"
|
tox = ">=4.11.3"
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from importlib.metadata import version
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from _pytest.fixtures import FixtureDef, FixtureManager
|
||||||
|
from _pytest.nodes import Node
|
||||||
|
from packaging.version import Version
|
||||||
|
from packaging.version import parse as parse_version
|
||||||
|
|
||||||
|
pytest_version = parse_version(version("pytest"))
|
||||||
|
|
||||||
|
|
||||||
|
if pytest_version >= Version("8.1"):
|
||||||
|
|
||||||
|
def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Optional[Sequence[FixtureDef]]:
|
||||||
|
return fixturemanager.getfixturedefs(fixturename, node)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Optional[Sequence[FixtureDef]]:
|
||||||
|
return fixturemanager.getfixturedefs(fixturename, node.nodeid)
|
|
@ -5,10 +5,10 @@ import itertools
|
||||||
import os.path
|
import os.path
|
||||||
from typing import TYPE_CHECKING, cast
|
from typing import TYPE_CHECKING, cast
|
||||||
|
|
||||||
import pytest
|
|
||||||
from _pytest._io import TerminalWriter
|
from _pytest._io import TerminalWriter
|
||||||
from mako.lookup import TemplateLookup
|
from mako.lookup import TemplateLookup
|
||||||
|
|
||||||
|
from .compat import getfixturedefs
|
||||||
from .feature import get_features
|
from .feature import get_features
|
||||||
from .scenario import inject_fixturedefs_for_step, make_python_docstring, make_python_name, make_string_literal
|
from .scenario import inject_fixturedefs_for_step, make_python_docstring, make_python_name, make_string_literal
|
||||||
from .steps import get_step_fixture_name
|
from .steps import get_step_fixture_name
|
||||||
|
@ -130,10 +130,7 @@ def _find_step_fixturedef(
|
||||||
"""Find step fixturedef."""
|
"""Find step fixturedef."""
|
||||||
with inject_fixturedefs_for_step(step=step, fixturemanager=fixturemanager, node=item):
|
with inject_fixturedefs_for_step(step=step, fixturemanager=fixturemanager, node=item):
|
||||||
bdd_name = get_step_fixture_name(step=step)
|
bdd_name = get_step_fixture_name(step=step)
|
||||||
if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (8, 1):
|
return getfixturedefs(fixturemanager, bdd_name, item)
|
||||||
return fixturemanager.getfixturedefs(bdd_name, item)
|
|
||||||
else:
|
|
||||||
return fixturemanager.getfixturedefs(bdd_name, item.nodeid)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_feature_files(paths: list[str], **kwargs: Any) -> tuple[list[Feature], list[ScenarioTemplate], list[Step]]:
|
def parse_feature_files(paths: list[str], **kwargs: Any) -> tuple[list[Feature], list[ScenarioTemplate], list[Step]]:
|
||||||
|
|
|
@ -23,12 +23,14 @@ from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest, call_fi
|
||||||
from typing_extensions import ParamSpec
|
from typing_extensions import ParamSpec
|
||||||
|
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
|
from .compat import getfixturedefs
|
||||||
from .feature import get_feature, get_features
|
from .feature import get_feature, get_features
|
||||||
from .steps import StepFunctionContext, get_step_fixture_name, inject_fixture
|
from .steps import StepFunctionContext, get_step_fixture_name, inject_fixture
|
||||||
from .utils import CONFIG_STACK, get_args, get_caller_module_locals, get_caller_module_path
|
from .utils import CONFIG_STACK, get_args, get_caller_module_locals, get_caller_module_path
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from _pytest.mark.structures import ParameterSet
|
from _pytest.mark.structures import ParameterSet
|
||||||
|
from _pytest.nodes import Node
|
||||||
|
|
||||||
from .parser import Feature, Scenario, ScenarioTemplate, Step
|
from .parser import Feature, Scenario, ScenarioTemplate, Step
|
||||||
|
|
||||||
|
@ -42,7 +44,7 @@ PYTHON_REPLACE_REGEX = re.compile(r"\W")
|
||||||
ALPHA_REGEX = re.compile(r"^\d+_*")
|
ALPHA_REGEX = re.compile(r"^\d+_*")
|
||||||
|
|
||||||
|
|
||||||
def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node) -> Iterable[FixtureDef[Any]]:
|
def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node: Node) -> Iterable[FixtureDef[Any]]:
|
||||||
"""Find the fixture defs that can parse a step."""
|
"""Find the fixture defs that can parse a step."""
|
||||||
# happens to be that _arg2fixturedefs is changed during the iteration so we use a copy
|
# happens to be that _arg2fixturedefs is changed during the iteration so we use a copy
|
||||||
fixture_def_by_name = list(fixturemanager._arg2fixturedefs.items())
|
fixture_def_by_name = list(fixturemanager._arg2fixturedefs.items())
|
||||||
|
@ -59,10 +61,7 @@ def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node)
|
||||||
if not match:
|
if not match:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (8, 1):
|
fixturedefs = getfixturedefs(fixturemanager, fixturename, node)
|
||||||
fixturedefs = fixturemanager.getfixturedefs(fixturename, node)
|
|
||||||
else:
|
|
||||||
fixturedefs = fixturemanager.getfixturedefs(fixturename, node.nodeid)
|
|
||||||
if fixturedef not in (fixturedefs or []):
|
if fixturedef not in (fixturedefs or []):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -117,7 +116,7 @@ def iterparentnodeids(nodeid: str) -> Iterator[str]:
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def inject_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node) -> Iterator[None]:
|
def inject_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node: Node) -> Iterator[None]:
|
||||||
"""Inject fixture definitions that can parse a step.
|
"""Inject fixture definitions that can parse a step.
|
||||||
|
|
||||||
We fist iterate over all the fixturedefs that can parse the step.
|
We fist iterate over all the fixturedefs that can parse the step.
|
||||||
|
|
Loading…
Reference in New Issue