Skip to content

Ignore mock patch arguments when resolving step fixtures#816

Open
lin-hongkuan wants to merge 1 commit into
pytest-dev:masterfrom
lin-hongkuan:codex/allow-mock-patched-step-args
Open

Ignore mock patch arguments when resolving step fixtures#816
lin-hongkuan wants to merge 1 commit into
pytest-dev:masterfrom
lin-hongkuan:codex/allow-mock-patched-step-args

Conversation

@lin-hongkuan

Copy link
Copy Markdown

Fixes #330.

Summary

unittest.mock.patch() injects mock objects as positional arguments when the decorated function is called. Pytest accounts for that by ignoring those injected arguments while resolving fixtures, but pytest-bdd currently treats every required positional step parameter as a fixture dependency.

That means a patched step like:

@then("the patched function is called")
@patch("pathlib.Path.cwd")
def _(cwd_mock):
    ...

fails before execution with fixture 'cwd_mock' not found.

This updates pytest-bdd's required-argument helper to reuse pytest's num_mock_patch_args() rule, so mock-provided arguments are skipped when resolving step fixtures. Normal fixture dependencies and parser-provided step arguments continue to work unchanged.

Tests

  • .\.venv\Scripts\python.exe -m pytest tests/steps/test_common.py::test_step_function_accepts_unittest_mock_patch_argument -q
  • .\.venv\Scripts\python.exe -m pytest tests/steps/test_common.py tests/args/parse/test_args.py tests/args/regex/test_args.py tests/args/cfparse/test_args.py -q -> 16 passed
  • .\.venv\Scripts\python.exe -m pytest tests/feature/test_steps.py::test_step_hooks tests/feature/test_steps.py::test_step_trace -q -> 2 passed
  • .\.venv\Scripts\ruff.exe check src/pytest_bdd/utils.py tests/steps/test_common.py
  • .\.venv\Scripts\ruff.exe format --check src/pytest_bdd/utils.py tests/steps/test_common.py
  • .\.venv\Scripts\mypy.exe src/pytest_bdd/utils.py -> no type issues; current mypy reports that this version no longer supports the repository's configured python_version = 3.9
  • git diff --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

It's not possible to disable "argument is a fixture" behavior (pytest-bdd + unittest.mock)

1 participant