Fix #210 do not raise wrong Error for no strict gherkin feature

The issue, that if user disable strict gherkin check, it will lead to
situation, when wrong Error be raised. For the current situation it was
a message, that Step is defined outside of Background section.

Current patch makes check for orphan steps more tolerant for features
without strict gherkin syntax.

Also README was updated to show explicitly, that using "When, Then" is
prohibited in "Background" section.
This commit is contained in:
Sergey Kraynev 2017-04-18 11:49:24 +03:00 committed by Sergey Kraynev
parent 65bfca8027
commit 5d40f2c28e
8 changed files with 114 additions and 1 deletions

View File

@ -19,3 +19,4 @@ These people have contributed to `pytest-bdd`, in alphabetical order:
* `Laurence Rowe <l@lrowe.co.uk>`_ * `Laurence Rowe <l@lrowe.co.uk>`_
* `Leonardo Santagada <santagada@github.com>`_ * `Leonardo Santagada <santagada@github.com>`_
* `Robin Pedersen <ropez@github.com>`_ * `Robin Pedersen <ropez@github.com>`_
* `Sergey Kraynev <sergejyit@gmail.com>`_

View File

@ -1,6 +1,11 @@
Changelog Changelog
========= =========
2.18.2
------
- Fix check for out section steps definitions for no strict gherkin feature
2.18.1 2.18.1
------ ------

View File

@ -884,6 +884,14 @@ steps, adding possibility to prepare some common setup for multiple scenarios in
About background best practices, please read About background best practices, please read
`here <https://github.com/cucumber/cucumber/wiki/Background#good-practices-for-using-background>`_. `here <https://github.com/cucumber/cucumber/wiki/Background#good-practices-for-using-background>`_.
.. NOTE:: There is only step "Given" should be used in "Background" section,
steps "When" and "Then" are prohibited, because their purpose are
related to actions and consuming outcomes, that is conflict with
"Background" aim - prepare system for tests or "put the system
in a known state" as "Given" does it.
The statement above is applied for strict Gherkin mode, which is
enabled by default.
Reusing fixtures Reusing fixtures
---------------- ----------------

View File

@ -293,7 +293,11 @@ class Feature(object):
continue continue
mode = get_step_type(clean_line) or mode mode = get_step_type(clean_line) or mode
if not scenario and prev_mode not in (types.BACKGROUND, types.GIVEN) and mode in types.STEP_TYPES: allowed_prev_mode = (types.BACKGROUND, types.GIVEN)
if not strict_gherkin:
allowed_prev_mode += (types.WHEN, )
if not scenario and prev_mode not in allowed_prev_mode and mode in types.STEP_TYPES:
raise exceptions.FeatureError( raise exceptions.FeatureError(
"Step definition outside of a Scenario or a Background", line_number, clean_line, filename) "Step definition outside of a Scenario or a Background", line_number, clean_line, filename)

View File

@ -0,0 +1,8 @@
Feature: No strict Gherkin Background support
Background:
When foo has a value "bar"
And foo is not boolean
And foo has not a value "baz"
Scenario: Test background

View File

@ -0,0 +1,6 @@
Feature: No strict Gherkin Scenario support
Scenario: Test scenario
When foo has a value "bar"
And foo is not boolean
And foo has not a value "baz"

View File

@ -24,3 +24,25 @@ def test_no_scenarios(testdir):
'*FeatureError: Step definition outside of a Scenario or a Background.*', '*FeatureError: Step definition outside of a Scenario or a Background.*',
], ],
) )
def test_only_background_strict_mode(testdir):
"""Test only wrong background defined in the feature file."""
features = testdir.mkdir('features')
features.join('test.feature').write_text(textwrap.dedent(u"""
Background:
Given foo
When bar
"""), 'utf-8', ensure=True)
testdir.makepyfile(py.code.Source("""
from pytest_bdd import scenarios
scenarios('features')
"""))
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
'*FeatureError: Background section can only contain Given steps.*',
],
)

View File

@ -0,0 +1,59 @@
"""Test no strict gherkin for sections."""
import py
import pytest
from pytest_bdd import (
when,
scenario,
)
@pytest.fixture
def pytestbdd_strict_gherkin():
return False
def test_background_no_strict_gherkin(request):
"""Test background no strict gherkin."""
@scenario(
"no_sctrict_gherkin_background.feature",
"Test background",
)
def test():
pass
test(request)
def test_scenario_no_strict_gherkin(request):
"""Test scenario no strict gherkin."""
@scenario(
"no_sctrict_gherkin_scenario.feature",
"Test scenario",
)
def test():
pass
test(request)
@pytest.fixture
def foo():
return {}
@when('foo has a value "bar"')
def bar(foo):
foo["bar"] = "bar"
return foo["bar"]
@when('foo is not boolean')
def not_boolean(foo):
assert foo is not bool
@when('foo has not a value "baz"')
def has_not_baz(foo):
assert "baz" not in foo