Skip to content

Fix integration for Allure and pytest_bdd when test parameter is dict #660

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

Closed
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
17 changes: 7 additions & 10 deletions allure-pytest-bdd/features/outline.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ Feature: Scenario outline
"""
from pytest_bdd import scenario
from pytest_bdd import given, then, when
from pytest_bdd.parsers import cfparse

@given("<first> step")
@given(cfparse("{first} step"))
def given_step(first):
pass

@when("do nothing")
def nope_step():
pass

@then("step with <second> param")
@then(cfparse("step with {second} param"))
def then_step(second):
pass

Expand All @@ -37,15 +38,11 @@ Feature: Scenario outline
When run pytest-bdd with allure

Then allure report has result for "Outline example" scenario
Then this scenario has parameter "first" with value "Alpha"
Then this scenario has parameter "second" with value "1"
Then this scenario contains "Given <Alpha> step" step
Then this scenario contains "Then step with <1> param" step
Then this scenario contains "Given Alpha step" step
Then this scenario contains "Then step with 1 param" step

Then allure report has result for "Outline example" scenario
Then this scenario has parameter "first" with value "Bravo"
Then this scenario has parameter "second" with value "2"
Then this scenario contains "Given <Bravo> step" step
Then this scenario contains "Then step with <2> param" step
Then this scenario contains "Given Bravo step" step
Then this scenario contains "Then step with 2 param" step


2 changes: 1 addition & 1 deletion allure-pytest-bdd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

install_requires = [
"pytest>=4.5.0",
"pytest-bdd>=3.0.0",
"pytest-bdd>=5.0.0",
"six>=1.9.0",
]

Expand Down
2 changes: 2 additions & 0 deletions allure-pytest-bdd/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from .steps import * # noqa F401 F403
from pytest_bdd import given, when, parsers

pytest_plugins = ["pytester"]


@contextmanager
def fake_logger(path, logger):
Expand Down
24 changes: 19 additions & 5 deletions allure-python-commons/src/logger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import errno
import io
import json
import os
import shutil
import sys
import json
import uuid
import shutil
from six import text_type
from attr import asdict
from functools import partial

from allure_commons import hookimpl
from allure_commons.model2 import Parameter
from attr import asdict
from six import text_type

INDENT = 4

Expand All @@ -31,7 +34,18 @@ def __init__(self, report_dir, clean=False):
def _report_item(self, item):
indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None
filename = item.file_pattern.format(prefix=uuid.uuid4())
data = asdict(item, filter=lambda attr, value: not (type(value) != bool and not bool(value)))

def serializer(instance, attr, value):
if isinstance(instance, Parameter) and attr.name == 'value':
return (partial(json.dumps, indent=indent) if isinstance(value, dict) else str)(value)
else:
return value
data = asdict(
item,
filter=lambda attr, value: not (type(value) != bool and not bool(value)),
value_serializer=serializer
)

with io.open(os.path.join(self._report_dir, filename), 'w', encoding='utf8') as json_file:
if sys.version_info.major < 3:
json_file.write(
Expand Down