Skip to content

Commit

Permalink
Merge branch 'master' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Mar 17, 2024
2 parents 9081a08 + 977bf61 commit c64915c
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 268 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Expand Up @@ -43,7 +43,7 @@ jobs:

- name: Install poetry
run: |
python -m pip install poetry==1.6.1
python -m pip install poetry==1.8.2
- name: Configure poetry
run: |
Expand Down
11 changes: 11 additions & 0 deletions CHANGES.rst
Expand Up @@ -3,6 +3,17 @@ Changelog

Unreleased
----------

7.1.2
----------
- Address another compatibility issue with pytest 8.1 (fixture registration). `#680 <https://github.com/pytest-dev/pytest-bdd/pull/680>`_

7.1.1
----------
- Address a bug introduced in pytest-bdd 7.1 caused by incorrect pytest version check.

7.1
----------
- Address compatibility issue with pytest 8.1. `#666 <https://github.com/pytest-dev/pytest-bdd/pull/666>`_

7.0.1
Expand Down
12 changes: 6 additions & 6 deletions README.rst
@@ -1,15 +1,15 @@
BDD library for the pytest runner
=================================

.. image:: http://img.shields.io/pypi/v/pytest-bdd.svg
.. image:: https://img.shields.io/pypi/v/pytest-bdd.svg
:target: https://pypi.python.org/pypi/pytest-bdd
.. image:: https://codecov.io/gh/pytest-dev/pytest-bdd/branch/master/graph/badge.svg
:target: https://codecov.io/gh/pytest-dev/pytest-bdd
.. image:: https://travis-ci.org/pytest-dev/pytest-bdd.svg?branch=master
:target: https://travis-ci.org/pytest-dev/pytest-bdd
:target: https://codecov.io/gh/pytest-dev/pytest-bdd
.. image:: https://github.com/pytest-dev/pytest-bdd/actions/workflows/main.yml/badge.svg
:target: https://github.com/pytest-dev/pytest-bdd/actions/workflows/main.yml
.. image:: https://readthedocs.org/projects/pytest-bdd/badge/?version=stable
:target: https://readthedocs.org/projects/pytest-bdd/
:alt: Documentation Status
:target: https://readthedocs.org/projects/pytest-bdd/
:alt: Documentation Status

pytest-bdd implements a subset of the Gherkin language to enable automating project
requirements testing and to facilitate behavioral driven development.
Expand Down
424 changes: 213 additions & 211 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-bdd"
version = "7.0.1"
version = "7.1.2"
description = "BDD for pytest"
authors = ["Oleg Pidsadnyi <oleg.pidsadnyi@gmail.com>", "Anatoly Bubenkov <bubenkoff@gmail.com>"]
maintainers = ["Alessio Bogon <778703+youtux@users.noreply.github.com>"]
Expand Down
60 changes: 57 additions & 3 deletions src/pytest_bdd/compat.py
Expand Up @@ -2,21 +2,75 @@

from collections.abc import Sequence
from importlib.metadata import version
from typing import Any

from _pytest.fixtures import FixtureDef, FixtureManager
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest
from _pytest.nodes import Node
from packaging.version import Version
from packaging.version import parse as parse_version

pytest_version = parse_version(version("pytest"))

__all__ = ["getfixturedefs", "inject_fixture"]

if pytest_version >= Version("8.1"):
if pytest_version.release >= (8, 1):

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Sequence[FixtureDef] | None:
return fixturemanager.getfixturedefs(fixturename, node)

def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None:
"""Inject fixture into pytest fixture request.
:param request: pytest fixture request
:param arg: argument name
:param value: argument value
"""

request._fixturemanager._register_fixture(
name=arg,
func=lambda: value,
nodeid=request.node.nodeid,
)

else:

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Sequence[FixtureDef] | None:
return fixturemanager.getfixturedefs(fixturename, node.nodeid)

