Skip to content

Commit 0f02d56

Browse files
authored
Merge pull request #420 from pytest-dev/simplify-code
Simplify code
2 parents 87d8e50 + 0930c8a commit 0f02d56

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

pytest_bdd/scenario.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
import re
1616

1717
import pytest
18-
19-
try:
20-
from _pytest import fixtures as pytest_fixtures
21-
except ImportError:
22-
from _pytest import python as pytest_fixtures
18+
from _pytest.fixtures import FixtureLookupError
2319

2420
from . import exceptions
2521
from .feature import get_feature, get_features
@@ -36,21 +32,25 @@ def find_argumented_step_fixture_name(name, type_, fixturemanager, request=None)
3632
for fixturename, fixturedefs in list(fixturemanager._arg2fixturedefs.items()):
3733
for fixturedef in fixturedefs:
3834
parser = getattr(fixturedef.func, "parser", None)
39-
match = parser.is_matching(name) if parser else None
40-
if match:
41-
converters = getattr(fixturedef.func, "converters", {})
42-
for arg, value in parser.parse_arguments(name).items():
43-
if arg in converters:
44-
value = converters[arg](value)
45-
if request:
46-
inject_fixture(request, arg, value)
47-
parser_name = get_step_fixture_name(parser.name, type_)
35+
if parser is None:
36+
continue
37+
match = parser.is_matching(name)
38+
if not match:
39+
continue
40+
41+
converters = getattr(fixturedef.func, "converters", {})
42+
for arg, value in parser.parse_arguments(name).items():
43+
if arg in converters:
44+
value = converters[arg](value)
4845
if request:
49-
try:
50-
request.getfixturevalue(parser_name)
51-
except pytest_fixtures.FixtureLookupError:
52-
continue
53-
return parser_name
46+
inject_fixture(request, arg, value)
47+
parser_name = get_step_fixture_name(parser.name, type_)
48+
if request:
49+
try:
50+
request.getfixturevalue(parser_name)
51+
except FixtureLookupError:
52+
continue
53+
return parser_name
5454

5555

5656
def _find_step_function(request, step, scenario):
@@ -67,14 +67,14 @@ def _find_step_function(request, step, scenario):
6767
try:
6868
# Simple case where no parser is used for the step
6969
return request.getfixturevalue(get_step_fixture_name(name, step.type))
70-
except pytest_fixtures.FixtureLookupError:
70+
except FixtureLookupError:
7171
try:
7272
# Could not find a fixture with the same name, let's see if there is a parser involved
7373
name = find_argumented_step_fixture_name(name, step.type, request._fixturemanager, request)
7474
if name:
7575
return request.getfixturevalue(name)
7676
raise
77-
except pytest_fixtures.FixtureLookupError:
77+
except FixtureLookupError:
7878
raise exceptions.StepDefinitionNotFoundError(
7979
f"Step definition is not found: {step}. "
8080
f'Line {step.line_number} in scenario "{scenario.name}" in the feature "{scenario.feature.filename}"'

pytest_bdd/steps.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ def given_beautiful_article(article):
3737

3838
import pytest
3939

40-
try:
41-
from _pytest import fixtures as pytest_fixtures
42-
except ImportError:
43-
from _pytest import python as pytest_fixtures
40+
from _pytest.fixtures import FixtureDef
4441

4542
from .types import GIVEN, WHEN, THEN
4643
from .parsers import get_parser
47-
from .utils import get_args, get_caller_module_locals
44+
from .utils import get_caller_module_locals
4845

4946

5047
def get_step_fixture_name(name, type_):
@@ -145,19 +142,15 @@ def inject_fixture(request, arg, value):
145142
:param arg: argument name
146143
:param value: argument value
147144
"""
148-
fd_kwargs = {
149-
"fixturemanager": request._fixturemanager,
150-
"baseid": None,
151-
"argname": arg,
152-
"func": lambda: value,
153-
"scope": "function",
154-
"params": None,
155-
}
156-
157-
if "yieldctx" in get_args(pytest_fixtures.FixtureDef.__init__):
158-
fd_kwargs["yieldctx"] = False
159-
160-
fd = pytest_fixtures.FixtureDef(**fd_kwargs)
145+
146+
fd = FixtureDef(
147+
fixturemanager=request._fixturemanager,
148+
baseid=None,
149+
argname=arg,
150+
func=lambda: value,
151+
scope="function",
152+
params=None,
153+
)
161154
fd.cached_result = (value, 0, None)
162155

163156
old_fd = request._fixture_defs.get(arg)

0 commit comments

Comments
 (0)