Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialization issue when parameter value is array #638

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions allure-pytest-bdd/features/outline_converters.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Feature: Scenario outline example converters
Scenario: Scenario outline example converters
Given example.feature with content:
"""
Feature: Scenario outline example converters
Scenario Outline: Outline example converters
Given step with <array> param

Examples:
| array |
| 0;1 |
| 2;3 |
"""
And example_test.py with content:
"""
from pytest_bdd import scenario
from pytest_bdd import given, then, when

def param_to_array(data):
if data:
if ";" in data:
return [item for item in data.split(";")]
# case for single item that should also be returned as an array item
elif isinstance(data, str):
return [data]
else:
return []

@given("step with <array> param")
def then_step_with_array(array):
pass

@scenario("example.feature", "Outline example converters", example_converters={'array':param_to_array})
def test_scenario_outline_example():
pass
"""
When run pytest-bdd with allure

Then allure report has result for "Outline example converters" scenario
Then this scenario has parameter "array" with value "['0','1']"
Then this scenario contains "Given step with <['0', '1']> param" step

Then allure report has result for "Outline example converters" scenario
Then this scenario has parameter "array" with value "['2','3']"
Then this scenario contains "Given step with <['2', '3']> param" step


2 changes: 1 addition & 1 deletion allure-pytest-bdd/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def get_pytest_report_status(pytest_report):
def get_params(node):
if hasattr(node, 'callspec'):
params = node.callspec.params
return [Parameter(name=name, value=value) for name, value in params.items()]
return [Parameter(name=name, value=str(value)) for name, value in params.items()]
6 changes: 6 additions & 0 deletions allure-pytest-bdd/test/outline_converters_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pytest_bdd import scenario


@scenario("../features/outline_converters.feature", "Scenario outline example converters")
def test_scenario_outline():
pass
13 changes: 11 additions & 2 deletions allure-pytest-bdd/test/steps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import literal_eval
from pytest_bdd import then
from pytest_bdd import parsers
from functools import partial
Expand Down Expand Up @@ -29,6 +30,13 @@ def match(matcher, *args):
return matcher()


def to_array(string):
if string.startswith("[") and string.endswith("]"):
return literal_eval(string)
else:
return string


@then(parsers.re("allure report has result for (?:\")(?P<scenario_name>[\\w|\\s|,]*)(?:\") scenario"))
def match_scenario(allure_report, context, scenario_name):
matcher = partial(match, has_test_case, scenario_name)
Expand All @@ -52,14 +60,15 @@ def item_history_id(allure_report, context, item):

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


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