def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None:
"""Inject fixture into pytest fixture request.
:param request: pytest fixture request
:param arg: argument name
:param value: argument value
"""
fd = FixtureDef(
fixturemanager=request._fixturemanager,
baseid=None,
argname=arg,
func=lambda: value,
scope="function",
params=None,
)
fd.cached_result = (value, 0, None)

old_fd = request._fixture_defs.get(arg)
add_fixturename = arg not in request.fixturenames

def fin() -> None:
request._fixturemanager._arg2fixturedefs[arg].remove(fd)

if old_fd is not None:
request._fixture_defs[arg] = old_fd

if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.remove(arg)

request.addfinalizer(fin)

# inject fixture definition
request._fixturemanager._arg2fixturedefs.setdefault(arg, []).append(fd)

# inject fixture value in request cache
request._fixture_defs[arg] = fd
if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.append(arg)
4 changes: 2 additions & 2 deletions src/pytest_bdd/scenario.py
Expand Up @@ -24,9 +24,9 @@
from typing_extensions import ParamSpec

from . import exceptions
from .compat import getfixturedefs
from .compat import getfixturedefs, inject_fixture
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
from .utils import CONFIG_STACK, get_args, get_caller_module_locals, get_caller_module_path

if TYPE_CHECKING:
Expand Down
44 changes: 2 additions & 42 deletions src/pytest_bdd/steps.py
Expand Up @@ -43,9 +43,10 @@ def _(article):
from typing import Any, Callable, Iterable, Literal, TypeVar

import pytest
from _pytest.fixtures import FixtureDef, FixtureRequest
from _pytest.fixtures import FixtureRequest
from typing_extensions import ParamSpec

from . import compat
from .parser import Step
from .parsers import StepParser, get_parser
from .types import GIVEN, THEN, WHEN
Expand Down Expand Up @@ -201,44 +202,3 @@ def find_unique_name(name: str, seen: Iterable[str]) -> str:
new_name = f"{name}_{i}"
if new_name not in seen:
return new_name


def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None:
"""Inject fixture into pytest fixture request.
:param request: pytest fixture request
:param arg: argument name
:param value: argument value
"""

fd = FixtureDef(
fixturemanager=request._fixturemanager,
baseid=None,
argname=arg,
func=lambda: value,
scope="function",
params=None,
)
fd.cached_result = (value, 0, None)

old_fd = request._fixture_defs.get(arg)
add_fixturename = arg not in request.fixturenames

def fin() -> None:
request._fixturemanager._arg2fixturedefs[arg].remove(fd)

if old_fd is not None:
request._fixture_defs[arg] = old_fd

if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.remove(arg)

request.addfinalizer(fin)

# inject fixture definition
request._fixturemanager._arg2fixturedefs.setdefault(arg, []).append(fd)

# inject fixture value in request cache
request._fixture_defs[arg] = fd
if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.append(arg)
6 changes: 4 additions & 2 deletions tox.ini
@@ -1,7 +1,7 @@
[tox]
distshare = {homedir}/.tox/distshare
envlist = py{3.8,3.9,3.10,3.11}-pytest{6.2,7.0,7.1,7.2,7.3,7.4,latest}-coverage
py{3.12,3.13}-pytest{7.3,7.4,latest}-coverage
envlist = py{3.8,3.9,3.10,3.11}-pytest{6.2,7.0,7.1,7.2,7.3,7.4,8.0,8.1,latest}-coverage
py{3.12,3.13}-pytest{7.3,7.4,8.0,8.1,latest}-coverage
py3.12-pytestlatest-xdist-coverage
mypy

Expand All @@ -12,6 +12,8 @@ setenv =
xdist: _PYTEST_MORE_ARGS=-n3 -rfsxX
deps =
pytestlatest: pytest
pytest8.1: pytest~=8.1.0
pytest8.0: pytest~=8.0.0
pytest7.4: pytest~=7.4.0
pytest7.3: pytest~=7.3.0
pytest7.2: pytest~=7.2.0
Expand Down

0 comments on commit c64915c

Please sign in to comment.