Skip to content

Commit b7183d3

Browse files
committed
Provide tests for string to array parameter issue fix
1 parent cd70795 commit b7183d3

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Feature: Scenario outline example converters
2+
Scenario: Scenario outline example converters
3+
Given example.feature with content:
4+
"""
5+
Feature: Scenario outline example converters
6+
Scenario Outline: Outline example converters
7+
Given step with <array> param
8+
9+
Examples:
10+
| array |
11+
| 0;1 |
12+
| 2;3 |
13+
"""
14+
And example_test.py with content:
15+
"""
16+
from pytest_bdd import scenario
17+
from pytest_bdd import given, then, when
18+
19+
def param_to_array(data):
20+
if data:
21+
if ";" in data:
22+
return [item for item in data.split(";")]
23+
# case for single item that should also be returned as an array item
24+
elif isinstance(data, str):
25+
return [data]
26+
else:
27+
return []
28+
29+
@given("step with <array> param")
30+
def then_step_with_array(array):
31+
pass
32+
33+
@scenario("example.feature", "Outline example converters", example_converters={'array':param_to_array})
34+
def test_scenario_outline_example():
35+
pass
36+
"""
37+
When run pytest-bdd with allure
38+
39+
Then allure report has result for "Outline example converters" scenario
40+
Then this scenario has parameter "array" with value "['0','1']"
41+
Then this scenario contains "Given step with <['0', '1']> param" step
42+
43+
Then allure report has result for "Outline example converters" scenario
44+
Then this scenario has parameter "array" with value "['2','3']"
45+
Then this scenario contains "Given step with <['2', '3']> param" step
46+
47+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pytest_bdd import scenario
2+
3+
4+
@scenario("../features/outline_converters.feature", "Scenario outline example converters")
5+
def test_scenario_outline():
6+
pass

allure-pytest-bdd/test/steps.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import literal_eval
12
from pytest_bdd import then
23
from pytest_bdd import parsers
34
from functools import partial
@@ -29,6 +30,12 @@ def match(matcher, *args):
2930
return matcher()
3031

3132

33+
def to_array(string):
34+
if string.startswith("[") and string.endswith("]"):
35+
return literal_eval(string)
36+
else:
37+
return string
38+
3239
@then(parsers.re("allure report has result for (?:\")(?P<scenario_name>[\\w|\\s|,]*)(?:\") scenario"))
3340
def match_scenario(allure_report, context, scenario_name):
3441
matcher = partial(match, has_test_case, scenario_name)
@@ -52,14 +59,15 @@ def item_history_id(allure_report, context, item):
5259

5360
@then(parsers.re("this (?P<item>\\w+) "
5461
"has parameter (?:\")(?P<param_name>[\\w|\\s]*)(?:\") "
55-
"with value (?:\")(?P<param_value>[\\w|\\s]*)(?:\")"))
62+
"with value (?:\")(?P<param_value>[\\w|\\s|,|\\[|\\]|<|>|']*)(?:\")"),
63+
converters={'param_value': to_array})
5664
def item_parameter(allure_report, context, item, param_name, param_value):
5765
context_matcher = context[item]
5866
matcher = partial(context_matcher, has_parameter, param_name, param_value)
5967
assert_that(allure_report, matcher())
6068

6169

62-
@then(parsers.re("this (?P<item>\\w+) contains (?:\")(?P<step>[\\w|\\s|>|<]+)(?:\") step"))
70+
@then(parsers.re("this (?P<item>\\w+) contains (?:\")(?P<step>[\\w|\\s|>|<|,|\\[|\\]|']+)(?:\") step"))
6371
def step_step(allure_report, context, item, step):
6472
context_matcher = context[item]
6573
matcher = partial(context_matcher, has_step, step)

0 commit comments

Comments
 (0)