Skip to content

Commit 6ce9c0c

Browse files
sliwinski-miloszyoutux
authored andcommitted
Do not call fixtures directly. bdd_feature_base_dir and bdd_strict_gherkin ini configs added. Pytest-bdd fixtures removed
1 parent 39f7297 commit 6ce9c0c

9 files changed

+160
-123
lines changed

pytest_bdd/fixtures.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

pytest_bdd/plugin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from . import reporting
99
from . import gherkin_terminal_reporter
1010

11-
from .fixtures import *
12-
1311

1412
def pytest_addhooks(pluginmanager):
1513
"""Register plugin hooks."""
@@ -32,11 +30,20 @@ def trace():
3230

3331
def pytest_addoption(parser):
3432
"""Add pytest-bdd options."""
33+
add_bdd_ini(parser)
3534
cucumber_json.add_options(parser)
3635
generation.add_options(parser)
3736
gherkin_terminal_reporter.add_options(parser)
3837

3938

39+
def add_bdd_ini(parser):
40+
parser.addini('bdd_feature_base_dir',
41+
'Base feature directory.')
42+
parser.addini('bdd_strict_gherkin',
43+
'Parse features to be strict gherkin.',
44+
type='bool', default=True)
45+
46+
4047
@pytest.mark.trylast
4148
def pytest_configure(config):
4249
"""Configure all subplugins."""

pytest_bdd/scenario.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import six
2424

