python 3 support. additional checks for parameters

This commit is contained in:
Anatoly Bubenkov 2013-08-12 00:42:09 +02:00
parent 1f9bbc8cde
commit 1801193062
5 changed files with 54 additions and 24 deletions

View File

@ -19,21 +19,21 @@ Syntax example:
:note: There're no multiline steps, the description of the step must fit in
one line.
"""
import re
import re # pragma: no cover
from pytest_bdd.types import SCENARIO, GIVEN, WHEN, THEN # pragma: no cover
class FeatureError(Exception):
class FeatureError(Exception): # pragma: no cover
"""Feature parse error."""
pass
# Global features dictionary
features = {}
features = {} # pragma: no cover
STEP_PREFIXES = {
STEP_PREFIXES = { # pragma: no cover
'Scenario: ': SCENARIO,
'Given ': GIVEN,
'When ': WHEN,
@ -41,9 +41,9 @@ STEP_PREFIXES = {
'And ': None, # Unknown step type
}
COMMENT_SYMBOLS = '#'
COMMENT_SYMBOLS = '#' # pragma: no cover
STEP_PARAM_RE = re.compile('\<(.+?)\>')
STEP_PARAM_RE = re.compile('\<(.+?)\>') # pragma: no cover
def get_step_type(line):

View File

@ -23,6 +23,10 @@ class ScenarioNotFound(Exception): # pragma: no cover
"""Scenario Not Found"""
class NotEnoughScenarioParams(Exception): # pragma: no cover
pass
def scenario(feature_name, scenario_name):
"""Scenario. May be called both as decorator and as just normal function"""
@ -42,7 +46,10 @@ def scenario(feature_name, scenario_name):
'Scenario "{0}" in feature "{1}" is not found'.format(scenario_name, feature_name))
if scenario.params != _scenario.pytestbdd_params:
raise Exception(scenario.params, _scenario.pytestbdd_params)
raise NotEnoughScenarioParams(
"""Scenario "{0}" in feature "{1}" doesn't have enough parameters declared.
Should declare params: {2}, but declared only: {3}""".format(
scenario_name, feature_name, list(scenario.params), list(_scenario.pytestbdd_params)))
# Execute scenario's steps
for step in scenario.steps:
@ -60,7 +67,7 @@ def scenario(feature_name, scenario_name):
func_args = inspect.getargspec(request).args
if 'request' in func_args:
func_args.remove('request')
_scenario = recreate_function(_scenario, add_args=func_args)
_scenario = recreate_function(_scenario, name=request.__name__, add_args=func_args)
_scenario.pytestbdd_params = set(func_args)
return _scenario

View File

@ -31,24 +31,24 @@ Reusing existing fixtures for a different step name:
given('I have a beautiful article', fixture='article')
"""
from __future__ import absolute_import
from types import CodeType
import inspect
import sys
from __future__ import absolute_import # pragma: no cover
from types import CodeType # pragma: no cover
import inspect # pragma: no cover # pragma: no cover
import sys # pragma: no cover
import pytest
import pytest # pragma: no cover
from pytest_bdd.feature import remove_prefix, get_step_params
from pytest_bdd.types import GIVEN, WHEN, THEN
from pytest_bdd.feature import remove_prefix, get_step_params # pragma: no cover
from pytest_bdd.types import GIVEN, WHEN, THEN # pragma: no cover
PY3 = sys.version_info[0] >= 3
PY3 = sys.version_info[0] >= 3 # pragma: no cover
class StepError(Exception):
class StepError(Exception): # pragma: no cover
pass
class NotEnoughStepParams(Exception):
class NotEnoughStepParams(Exception): # pragma: no cover
pass
@ -120,7 +120,7 @@ def _step_decorator(step_type, step_name):
if step_params.intersection(step_func_args) != step_params:
raise NotEnoughStepParams(
"""Step "{0}" doesn't have enough parameters declared.
Should declare params: {1}, but declared only: {2}""".format(step_name, step_params, step_func_args))
Should declare params: {1}, but declared only: {2}""".format(step_name, list(step_params), list(step_func_args)))
if step_type == GIVEN:
if not hasattr(func, '_pytestfixturefunction'):
@ -140,7 +140,7 @@ Should declare params: {1}, but declared only: {2}""".format(step_name, step_par
return decorator
def recreate_function(func, module=None, add_args=()):
def recreate_function(func, module=None, name=None, add_args=()):
"""Recreate a function, replacing some info.
:param func: Function object.
:param module: Module to contribute to.
@ -167,6 +167,8 @@ def recreate_function(func, module=None, add_args=()):
for arg in argnames:
if module is not None and arg == 'co_filename':
args.append(module.__file__)
elif name is not None and arg == 'co_name':
args.append(name)
elif arg == 'co_argcount':
args.append(getattr(code, arg) + len(add_args))
elif arg == 'co_varnames':
@ -176,6 +178,8 @@ def recreate_function(func, module=None, add_args=()):
args.append(getattr(code, arg))
set_code(func, CodeType(*args))
if name is not None:
func.__name__ = name
return func

View File

@ -1,4 +1,4 @@
mock
pytest-pep8
pytest-cov
pytest-cache
pytest-cache

View File

@ -1,6 +1,7 @@
import pytest
from pytest_bdd.steps import NotEnoughStepParams
from pytest_bdd.scenario import NotEnoughScenarioParams
from pytest_bdd import given, when, then, scenario
@ -24,7 +25,7 @@ def test_parametrized_given():
return {}
assert exc.value.args == (
'Step "there are <some> cucumbers" doesn\'t have enough parameters declared.\n'
'Should declare params: set([\'some\']), but declared only: []',)
'Should declare params: [\'some\'], but declared only: []',)
def test_parametrized_when():
@ -35,7 +36,7 @@ def test_parametrized_when():
return {}
assert exc.value.args == (
'Step "I eat <some> cucumbers" doesn\'t have enough parameters declared.\n'
'Should declare params: set([\'some\']), but declared only: []',)
'Should declare params: [\'some\'], but declared only: []',)
def test_parametrized_then():
@ -46,7 +47,25 @@ def test_parametrized_then():
return {}
assert exc.value.args == (
'Step "I should have <some> cucumbers" doesn\'t have enough parameters declared.\n'
'Should declare params: set([\'some\']), but declared only: []',)
'Should declare params: [\'some\'], but declared only: []',)
def test_parametrized_wrongly(request):
"""Test parametrized scenario when the test function lacks parameters."""
@scenario(
'parametrized.feature',
'Parametrized given, when, thens',
)
def test_parametrized_wrongly(request):
pass
with pytest.raises(NotEnoughScenarioParams) as exc:
test_parametrized_wrongly(request)
assert exc.value.args == (
'Scenario "Parametrized given, when, thens" in feature "parametrized.feature" doesn\'t have enough '
'parameters declared.\nShould declare params: [\'start\', \'eat\', \'left\'], but declared only: []',
)
@given('there are <start> cucumbers')