Skip to content

Commit 0959385

Browse files
committed
Add multiple values in target_fixture
1 parent d45c543 commit 0959385

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/pytest_bdd/scenario.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ def _execute_step_function(
162162
raise
163163

164164
if context.target_fixture is not None:
165-
inject_fixture(request, context.target_fixture, return_value)
165+
target_fixture_tokens = [token for token in context.target_fixture.split(',') if token]
166+
return_values = (return_value,) if not isinstance(return_value, tuple) else return_value
167+
assert len(target_fixture_tokens) == len(return_values), f"Return value count: {len(return_values)} are not matching target_fixture count: {len(target_fixture_tokens)}"
168+
for token, value in zip(target_fixture_tokens, return_values):
169+
inject_fixture(request, token, value)
166170

167171
request.config.hook.pytest_bdd_after_step(**kw)
168172

tests/steps/test_given.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,42 @@ def _(foo):
3838
)
3939
result = pytester.runpytest()
4040
result.assert_outcomes(passed=1)
41+
42+
43+
def test_given_injection_multiple(pytester):
44+
pytester.makefile(
45+
".feature",
46+
given=textwrap.dedent(
47+
"""\
48+
Feature: Given
49+
Scenario: Test given fixture injection
50+
Given I have injecting given
51+
Then foo should be "injected foo"
52+
"""
53+
),
54+
)
55+
pytester.makepyfile(
56+
textwrap.dedent(
57+
"""\
58+
import pytest
59+
from pytest_bdd import given, then, scenario
60+
61+
@scenario("given.feature", "Test given fixture injection")
62+
def test_given():
63+
pass
64+
65+
@given("I have injecting given", target_fixture="foo,bar")
66+
def _():
67+
return "injected foo", "injected bar"
68+
69+
70+
@then('foo should be "injected foo"')
71+
def _(foo, bar):
72+
assert foo == "injected foo"
73+
assert bar == "injected bar"
74+
75+
"""
76+
)
77+
)
78+
result = pytester.runpytest()
79+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)