Skip to content

Commit 7df981e

Browse files
committed
Ignore mock patch args when resolving step fixtures
1 parent 2a2246b commit 7df981e

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎CHANGES.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Fixed
5858
* 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>`_
5959
* 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>`_
6060
* 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>`_
61+
* Ignore ``unittest.mock.patch``-provided arguments when resolving step fixtures. `#330 <https://github.com/pytest-dev/pytest-bdd/issues/330>`_
6162

6263
Security
6364
++++++++

‎src/pytest_bdd/utils.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from typing import TYPE_CHECKING, TypeVar, cast, overload
1212
from weakref import WeakKeyDictionary
1313

14+
from _pytest.compat import num_mock_patch_args
15+
1416
if TYPE_CHECKING:
1517
from _pytest.config import Config
1618
from _pytest.pytester import RunResult
@@ -30,9 +32,10 @@ def get_required_args(func: Callable[..., object]) -> list[str]:
3032
:return: A list of argument names.
3133
"""
3234
params = signature(func).parameters.values()
33-
return [
35+
required_args = [
3436
param.name for param in params if param.kind == param.POSITIONAL_OR_KEYWORD and param.default is param.empty
3537
]
38+
return required_args[num_mock_patch_args(func) :]
3639

3740

3841
def get_caller_module_locals(stacklevel: int = 1) -> dict[str, object]:

‎tests/steps/test_common.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,40 @@ def _(foo, expected_value):
123123
assert foo2 == "test bar"
124124

125125

126+
def test_step_function_accepts_unittest_mock_patch_argument(pytester):
127+
pytester.makefile(
128+
".feature",
129+
mock_patch=textwrap.dedent(
130+
"""\
131+
Feature: Mock patch arguments
132+
Scenario: A step uses unittest.mock.patch as a decorator
133+
Then the patched function is called
134+
"""
135+
),
136+
)
137+
pytester.makepyfile(
138+
textwrap.dedent(
139+
"""\
140+
from pathlib import Path
141+
from unittest.mock import patch
142+
143+
from pytest_bdd import scenarios, then
144+
145+
scenarios("mock_patch.feature")
146+
147+
148+
@then("the patched function is called")
149+
@patch("pathlib.Path.cwd")
150+
def _(cwd_mock):
151+
Path.cwd()
152+
cwd_mock.assert_called_once_with()
153+
"""
154+
)
155+
)
156+
result = pytester.runpytest()
157+
result.assert_outcomes(passed=1)
158+
159+
126160
def test_step_functions_same_parser(pytester):
127161
pytester.makefile(
128162
".feature",

0 commit comments

Comments
 (0)