pytest-bdd
pytest-bdd copied to clipboard
Stacking multiple `then` decorators breaks parametrization
trafficstars
Env: Pytest 8.2.2 Pytest-bdd 7.2.0 python 3.12.3
When I stack multiple then step, of which at least one has parametrization, pytest raises fixture <parameter> not found error.
But, if there is a single parametrized then decorator, the test passes. I have tried changing the order of the decorators, needless to say it didn't make any difference.
Here's the code:
Feature: Step arguments
Scenario: Arguments for given, when, then
Given there are 5 cucumbers
When I eat 3 cucumbers
And I eat 2 cucumbers
Then I should have 0 cucumbers
And I should be able to get this example working
from pytest_bdd import given, parsers, scenarios, then, when
scenarios("../features/dummy_feature.feature")
@given(parsers.parse("there are {start:d} cucumbers"), target_fixture="cucumbers")
def given_cucumbers(start):
return {"start": start, "eat": 0}
@when(parsers.parse("I eat {eat:d} cucumbers"))
def eat_cucumbers(cucumbers, eat):
cucumbers["eat"] += eat
@then(parsers.parse("I should have {left:d} cucumbers"))
@then("I should be able to get this example working")
def should_have_left_cucumbers(cucumbers, left):
assert cucumbers["start"] - cucumbers["eat"] == left
I expect the test to pass, but I get the following:
❯ pytest test/step_definition/test_dummy_feature.py
=================================================================================== test session starts ====================================================================================
platform linux -- Python 3.12.3, pytest-8.2.2, pluggy-1.5.0
rootdir: /path/to/rootdir
configfile: pyproject.toml
plugins: hypothesis-6.103.1, bdd-7.2.0, anyio-4.4.0
collected 1 item
test/step_definition/test_dummy_feature.py F [100%]
========================================================================================= FAILURES =========================================================================================
____________________________________________________________________________ test_arguments_for_given_when_then ____________________________________________________________________________
file /path/to/venv/lib/python3.12/site-packages/pytest_bdd/scenario.py, line 268
@pytest.mark.usefixtures(*func_args)
def scenario_wrapper(request: FixtureRequest, _pytest_bdd_example: dict[str, str]) -> Any:
E fixture 'left' not found
> available fixtures: _pytest_bdd_example, anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monke
ypatch, pytestbdd_stepdef_given_there are {start:d} cucumbers, pytestbdd_stepdef_given_trace, pytestbdd_stepdef_then_I should be able to get this example working, pytestbdd_stepdef_then_I
should have {left:d} cucumbers, pytestbdd_stepdef_then_trace, pytestbdd_stepdef_when_I eat {eat:d} cucumbers, pytestbdd_stepdef_when_trace, pytestconfig, record_property, record_testsuite_
property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/path/to/venv/lib/python3.12/site-packages/pytest_bdd/scenario.py:268
================================================================================= short test summary info ==================================================================================
FAILED test/step_definition/test_dummy_feature.py::test_arguments_for_given_when_then
==================================================================================== 1 failed in 0.07s =====================================================================================
I am not sure if I need to configure something in toml. Even if I were doing something wrong, the fact that one works, but >1 doesn't, seems very surprising. Even then, stacking non-parametrized then steps work as expected. Either I must be missing something completely obvious, or this is a bug.