In my use case, I need to take arbitrary variables supplied via gherkin feature file and provide them to a pytest fixture and am struggling to figure out how this is possible with pytest-bdd. I have looked at the Parametrization API, but don't think it's directly usable here since it uses a separate decorator.
Here is a very simple example showing the dilemma. Assume the work of "adder" HAS to be done in a fixture and not the step function itself.
test.feature:
Feature: Example feature
Scenario: Example scenario
When my fixture adds 1 to 2
Then result is 3
test.py:
import pytest
from pytest_bdd import scenarios
from steps import *
scenarios("test.feature")
steps.py:
from pytest_bdd import parsers, then, when
@when(parsers.parse("my fixture adds {a:d} to {b:d}"))
def _(adder) -> None:
pass
@then(parsers.parse("result is {c:d}"))
def _(adder, c) -> None:
assert adder == c
conftest.py:
import pytest
@pytest.fixture
def adder(request):
c = 0 # In this part, I want to get access to 'a' and 'b' but don't know how
yield c
My question is, how can I supply the parsed arguments "a" and "b" to the adder fixture?
- pytest==7.4.4
- pytest-bdd==6.1.1