This commit is contained in:
Oleg Podsadny 2013-04-07 19:45:43 +02:00
parent 0f910fcc51
commit f425e991bf
8 changed files with 121 additions and 1 deletions

6
pytest_bdd/exceptions.py Normal file
View File

@ -0,0 +1,6 @@
"""Exception classes."""
class FeatureError(Exception):
"""Feature parse error."""
pass

View File

@ -21,6 +21,7 @@ one line.
"""
from pytest_bdd.types import SCENARIO, GIVEN, WHEN, THEN
from pytest_bdd.exceptions import FeatureError
# Global features dictionary
features = {}
@ -85,6 +86,7 @@ class Feature(object):
scenario = None
mode = None
prev_mode = None
with open(filename, 'r') as f:
content = f.read()
@ -94,6 +96,18 @@ class Feature(object):
continue
mode = get_step_type(line) or mode
if mode == GIVEN and prev_mode not in (GIVEN, SCENARIO):
raise FeatureError('Given steps must be the first in withing the Scenario')
if mode == WHEN and prev_mode not in (GIVEN, WHEN):
raise FeatureError('When steps must follow Given steps')
if mode == THEN and prev_mode not in (WHEN, THEN):
raise FeatureError('Then steps must follow When steps')
prev_mode = mode
# Remove Given, When, Then, And
line = remove_prefix(line)

View File

@ -22,6 +22,7 @@ def no_error_message(browser):
import pytest
from pytest_bdd.types import GIVEN, WHEN, THEN
from pytest_bdd.feature import remove_prefix
def given(name):
@ -50,7 +51,7 @@ def _decorate_step(step_type, step_name):
"""
def decorator(func):
func.__step_type__ = step_type
func.__step_name__ = step_name
func.__step_name__ = remove_prefix(step_name)
if step_type == GIVEN:
return pytest.fixture(func)
return func

View File

View File

@ -0,0 +1,8 @@
Scenario: Executed step by step
Given I have a foo fixture with value "foo"
And there is a list
When I append 1 to the list
And I append 2 to the list
And I append 3 to the list
Then foo should have value "foo"
And the list should be [1, 2, 3]

View File

@ -0,0 +1,39 @@
from pytest_bdd import scenario, given, when, then
test_steps = scenario('steps.feature', 'Executed step by step')
@given('I have a foo fixture with value "foo"')
def foo():
return 'foo'
@given('there is a list')
def results():
return []
@when('I append 1 to the list')
def append_1(results):
results.append(1)
@when('I append 2 to the list')
def append_2(results):
results.append(2)
@when('I append 3 to the list')
def append_3(results):
results.append(3)
@then('foo should have value "foo"')
def foo_is_foo(foo):
assert foo == 'foo'
@then('the list should be [1, 2, 3]')
def check_results(results):
assert results == [1, 2, 3]

View File

@ -0,0 +1,25 @@
"""Test wrong feature syntax."""
import pytest
from pytest_bdd import scenario
from pytest_bdd.exceptions import FeatureError
@pytest.fixture(params=[
'When after then',
'Then first',
'When first',
'Given after When',
'Given after Then',
])
def scenario_name(request):
return request.param
def test_wrong(request, scenario_name):
"""Test wrong feature scenarios."""
sc = scenario('wrong.feature', scenario_name)
with pytest.raises(FeatureError):
sc(request)

View File

@ -0,0 +1,27 @@
Scenario: When after then
Given I don't always write when after then, but
When I do
Then its fine
When I do it again
Then its wrong
Scenario: Then first
Then it won't work
Scenario: When first
When it won't work
Scenario: Given after When
Given something
When something else
Given won't work
Scenario: Given after Then
Given something
When something else
Then nevermind
Given won't work