forked from test_framework/pytest-bdd
when and then type test added
This commit is contained in:
parent
8e37b8f616
commit
e30401af01
|
@ -14,6 +14,7 @@ test_publish_article = scenario(
|
|||
import inspect
|
||||
from os import path as op
|
||||
|
||||
from pytest_bdd.types import GIVEN, THEN
|
||||
from pytest_bdd.feature import Feature
|
||||
|
||||
|
||||
|
@ -25,36 +26,27 @@ def scenario(feature_name, scenario_name):
|
|||
"""Scenario."""
|
||||
|
||||
def _scenario(request):
|
||||
# get feature
|
||||
# Get the feature
|
||||
feature_path = op.abspath(op.join(op.dirname(request.module.__file__), feature_name))
|
||||
feature = Feature.get_feature(feature_path)
|
||||
|
||||
# get scenario
|
||||
# Get the scenario
|
||||
try:
|
||||
scenario = feature.scenarios[scenario_name]
|
||||
except KeyError:
|
||||
raise ScenarioNotFound('Scenario "{0}" in feature "{1}" is not found'.format(scenario_name, feature_name))
|
||||
|
||||
# execute scenario's steps
|
||||
# Execute scenario's steps
|
||||
for step in scenario.steps:
|
||||
_execute_step(request, step)
|
||||
# Evaluate the fixture, also applies to given
|
||||
func = request.getfuncargvalue(step)
|
||||
if getattr(func, '__step_type__', None) is None:
|
||||
continue
|
||||
|
||||
# Execute when and then steps
|
||||
kwargs = dict((arg, request.getfuncargvalue(arg)) for arg in inspect.getargspec(func).args)
|
||||
result = func(**kwargs)
|
||||
if func.__step_type__ == THEN:
|
||||
assert result is None or result
|
||||
|
||||
return _scenario
|
||||
|
||||
|
||||
def _execute_step(request, name):
|
||||
"""Execute the step.
|
||||
|
||||
:param request: pytest request object.
|
||||
:param name: Step name.
|
||||
|
||||
:note: Steps can take pytest fixture parameters. They will be evaluated
|
||||
from the request and passed to the step function.
|
||||
"""
|
||||
fixture = request.getfuncargvalue(name)
|
||||
|
||||
# if fixture is a callable and has a __step_type__ then it's a step fixture
|
||||
if callable(fixture) and hasattr(fixture, '__step_type__'):
|
||||
kwargs = dict((arg, request.getfuncargvalue(arg)) for arg in inspect.getargspec(fixture).args)
|
||||
# calling an action
|
||||
fixture(**kwargs)
|
||||
|
|
|
@ -138,8 +138,8 @@ def _step_decorator(step_type, step_name):
|
|||
frame = inspect.stack()[1]
|
||||
module = inspect.getmodule(frame[0])
|
||||
|
||||
# if it's a THEN step, we should defer the evaluation to separate resolving params and action
|
||||
if step_type == THEN:
|
||||
# When and then steps are functions
|
||||
if step_type != GIVEN:
|
||||
func = lambda request: func
|
||||
func = _decorate_step(func, step_type, step_name)
|
||||
|
||||
|
|
Loading…
Reference in New Issue