forked from test_framework/pytest-bdd
Execute step definitions as if they were pytest fixture.
This way we can get the same behaviour of yield-fixtures.
This commit is contained in:
parent
2fb5a11812
commit
8fe2e4ff3c
|
@ -16,7 +16,7 @@ import re
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.fixtures import FixtureLookupError
|
from _pytest.fixtures import FixtureLookupError, call_fixture_func
|
||||||
|
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
from .feature import get_feature, get_features
|
from .feature import get_feature, get_features
|
||||||
|
@ -112,8 +112,9 @@ def _execute_step_function(request, scenario, step, step_func):
|
||||||
|
|
||||||
request.config.hook.pytest_bdd_before_step_call(**kw)
|
request.config.hook.pytest_bdd_before_step_call(**kw)
|
||||||
target_fixture = getattr(step_func, "target_fixture", None)
|
target_fixture = getattr(step_func, "target_fixture", None)
|
||||||
# Execute the step.
|
|
||||||
return_value = step_func(**kwargs)
|
# Execute the step as if it was a pytest fixture, so that we can allow "yield" statements in it
|
||||||
|
return_value = call_fixture_func(fixturefunc=step_func, request=request, kwargs=kwargs)
|
||||||
if target_fixture:
|
if target_fixture:
|
||||||
inject_fixture(request, target_fixture, return_value)
|
inject_fixture(request, target_fixture, return_value)
|
||||||
|
|
||||||
|
|
|
@ -484,3 +484,51 @@ def test_step_trace(testdir):
|
||||||
result.assert_outcomes(failed=1)
|
result.assert_outcomes(failed=1)
|
||||||
result.stdout.fnmatch_lines(["*test_when_step_validation_error*FAILED"])
|
result.stdout.fnmatch_lines(["*test_when_step_validation_error*FAILED"])
|
||||||
assert "INTERNALERROR" not in result.stdout.str()
|
assert "INTERNALERROR" not in result.stdout.str()
|
||||||
|
|
||||||
|
|
||||||
|
def test_steps_with_yield(testdir):
|
||||||
|
"""Test that steps definition containing a yield statement work the same way as
|
||||||
|
pytest fixture do, that is the code after the yield is executed during teardown."""
|
||||||
|
|
||||||
|
testdir.makefile(
|
||||||
|
".feature",
|
||||||
|
a="""\
|
||||||
|
Feature: A feature
|
||||||
|
|
||||||
|
Scenario: A scenario
|
||||||
|
When I setup stuff
|
||||||
|
Then stuff should be 42
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
testdir.makepyfile(
|
||||||
|
textwrap.dedent(
|
||||||
|
"""\
|
||||||
|
import pytest
|
||||||
|
from pytest_bdd import given, when, then, scenarios
|
||||||
|
|
||||||
|
scenarios("a.feature")
|
||||||
|
|
||||||
|
@when("I setup stuff", target_fixture="stuff")
|
||||||
|
def stuff():
|
||||||
|
print("Setting up...")
|
||||||
|
yield 42
|
||||||
|
print("Tearing down...")
|
||||||
|
|
||||||
|
|
||||||
|
@then("stuff should be 42")
|
||||||
|
def check_stuff(stuff):
|
||||||
|
assert stuff == 42
|
||||||
|
print("Asserted stuff is 42")
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
result = testdir.runpytest("-s")
|
||||||
|
result.assert_outcomes(passed=1)
|
||||||
|
result.stdout.fnmatch_lines(
|
||||||
|
[
|
||||||
|
"*Setting up...*",
|
||||||
|
"*Asserted stuff is 42*",
|
||||||
|
"*Tearing down...*",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue