Skip to content

Commit badc11a

Browse files
authored
Merge pull request #211 from skraynev/no-strict-check-for-background
Fix #210 do not raise wrong Error for no strict gherkin feature
2 parents 65bfca8 + 5d40f2c commit badc11a

File tree

8 files changed

+114
-1
lines changed

8 files changed

+114
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ These people have contributed to `pytest-bdd`, in alphabetical order:
1919
* `Laurence Rowe <[email protected]>`_
2020
* `Leonardo Santagada <[email protected]>`_
2121
* `Robin Pedersen <[email protected]>`_
22+
* `Sergey Kraynev <[email protected]>`_

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
2.18.2
5+
------
6+
7+
- Fix check for out section steps definitions for no strict gherkin feature
8+
49
2.18.1
510
------
611

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,14 @@ steps, adding possibility to prepare some common setup for multiple scenarios in
884884
About background best practices, please read
885885
`here <https://github.com/cucumber/cucumber/wiki/Background#good-practices-for-using-background>`_.
886886

887+
.. NOTE:: There is only step "Given" should be used in "Background" section,
888+
steps "When" and "Then" are prohibited, because their purpose are
889+
related to actions and consuming outcomes, that is conflict with
890+
"Background" aim - prepare system for tests or "put the system
891+
in a known state" as "Given" does it.
892+
The statement above is applied for strict Gherkin mode, which is
893+
enabled by default.
894+
887895

888896
Reusing fixtures
889897
----------------

pytest_bdd/feature.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ def __init__(self, basedir, filename, encoding="utf-8", strict_gherkin=True):
293293
continue
294294
mode = get_step_type(clean_line) or mode
295295

296-
if not scenario and prev_mode not in (types.BACKGROUND, types.GIVEN) and mode in types.STEP_TYPES:
296+
allowed_prev_mode = (types.BACKGROUND, types.GIVEN)
297+
if not strict_gherkin:
298+
allowed_prev_mode += (types.WHEN, )
299+
300+
if not scenario and prev_mode not in allowed_prev_mode and mode in types.STEP_TYPES:
297301
raise exceptions.FeatureError(
298302
"Step definition outside of a Scenario or a Background", line_number, clean_line, filename)
299303

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Feature: No strict Gherkin Background support
2+
3+
Background:
4+
When foo has a value "bar"
5+
And foo is not boolean
6+
And foo has not a value "baz"
7+
8+
Scenario: Test background
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: No strict Gherkin Scenario support
2+
3+
Scenario: Test scenario
4+
When foo has a value "bar"
5+
And foo is not boolean
6+
And foo has not a value "baz"

tests/feature/test_no_scenario.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,25 @@ def test_no_scenarios(testdir):
2424
'*FeatureError: Step definition outside of a Scenario or a Background.*',
2525
],
2626
)
27+
28+
29+
def test_only_background_strict_mode(testdir):
30+
"""Test only wrong background defined in the feature file."""
31+
features = testdir.mkdir('features')
32+
features.join('test.feature').write_text(textwrap.dedent(u"""
33+
Background:
34+
Given foo
35+
When bar
36+
"""), 'utf-8', ensure=True)
37+
testdir.makepyfile(py.code.Source("""
38+
39+
from pytest_bdd import scenarios
40+
41+
scenarios('features')
42+
"""))
43+
result = testdir.runpytest()
44+
result.stdout.fnmatch_lines(
45+
[
46+
'*FeatureError: Background section can only contain Given steps.*',
47+
],
48+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Test no strict gherkin for sections."""
2+
3+
import py
4+
import pytest
5+
6+
from pytest_bdd import (
7+
when,
8+
scenario,
9+
)
10+
11+
12+
@pytest.fixture
13+
def pytestbdd_strict_gherkin():
14+
return False
15+
16+
17+
def test_background_no_strict_gherkin(request):
18+
"""Test background no strict gherkin."""
19+
@scenario(
20+
"no_sctrict_gherkin_background.feature",
21+
"Test background",
22+
)
23+
def test():
24+
pass
25+
26+
test(request)
27+
28+
def test_scenario_no_strict_gherkin(request):
29+
"""Test scenario no strict gherkin."""
30+
@scenario(
31+
"no_sctrict_gherkin_scenario.feature",
32+
"Test scenario",
33+
)
34+
def test():
35+
pass
36+
37+
test(request)
38+
39+
40+
@pytest.fixture
41+
def foo():
42+
return {}
43+
44+
45+
@when('foo has a value "bar"')
46+
def bar(foo):
47+
foo["bar"] = "bar"
48+
return foo["bar"]
49+
50+
51+
@when('foo is not boolean')
52+
def not_boolean(foo):
53+
assert foo is not bool
54+
55+
56+
@when('foo has not a value "baz"')
57+
def has_not_baz(foo):
58+
assert "baz" not in foo
59+

0 commit comments

Comments
 (0)