pytest-dev / pytest-dev/pytest-bdd

Assertions not rewritten for steps in files with names that don't match Pytest's python_files setting

Open
#304 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
251
Avg merge
43m
Merged PRs (30d)
2

Description

pytest-bdd steps in files with names that don't match pytest's patterns in thepython_files settings, like steps.py, given.py, when.py then.py, are not picked up by pytests assertion rewriting.

Here's a simple setup to reproduce: https://github.com/blaise-io/pytest-bdd-assertion-rewrite-issue.

In most apps this can be fixed by using pytest.register_assert_rewrite to register modules containing steps, but I'd expect pytest-bdd to either do that for me or for pytest-bdd to warn me about this limitation.

For pytest-bdd to register steps modules magically seemed only possible with heavy monkey patching, as pytest does not allow registering a module for rewriting when the module is already imported. The following code registers the module of a steps for assertion rewriting, which is to demonstrate why monkey patching is a bad idea:

    # append to pytest_bdd.steps.contribute_to_module
    from _pytest.assertion import rewrite
    from six.moves import reload_module
    for hook in sys.meta_path:
        if isinstance(hook, rewrite.AssertionRewritingHook):
            # Add module file name to python_files patterns
            if module.__file__ not in hook.fnpats:
                hook.fnpats.append(module.__file__)
            # Remove module path from checks
            if name in hook._rewritten_names:
                hook._rewritten_names.remove(name)
            # Force-reload the module after our rewrite in setattr
            reload_module(module)
            break

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the linked reproduction project first, then inspect pytest_bdd.steps.contribute_to_module and pytest.register_assert_rewrite. Determine how step modules are imported relative to pytest's assertion-rewriting patterns; done means steps in files such as steps.py receive assertion rewriting, or the limitation is clearly reported to users.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.