Skip to content

Commit

Permalink
Add multiple values in target_fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
arunpkm committed Apr 21, 2023
1 parent d45c543 commit 0959385
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/pytest_bdd/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def _execute_step_function(
raise

if context.target_fixture is not None:
inject_fixture(request, context.target_fixture, return_value)
target_fixture_tokens = [token for token in context.target_fixture.split(',') if token]
return_values = (return_value,) if not isinstance(return_value, tuple) else return_value
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)}"
for token, value in zip(target_fixture_tokens, return_values):
inject_fixture(request, token, value)

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

Expand Down
39 changes: 39 additions & 0 deletions tests/steps/test_given.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,42 @@ def _(foo):
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)


def test_given_injection_multiple(pytester):
pytester.makefile(
".feature",
given=textwrap.dedent(
"""\
Feature: Given
Scenario: Test given fixture injection
Given I have injecting given
Then foo should be "injected foo"
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
import pytest
from pytest_bdd import given, then, scenario
@scenario("given.feature", "Test given fixture injection")
def test_given():
pass
@given("I have injecting given", target_fixture="foo,bar")
def _():
return "injected foo", "injected bar"
@then('foo should be "injected foo"')
def _(foo, bar):
assert foo == "injected foo"
assert bar == "injected bar"
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)

0 comments on commit 0959385

Please sign in to comment.