Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Removed
Fixed
+++++
* 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>`_
* Ignore ``unittest.mock.patch``-provided arguments when resolving step fixtures. `#330 <https://github.com/pytest-dev/pytest-bdd/issues/330>`_

Security
++++++++
Expand Down
5 changes: 4 additions & 1 deletion src/pytest_bdd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from typing import TYPE_CHECKING, Callable, TypeVar, cast, overload
from weakref import WeakKeyDictionary

from _pytest.compat import num_mock_patch_args

if TYPE_CHECKING:
from _pytest.config import Config
from _pytest.pytester import RunResult
Expand All @@ -29,9 +31,10 @@ def get_required_args(func: Callable[..., object]) -> list[str]:
:return: A list of argument names.
"""
params = signature(func).parameters.values()
return [
required_args = [
param.name for param in params if param.kind == param.POSITIONAL_OR_KEYWORD and param.default is param.empty
]
return required_args[num_mock_patch_args(func) :]


def get_caller_module_locals(stacklevel: int = 1) -> dict[str, object]:
Expand Down
34 changes: 34 additions & 0 deletions tests/steps/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@ def _(foo, expected_value):
assert foo2 == "test bar"


def test_step_function_accepts_unittest_mock_patch_argument(pytester):
pytester.makefile(
".feature",
mock_patch=textwrap.dedent(
"""\
Feature: Mock patch arguments
Scenario: A step uses unittest.mock.patch as a decorator
Then the patched function is called
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pathlib import Path
from unittest.mock import patch

from pytest_bdd import scenarios, then

scenarios("mock_patch.feature")


@then("the patched function is called")
@patch("pathlib.Path.cwd")
def _(cwd_mock):
Path.cwd()
cwd_mock.assert_called_once_with()
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)


def test_step_functions_same_parser(pytester):
pytester.makefile(
".feature",
Expand Down