Skip to content

Commit 5678a5c

Browse files
sliwinski-miloszyoutux
authored andcommitted
Do not use deprecated MarkInfo objects on pytest>=3.6 (#262)
* Do not use deprecated MarkInfo objects on pytest>=3.6 * Provide way to get all parametrize args, not only the ones from the closest marker * Fix for remaining tests that were using depracated MarkInfo objects
1 parent f1a3e45 commit 5678a5c

File tree

4 files changed

+75
-34
lines changed

4 files changed

+75
-34
lines changed

pytest_bdd/reporting.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88

99
from .feature import force_unicode
10+
from .utils import get_parametrize_markers_args
1011

1112

1213
class StepReport(object):
@@ -73,11 +74,11 @@ def __init__(self, scenario, node):
7374
self.scenario = scenario
7475
self.step_reports = []
7576
self.param_index = None
76-
parametrize = node.keywords._markers.get('parametrize')
77-
if parametrize and scenario.examples:
78-
param_names = parametrize.args[0] if isinstance(parametrize.args[0], (tuple, list)) else [
79-
parametrize.args[0]]
80-
param_values = parametrize.args[1]
77+
parametrize_args = get_parametrize_markers_args(node)
78+
if parametrize_args and scenario.examples:
79+
param_names = parametrize_args[0] if isinstance(parametrize_args[0], (tuple, list)) else [
80+
parametrize_args[0]]
81+
param_values = parametrize_args[1]
8182
node_param_values = [node.funcargs[param_name] for param_name in param_names]
8283
if node_param_values in param_values:
8384
self.param_index = param_values.index(node_param_values)

pytest_bdd/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,29 @@ def get_request_fixture_names(request):
8484
Compatibility with pytest 3.0.
8585
"""
8686
return request._pyfuncitem._fixtureinfo.names_closure
87+
88+
89+
def get_parametrize_markers_args(node):
90+
"""In pytest 3.6 new API to access markers has been introduced and it deprecated
91+
MarkInfo objects.
92+
93+
This function uses that API if it is available otherwise it uses MarkInfo objects.
94+
"""
95+
mark_name = 'parametrize'
96+
try:
97+
return get_markers_args_using_iter_markers(node, mark_name)
98+
except AttributeError:
99+
return get_markers_args_using_get_marker(node, mark_name)
100+
101+
102+
def get_markers_args_using_iter_markers(node, mark_name):
103+
"""Recommended on pytest>=3.6"""
104+
args = []
105+
for mark in node.iter_markers(mark_name):
106+
args += mark.args
107+
return tuple(args)
108+
109+
110+
def get_markers_args_using_get_marker(node, mark_name):
111+
"""Deprecated on pytest>=3.6"""
112+
return getattr(node.get_marker(mark_name), 'args', ())

tests/feature/test_outline.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
from pytest_bdd import given, when, then, scenario
88
from pytest_bdd import exceptions
9+
from pytest_bdd.utils import get_parametrize_markers_args
910

1011

1112
@scenario(
1213
'outline.feature',
1314
'Outlined given, when, thens',
1415
example_converters=dict(start=int, eat=float, left=str)
1516
)
16-
def test_outlined():
17-
assert test_outlined.parametrize.args == (
17+
def test_outlined(request):
18+
assert get_parametrize_markers_args(request.node) == (
1819
[u'start', u'eat', u'left'], [[12, 5.0, '7'], [5, 4.0, '1']])
1920

2021

@@ -133,35 +134,12 @@ def test_outlined_with_other_fixtures(other_fixture):
133134
'Outlined with vertical example table',
134135
example_converters=dict(start=int, eat=float, left=str)
135136
)
136-
def test_vertical_example():
137+
def test_vertical_example(request):
137138
"""Test outlined scenario with vertical examples table."""
138-
assert test_vertical_example.parametrize.args == (
139+
assert get_parametrize_markers_args(request.node) == (
139140
[u'start', u'eat', u'left'], [[12, 5.0, '7'], [2, 1.0, '1']])
140141

141142

142-
def test_empty_example_values():
143-
"""Test outlined scenario with empty example values."""
144-
@scenario(
145-
'outline.feature',
146-
'Outlined with empty example values',
147-
)
148-
def test_scenario():
149-
pass
150-
151-
assert test_scenario.parametrize.args == (
152-
[u'start', u'eat', u'left'], [['#', '', '']])
153-
154-
@scenario(
155-
'outline.feature',
156-
'Outlined with empty example values vertical',
157-
)
158-
def test_scenario():
159-
pass
160-
161-
assert test_scenario.parametrize.args == (
162-
[u'start', u'eat', u'left'], [['#', '', '']])
163-
164-
165143
@given('there are <start> <fruits>')
166144
def start_fruits(start, fruits):
167145
assert isinstance(start, int)
@@ -187,8 +165,8 @@ def should_have_left_fruits(start_fruits, start, eat, left, fruits):
187165
'Outlined given, when, thens',
188166
example_converters=dict(start=int, eat=float, left=str)
189167
)
190-
def test_outlined_feature():
191-
assert test_outlined_feature.parametrize.args == (
168+
def test_outlined_feature(request):
169+
assert get_parametrize_markers_args(request.node) == (
192170
['start', 'eat', 'left'],
193171
[[12, 5.0, '7'], [5, 4.0, '1']],
194172
['fruits'],
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Scenario Outline with empty example values tests."""
2+
from pytest_bdd import given, scenario, then, when
3+
from pytest_bdd.utils import get_parametrize_markers_args
4+
5+
6+
@given('there are <start> cucumbers')
7+
def start_cucumbers(start):
8+
pass
9+
10+
11+
@when('I eat <eat> cucumbers')
12+
def eat_cucumbers(eat):
13+
pass
14+
15+
16+
@then('I should have <left> cucumbers')
17+
def should_have_left_cucumbers(left):
18+
pass
19+
20+
21+
@scenario(
22+
'outline.feature',
23+
'Outlined with empty example values',
24+
)
25+
def test_scenario_with_empty_example_values(request):
26+
assert get_parametrize_markers_args(request.node) == (
27+
[u'start', u'eat', u'left'], [['#', '', '']])
28+
29+
30+
@scenario(
31+
'outline.feature',
32+
'Outlined with empty example values vertical',
33+
)
34+
def test_scenario_with_empty_example_values_vertical(request):
35+
assert get_parametrize_markers_args(request.node) == (
36+
[u'start', u'eat', u'left'], [['#', '', '']])

0 commit comments

Comments
 (0)