Skip to content

Commit a26f453

Browse files
Use parametrize markers params for getting example_kwargs
1 parent 274782f commit a26f453

File tree

8 files changed

+91
-15
lines changed

8 files changed

+91
-15
lines changed

pytest_bdd/reporting.py

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

99
from .feature import force_unicode
10-
from .utils import get_parametrize_markers_args
10+
from .utils import get_parametrize_markers_args, get_parametrize_params
1111

1212

1313
class StepReport(object):
@@ -73,20 +73,30 @@ def __init__(self, scenario, node):
7373
"""
7474
self.scenario = scenario
7575
self.step_reports = []
76-
self.param_index = None
76+
7777
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]
78+
params = get_parametrize_params(parametrize_args)
79+
80+
self.param_index = self.get_param_index(node, params)
81+
self.example_kwargs = self.get_example_kwargs(node, params)
82+
83+
def get_param_index(self, node, params):
84+
if params:
85+
param_names = params[0]['names']
86+
param_values = params[0]['values']
8287
node_param_values = [node.funcargs[param_name] for param_name in param_names]
8388
if node_param_values in param_values:
84-
self.param_index = param_values.index(node_param_values)
89+
return param_values.index(node_param_values)
8590
elif tuple(node_param_values) in param_values:
86-
self.param_index = param_values.index(tuple(node_param_values))
87-
self.example_kwargs = {
88-
example_param: force_unicode(node.funcargs[example_param])
89-
for example_param in scenario.get_example_params()
91+
return param_values.index(tuple(node_param_values))
92+
return None
93+
94+
def get_example_kwargs(self, node, params):
95+
params_names = (param['names'] for param in params)
96+
all_names = sum(params_names, [])
97+
return {
98+
example_param_name: force_unicode(node.funcargs[example_param_name])
99+
for example_param_name in all_names
90100
}
91101

92102
@property

pytest_bdd/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,33 @@ def get_markers_args_using_iter_markers(node, mark_name):
110110
def get_markers_args_using_get_marker(node, mark_name):
111111
"""Deprecated on pytest>=3.6"""
112112
return getattr(node.get_marker(mark_name), 'args', ())
113+
114+
115+
def get_parametrize_params(parametrize_args):
116+
"""Group parametrize markers arugments names and values.
117+
118+
:param marker_args: json-serialized `Scenario` or `Feature`.
119+
:return: `list` of `dict` in the form of:
120+
[
121+
{
122+
"names": ["name1", "name2", ...],
123+
"values": [value1, value2, ...],
124+
},
125+
...
126+
]
127+
"""
128+
params = []
129+
for i in range(0, len(parametrize_args), 2):
130+
params.append({
131+
'names': _get_param_names(parametrize_args[i]),
132+
'values': parametrize_args[i+1]
133+
})
134+
return params
135+
136+
137+
def _get_param_names(names):
138+
if not isinstance(names, (tuple, list)):
139+
# As pytest.mark.parametrize has only one param name,
140+
# it is not returned as a list. Convert it to list:
141+
names = [names]
142+
return names

tests/feature/gherkin_terminal_reporter.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@ Feature: Gherkin terminal reporter
4343

4444
Scenario: Should step parameters be replaced by their values
4545
Given there is gherkin scenario outline implemented
46-
When tests are run with step expanded mode
46+
When tests are run with step expanded option
47+
Then output must contain parameters values
48+
49+
Scenario: Should step parameters be replaced by their values also when used together with gherkin reporter option
50+
Given there is gherkin scenario outline implemented
51+
When tests are run with step expanded and gherkin reporter options
4752
Then output must contain parameters values

tests/feature/outline_feature.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Feature: Outline
44
| start | eat | left |
55
| 12 | 5 | 7 |
66
| 5 | 4 | 1 |
7+
| 4 | 2 | 2 |
78

89
Scenario Outline: Outlined given, when, thens
910
Given there are <start> <fruits>

tests/feature/parametrized.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ Scenario: Parametrized given, when, thens
22
Given there are <start> cucumbers
33
When I eat <eat> cucumbers
44
Then I should have <left> cucumbers
5+
6+
7+
Scenario: Parametrized given - single param
8+
Given there are <start> cucumbers

tests/feature/test_gherkin_terminal_reporter.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def test_Should_step_parameters_be_replaced_by_their_values():
6060
pass
6161

6262

63+
@scenario('gherkin_terminal_reporter.feature',
64+
'Should step parameters be replaced by their values also when used together with gherkin reporter option')
65+
def test_Should_step_parameters_be_replaced_by_their_values_also_when_used_together_with_gherkin_reporter_option():
66+
pass
67+
68+
6369
@pytest.fixture(params=[0, 1, 2],
6470
ids=['compact mode', 'line per test', 'verbose'])
6571
def verbosity_mode(request):
@@ -184,8 +190,17 @@ def tests_are_run_with_very_verbose_mode(testdir, test_execution):
184190
test_execution['gherkin'] = testdir.runpytest('--gherkin-terminal-reporter', '-vv')
185191

186192

187-
@when("tests are run with step expanded mode")
188-
def tests_are_run_with_step_expanded_mode(testdir, test_execution):
193+
@when("tests are run with step expanded option")
194+
def tests_are_run_with_step_expanded_option(testdir, test_execution):
195+
test_execution['regular'] = testdir.runpytest('-vv')
196+
test_execution['gherkin'] = testdir.runpytest(
197+
'--gherkin-terminal-reporter-expanded',
198+
'-vv',
199+
)
200+
201+
202+
@when("tests are run with step expanded and gherkin reporter options")
203+
def tests_are_run_with_step_expanded_and_gherkin_reporter_options(testdir, test_execution):
189204
test_execution['regular'] = testdir.runpytest('-vv')
190205
test_execution['gherkin'] = testdir.runpytest(
191206
'--gherkin-terminal-reporter',

tests/feature/test_outline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def should_have_left_fruits(start_fruits, start, eat, left, fruits):
168168
def test_outlined_feature(request):
169169
assert get_parametrize_markers_args(request.node) == (
170170
['start', 'eat', 'left'],
171-
[[12, 5.0, '7'], [5, 4.0, '1']],
171+
[[12, 5.0, '7'], [5, 4.0, '1'], [4, 2.0, '2']],
172172
['fruits'],
173173
[[u'oranges'], [u'apples']]
174174
)

tests/feature/test_parametrized.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ def test_parametrized(request, start, eat, left):
1414
"""Test parametrized scenario."""
1515

1616

17+
@pytest.mark.parametrize(
18+
'start', [12]
19+
)
20+
@scenario(
21+
'parametrized.feature',
22+
'Parametrized given - single param',
23+
)
24+
def test_parametrized_single_param(request, start):
25+
"""Test parametrized scenario."""
26+
27+
1728
@pytest.fixture(params=[1, 2])
1829
def foo_bar(request):
1930
return 'bar' * request.param

0 commit comments

Comments
 (0)