pytest-dev / pytest-bdd

BDD library for the py.test runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.3k stars 219 forks source link

Pytest 8.1.1 fixture injection compability #677

Closed mp2218 closed 6 months ago

mp2218 commented 6 months ago

Test which had fixture injection worked fine last week, but now with new pytest release breaks with following trace:

def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None: """Inject fixture into pytest fixture request. :param request: pytest fixture request :param arg: argument name :param value: argument value """ fd = FixtureDef( fixturemanager=request._fixturemanager, baseid=None, argname=arg, func=lambda: value, scope="function", params=None, ) E TypeError: FixtureDef.init() got an unexpected keyword argument 'fixturemanager' /opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pytest_bdd/steps.py:213: TypeError

snippet of fixture injection:

@given("User does not have credit", target_fixture="fielding_page") def no_money(context_logged_in, base_url): return Fielding_Page(context_logged_in.new_page(), base_url)

Version: bdd-7.1.1 `

simbs commented 6 months ago

I ran into this yesterday as well. Here's the relevant pytest change, it's there from version 8.1.1

I was able to reproduce this locally running the pytest-bdd tests with the latest pytest version. I could also fix them by updating the call to FixtureDef in inject_fixture here: https://github.com/pytest-dev/pytest-bdd/blob/master/src/pytest_bdd/steps.py#L213

but obviously it then breaks compatibility with previous versions of pytest. The compat.py file is already addressing what looks like a related compatibility issue, with two different functions depending on whether pytest is below or above 8.1, so creating a function there that instantiates FixtureDef differently based on the version worked for me locally.

It's not the nicest solution, but I'm happy to open a PR with these changes if it'll help.

youtux commented 6 months ago

Fixed in https://github.com/pytest-dev/pytest-bdd/pull/680. I just released version 7.1.2 with the fix