2525
from . import exceptions
26-
from . import fixtures
2726
from .feature import (
2827
Feature,
2928
force_encode,
@@ -204,31 +203,6 @@ def _execute_scenario(feature, scenario, request, encoding):
204203
FakeRequest = collections.namedtuple("FakeRequest", ["module"])
205204

206205

207-
def get_fixture(caller_module, fixture, path=None):
208-
"""Get first conftest module from given one."""
209-
def call_fixture(function):
210-
args = []
211-
if "request" in get_args(function):
212-
args = [FakeRequest(module=caller_module)]
213-
return function(*args)
214-
215-
if path is None:
216-
if hasattr(caller_module, fixture):
217-
return call_fixture(getattr(caller_module, fixture))
218-
path = os.path.dirname(caller_module.__file__)
219-
220-
if os.path.exists(os.path.join(path, "__init__.py")):
221-
file_path = os.path.join(path, "conftest.py")
222-
if os.path.exists(file_path):
223-
globs = {}
224-
execfile(file_path, globs)
225-
if fixture in globs:
226-
return call_fixture(globs[fixture])
227-
else:
228-
return call_fixture(getattr(fixtures, fixture))
229-
return get_fixture(caller_module, fixture, path=os.path.dirname(path))
230-
231-
232206
def _get_scenario_decorator(feature, feature_name, scenario, scenario_name, caller_module, caller_function, encoding):
233207
"""Get scenario decorator."""
234208
g = locals()
@@ -298,9 +272,9 @@ def scenario(feature_name, scenario_name, encoding="utf-8", example_converters=N
298272
caller_function = caller_function or get_caller_function()
299273

300274
# Get the feature
301-
base_path = get_fixture(caller_module, "pytestbdd_feature_base_dir")
302-
strict_gherkin = get_fixture(caller_module, "pytestbdd_strict_gherkin")
303-
feature = Feature.get_feature(base_path, feature_name, encoding=encoding, strict_gherkin=strict_gherkin)
275+
base_dir = get_base_dir(caller_module)
276+
strict_gherkin = get_strict_gherkin()
277+
feature = Feature.get_feature(base_dir, feature_name, encoding=encoding, strict_gherkin=strict_gherkin)
304278

305279
# Get the sc_enario
306280
try:
@@ -330,6 +304,20 @@ def scenario(feature_name, scenario_name, encoding="utf-8", example_converters=N
330304
)
331305

332306

307+
def get_base_dir(caller_module):
308+
default_base_dir = os.path.dirname(caller_module.__file__)
309+
return get_from_ini('bdd_feature_base_dir', default_base_dir)
310+
311+
312+
def get_strict_gherkin():
313+
return get_from_ini('bdd_strict_gherkin', True)
314+
315+
316+
def get_from_ini(key, default):
317+
value = pytest.config.getini(key)
318+
return value if value != '' else default
319+
320+
333321
def make_python_name(string):
334322
"""Make python attribute name out of a given string."""
335323
string = re.sub(PYTHON_REPLACE_REGEX, "", string.replace(" ", "_"))
@@ -357,12 +345,12 @@ def scenarios(*feature_paths, **kwargs):
357345
"""
358346
frame = inspect.stack()[1]
359347
module = inspect.getmodule(frame[0])
360-
base_path = get_fixture(module, "pytestbdd_feature_base_dir")
361-
strict_gherkin = get_fixture(module, "pytestbdd_strict_gherkin")
348+
base_dir = get_base_dir(module)
349+
strict_gherkin = get_strict_gherkin()
362350
abs_feature_paths = []
363351
for path in feature_paths:
364352
if not os.path.isabs(path):
365-
path = os.path.abspath(os.path.join(base_path, path))
353+
path = os.path.abspath(os.path.join(base_dir, path))
366354
abs_feature_paths.append(path)
367355
found = False
368356

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Configuration for pytest runner."""
22

33
from pytest_bdd import given, when
4-
from pytest_bdd.fixtures import *
54

65
pytest_plugins = "pytester"
76

tests/feature/no_sctrict_gherkin_background.feature

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/feature/no_sctrict_gherkin_scenario.feature

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,60 @@
11
"""Test feature base dir."""
2-
import os.path
3-
42
import pytest
53

6-
from pytest_bdd import scenario
74

5+
@pytest.mark.parametrize(
6+
'base_dir', [
7+
'.',
8+
'/does/not/exist/',
9+
]
10+
)
11+
def test_feature_path_not_found(testdir, base_dir):
12+
"""Test feature base dir."""
13+
prepare_testdir(testdir, base_dir)
814

9-
@pytest.fixture(params=[
10-
'When step can be the first',
11-
])
12-
def scenario_name(request):
13-
return request.param
15+
result = testdir.runpytest('-k', 'test_not_found')
16+
result.assert_outcomes(passed=1)
1417

1518

16-
@pytest.fixture
17-
def pytestbdd_feature_base_dir():
18-
return '/does/not/exist'
19+
def test_feature_path_ok(testdir):
20+
base_dir = 'features'
21+
prepare_testdir(testdir, base_dir)
1922

23+
result = testdir.runpytest('-k', 'test_ok')
24+
result.assert_outcomes(passed=1)
2025

21-
def test_feature_path(request, scenario_name):
22-
"""Test feature base dir."""
23-
with pytest.raises(IOError) as exc:
26+
27+
def prepare_testdir(testdir, base_dir):
28+
testdir.makeini("""
29+
[pytest]
30+
bdd_feature_base_dir={}
31+
""".format(base_dir))
32+
33+
feature_file = testdir.mkdir('features').join('steps.feature')
34+
feature_file.write("""
35+
Scenario: When scenario found
36+
Given found
37+
""")
38+
39+
testdir.makepyfile("""
40+
import pytest
41+
from pytest_bdd import scenario
42+
import os.path
43+
44+
@pytest.fixture(params=[
45+
'When scenario found',
46+
])
47+
def scenario_name(request):
48+
return request.param
49+
50+
def test_not_found(scenario_name):
51+
base_dir = '{}'
52+
print("BS: %s" % base_dir)
53+
with pytest.raises(IOError) as exc:
54+
scenario('steps.feature', scenario_name)
55+
assert os.path.abspath(os.path.join(base_dir, 'steps.feature')) in str(exc.value)
56+
57+
def test_ok(scenario_name):
58+
# Shouldn't raise any exception
2459
scenario('steps.feature', scenario_name)
25-
assert os.path.join('/does/not/exist/', 'steps.feature') in str(exc.value)
60+
""".format(base_dir))
Lines changed: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,96 @@
11
"""Test no strict gherkin for sections."""
22

3-
import pytest
43

5-
from pytest_bdd import (
6-
when,
7-
scenario,
8-
)
4+
def test_background_no_strict_gherkin(testdir):
5+
"""Test background no strict gherkin."""
6+
prepare_test_dir(testdir)
97

8+
testdir.makefile('.feature', no_strict_gherkin_background="""
9+
Feature: No strict Gherkin Background support
1010
11-
@pytest.fixture
12-
def pytestbdd_strict_gherkin():
13-
return False
11+
Background:
12+
When foo has a value "bar"
13+
And foo is not boolean
14+
And foo has not a value "baz"
1415
16+
Scenario: Test background
1517
16-
def test_background_no_strict_gherkin(request):
17-
"""Test background no strict gherkin."""
18-
@scenario(
19-
"no_sctrict_gherkin_background.feature",
20-
"Test background",
21-
)
22-
def test():
23-
pass
18+
""")
2419

25-
test(request)
20+
result = testdir.runpytest('-k', 'test_background_ok')
21+
result.assert_outcomes(passed=1)
2622

2723

28-
def test_scenario_no_strict_gherkin(request):
24+
def test_scenario_no_strict_gherkin(testdir):
2925
"""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)
26+
prepare_test_dir(testdir)
3827

28+
testdir.makefile('.feature', no_strict_gherkin_scenario="""
29+
Feature: No strict Gherkin Scenario support
3930
40-
@pytest.fixture
41-
def foo():
42-
return {}
31+
Scenario: Test scenario
32+
When foo has a value "bar"
33+
And foo is not boolean
34+
And foo has not a value "baz"
4335
36+
""")
4437

45-
@when('foo has a value "bar"')
46-
def bar(foo):
47-
foo["bar"] = "bar"
48-
return foo["bar"]
38+
result = testdir.runpytest('-k', 'test_scenario_ok')
39+
result.assert_outcomes(passed=1)
4940

5041

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
42+
def prepare_test_dir(testdir):
43+
"""Test scenario no strict gherkin."""
44+
testdir.makeini("""
45+
[pytest]
46+
bdd_strict_gherkin=false
47+
"""
48+
)
49+
50+
testdir.makepyfile(test_gherkin="""
51+
import pytest
52+
53+
from pytest_bdd import (
54+
when,
55+
scenario,
56+
)
57+
58+
def test_scenario_ok(request):
59+
@scenario(
60+
"no_strict_gherkin_scenario.feature",
61+
"Test scenario",
62+
)
63+
def test():
64+
pass
65+
66+
test(request)
67+
68+
def test_background_ok(request):
69+
@scenario(
70+
"no_strict_gherkin_background.feature",
71+
"Test background",
72+
)
73+
def test():
74+
pass
75+
76+
test(request)
77+
78+
@pytest.fixture
79+
def foo():
80+
return {}
81+
82+
@when('foo has a value "bar"')
83+
def bar(foo):
84+
foo["bar"] = "bar"
85+
return foo["bar"]
86+
87+
88+
@when('foo is not boolean')
89+
def not_boolean(foo):
90+
assert foo is not bool
91+
92+
93+
@when('foo has not a value "baz"')
94+
def has_not_baz(foo):
95+
assert "baz" not in foo
96+
""")

tests/feature/test_wrong.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test wrong feature syntax."""
22
import os.path
33
import re
4+
import sys
45

56
import mock
67

@@ -38,7 +39,7 @@ def then_nevermind():
3839
)
3940
@pytest.mark.parametrize('strict_gherkin', [True, False])
4041
@pytest.mark.parametrize('multiple', [True, False])
41-
@mock.patch('pytest_bdd.fixtures.pytestbdd_strict_gherkin', autospec=True)
42+
@mock.patch.object(sys.modules['pytest_bdd.scenario'], 'get_strict_gherkin', return_value=True)
4243
def test_wrong(mocked_strict_gherkin, request, feature, scenario_name, strict_gherkin, multiple):
4344
"""Test wrong feature scenarios."""
4445
mocked_strict_gherkin.return_value = strict_gherkin

0 commit comments

Comments
 (0)