Skip to content

Commit 5caa29f

Browse files
authored
Merge pull request #827 from Sanjays2402/fix-register-fixture-node-deprecation
fix: avoid deprecated nodeid argument to _register_fixture
2 parents c17dbb2 + e05e4d8 commit 5caa29f

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

‎CHANGES.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Fixed
3434
* Backslashes in datatable and examples table cells are no longer over-quoted. A cell containing a single backslash now reaches the step as a single backslash, matching the Gherkin escaping rules. If you compensated by doubling backslashes in feature files, undo that. `#769 <https://github.com/pytest-dev/pytest-bdd/issues/769>`_
3535
* Made type annotations stronger and removed most of the ``typing.Any`` usages and ``# type: ignore`` annotations. `#658 <https://github.com/pytest-dev/pytest-bdd/pull/658>`_
3636
* Empty docstrings are now correctly forwarded to step functions as an empty string instead of being silently dropped, which previously caused pytest to report a missing ``docstring`` fixture. `#809 <https://github.com/pytest-dev/pytest-bdd/issues/809>`_
37+
* Injecting a ``target_fixture`` no longer emits ``PytestRemovedIn10Warning`` on pytest 9.1+, which deprecated passing ``nodeid`` to ``_register_fixture``. `#823 <https://github.com/pytest-dev/pytest-bdd/issues/823>`_
3738

3839
Security
3940
++++++++

‎src/pytest_bdd/compat.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ def inject_fixture(request: FixtureRequest, arg: str, value: object) -> None:
2525
:param arg: argument name
2626
:param value: argument value
2727
"""
28-
# Ensure there's a fixture definition for the argument
29-
request._fixturemanager._register_fixture(
30-
name=arg,
31-
func=lambda: value,
32-
nodeid=request.node.nodeid,
33-
)
28+
if pytest_version.release >= (9, 1):
29+
request._fixturemanager._register_fixture(
30+
name=arg,
31+
func=lambda: value,
32+
node=request.node,
33+
)
34+
else:
35+
request._fixturemanager._register_fixture(
36+
name=arg,
37+
func=lambda: value,
38+
nodeid=request.node.nodeid,
39+
)
3440
# Note the fixture we just registered will have a lower priority
3541
# if there was already one registered, so we need to force its value
3642
# to the one we want to inject.

‎tests/steps/test_given.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ def _(foo):
4141
)
4242
result = pytester.runpytest()
4343
result.assert_outcomes(passed=1)
44+
45+
46+
def test_given_injection_no_deprecation_warning(pytester):
47+
"""Injecting a target fixture must not emit pytest deprecation warnings."""
48+
pytester.makefile(
49+
".feature",
50+
given=textwrap.dedent(
51+
"""\
52+
Feature: Given
53+
Scenario: Test given fixture injection
54+
Given I have injecting given
55+
Then foo should be "injected foo"
56+
"""
57+
),
58+
)
59+
pytester.makepyfile(
60+
textwrap.dedent(
61+
"""\
62+
from pytest_bdd import given, then, scenario
63+
64+
@scenario("given.feature", "Test given fixture injection")
65+
def test_given():
66+
pass
67+
68+
69+
@given("I have injecting given", target_fixture="foo")
70+
def _():
71+
return "injected foo"
72+
73+
74+
@then('foo should be "injected foo"')
75+
def _(foo):
76+
assert foo == "injected foo"
77+
78+
"""
79+
)
80+
)
81+
# fmt: off
82+
result = pytester.runpytest(
83+
"-W", "error::DeprecationWarning",
84+
"-W", "error::PendingDeprecationWarning",
85+
"-W", "ignore:A private pytest class or function was used:pytest.PytestDeprecationWarning",
86+
)
87+
# fmat: on
88+
result.assert_outcomes(passed=1)

‎tox.ini‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[tox]
22
# pytest 7.0-7.2 do not support python 3.12+
3-
envlist = py{3.10,3.11}-pytest{7.0,7.1,7.2}-gherkin_official{29,30,32,33,34,35,36,37,latest}-coverage
4-
py{3.10,3.11,3.12,3.13,3.14}-pytest{7.3,7.4,8.0,8.1,8.2,8.3,9.0,latest}-gherkin_official{29,30,32,33,34,35,36,37,latest}-coverage
5-
py3.15-pytestlatest-gherkin_officiallatest-coverage
3+
envlist = py{3.10,3.11}-pytest{7.0,7.1,7.2}-gherkin_officiallatest-coverage
4+
py{3.10,3.11,3.12,3.13,3.14}-pytest{7.3,7.4,8.0,8.1,8.2,8.3,9.0,9.1,latest}-coverage
5+
py{3.10,3.11,3.12,3.13,3.14}-gherkin_official{29,30,32,33,34,35,36,37,38,39,40,41,42,latest}-coverage
6+
py3.15-coverage
67
py3.12-pytestlatest-xdist-coverage
78
mypy
89

@@ -13,6 +14,11 @@ setenv =
1314
xdist: _PYTEST_MORE_ARGS=-n3 -rfsxX
1415
deps =
1516
gherkin_officiallatest: gherkin-official
17+
gherkin_official42: gherkin-official~=42.0.0
18+
gherkin_official41: gherkin-official~=41.0.0
19+
gherkin_official40: gherkin-official~=40.0.0
20+
gherkin_official39: gherkin-official~=39.0.0
21+
gherkin_official38: gherkin-official~=38.0.0
1622
gherkin_official37: gherkin-official~=37.0.0
1723
gherkin_official36: gherkin-official~=36.0.0
1824
gherkin_official35: gherkin-official~=35.0.0
@@ -23,6 +29,7 @@ deps =
2329
gherkin_official29: gherkin-official~=29.0.0
2430

2531
pytestlatest: pytest
32+
pytest9.1: pytest~=9.1.0
2633
pytest9.0: pytest~=9.0.0
2734
pytest8.3: pytest~=8.3.0
2835
pytest8.2: pytest~=8.2.0

0 commit comments

Comments
 (0)