2013-03-31 09:03:40 +08:00
|
|
|
"""Scenario implementation.
|
|
|
|
|
|
|
|
The pytest will collect the test case and the steps will be executed
|
|
|
|
line by line.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
test_publish_article = scenario(
|
|
|
|
feature_name='publish_article.feature',
|
|
|
|
scenario_name='Publishing the article',
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
import inspect
|
|
|
|
from os import path as op
|
|
|
|
|
2013-03-31 16:37:53 +08:00
|
|
|
from pytest_bdd.feature import Feature
|
2013-03-31 09:03:40 +08:00
|
|
|
|
|
|
|
|
2013-04-03 16:21:50 +08:00
|
|
|
class ScenarioNotFound(Exception):
|
|
|
|
"""Scenario Not Found"""
|
|
|
|
|
|
|
|
|
2013-03-31 09:03:40 +08:00
|
|
|
def scenario(feature_name, scenario_name):
|
|
|
|
"""Scenario."""
|
|
|
|
|
|
|
|
def _scenario(request):
|
2013-04-14 05:32:49 +08:00
|
|
|
# Get the feature
|
2013-05-29 05:09:09 +08:00
|
|
|
base_path = request.getfuncargvalue('pytestbdd_feature_base_dir')
|
2013-05-28 22:41:45 +08:00
|
|
|
feature_path = op.abspath(op.join(base_path, feature_name))
|
2013-03-31 09:03:40 +08:00
|
|
|
feature = Feature.get_feature(feature_path)
|
2013-04-13 08:42:23 +08:00
|
|
|
|
2013-04-14 05:32:49 +08:00
|
|
|
# Get the scenario
|
2013-04-03 16:21:50 +08:00
|
|
|
try:
|
|
|
|
scenario = feature.scenarios[scenario_name]
|
|
|
|
except KeyError:
|
|
|
|
raise ScenarioNotFound('Scenario "{0}" in feature "{1}" is not found'.format(scenario_name, feature_name))
|
2013-03-31 09:03:40 +08:00
|
|
|
|
2013-04-14 05:32:49 +08:00
|
|
|
# Execute scenario's steps
|
2013-04-03 04:51:46 +08:00
|
|
|
for step in scenario.steps:
|
2013-04-14 05:32:49 +08:00
|
|
|
func = request.getfuncargvalue(step)
|
|
|
|
kwargs = dict((arg, request.getfuncargvalue(arg)) for arg in inspect.getargspec(func).args)
|
2013-04-15 23:44:26 +08:00
|
|
|
func(**kwargs)
|
2013-03-31 09:03:40 +08:00
|
|
|
|
2013-04-14 05:32:49 +08:00
|
|
|
return _scenario
|