Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d667b5e

Browse files
authored
Fix table data to csv conversion in allure-behave (allure-framework#718)
1 parent bbe1ee9 commit d667b5e

File tree

17 files changed

+182
-43
lines changed

17 files changed

+182
-43
lines changed

allure-behave/features/step.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,20 @@ Feature: Step
8282
And scenario contains step "Given passed step with attachment"
8383
And this step has attachment
8484
And this step has "passed" status
85+
86+
Scenario: Step with table data containing comma
87+
Given feature definition
88+
"""
89+
Feature: Step with data
90+
91+
Scenario: Step with a table data containing comma
92+
Given step with a table data
93+
|Items A|Items B|
94+
|Item 1, Item 2|Item 3, Item 4|
95+
"""
96+
When I run behave with allure formatter
97+
Then allure report has a scenario with name "Step with a table data containing comma"
98+
And scenario contains step "Given step with a table data"
99+
And this step has attachment ".table" with the following data
100+
|Items A|Items B|
101+
|Item 1, Item 2|Item 3, Item 4|

allure-behave/features/steps/report_steps.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
from functools import partial
22
from hamcrest import assert_that, contains_string
33
from hamcrest import not_
4+
from hamcrest import equal_to
5+
from allure_commons.types import AttachmentType
46
from allure_commons_test.report import has_test_case
57
from allure_commons_test.result import with_status
68
from allure_commons_test.result import has_step
79
from allure_commons_test.result import has_attachment
10+
from allure_commons_test.result import has_attachment_with_content
811
from allure_commons_test.result import has_parameter
912
from allure_commons_test.result import has_status_details
1013
from allure_commons_test.result import with_message_contains
1114
from allure_commons_test.result import has_link
1215
from allure_commons_test.result import has_description
1316
from allure_commons_test.container import has_container
1417
from allure_commons_test.container import has_before, has_after
18+
from allure_commons_test.content import csv_equivalent
1519
from allure_commons_test.label import has_severity
1620
from allure_commons_test.label import has_tag
1721
from allure_commons_test.label import has_label
@@ -152,8 +156,38 @@ def step_attachment(context, item):
152156
assert_that(context.allure_report, matcher())
153157

154158

159+
@then('this {item} has attachment "{name}" with the following data')
160+
def step_attachment_data(context, item, name):
161+
context_matcher = getattr(context, item)
162+
attachment_type, content_matcher = _get_attachment_type_and_matcher(context)
163+
matcher = partial(
164+
context_matcher,
165+
partial(
166+
has_attachment_with_content,
167+
context.allure_report.attachments,
168+
content_matcher,
169+
attachment_type.mime_type,
170+
name
171+
)
172+
)
173+
assert_that(context.allure_report, matcher())
174+
175+
155176
@then('scenario has description "{description}"')
156177
def step_description(context, description):
157178
context_matcher = context.scenario
158179
matcher = partial(context_matcher, has_description, contains_string(description))
159180
assert_that(context.allure_report, matcher())
181+
182+
def _get_attachment_type_and_matcher(context):
183+
return (
184+
AttachmentType.CSV,
185+
csv_equivalent(
186+
[context.table.headings] + [
187+
r.cells for r in context.table.rows
188+
]
189+
)
190+
) if context.table is not None else (
191+
AttachmentType.TEXT,
192+
equal_to(context.text)
193+
)

allure-behave/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[tool.poe.tasks]
2-
linter = "flake8 ./src"
2+
linter = "flake8 --extend-ignore=A003 ./src"
33
tests = """behave -f allure_behave.formatter:AllureFormatter -o allure-results
44
-f pretty ./features"""

allure-behave/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
poethepoet
22
# linters
3-
flake8==5.*
3+
flake8

allure-behave/src/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import csv
2+
import io
13
from enum import Enum
24
from behave.runner_util import make_undefined_step_snippet
35
from allure_commons.types import Severity, LabelType
@@ -124,10 +126,11 @@ def step_status_details(result):
124126

125127

126128
def step_table(step):
127-
table = [','.join(step.table.headings)]
128-
for row in step.table.rows:
129-
table.append(','.join(list(row)))
130-
return '\n'.join(table)
129+
with io.StringIO() as buffer:
130+
writer = csv.writer(buffer)
131+
writer.writerow(step.table.headings)
132+
writer.writerows(r.cells for r in step.table.rows)
133+
return buffer.getvalue()
131134

132135

133136
def is_planned_scenario(scenario, test_plan):

allure-nose2/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
poethepoet
22
# linters
3-
flake8==4.*
3+
flake8
44
flake8-builtins

allure-pytest-bdd/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mock
22
poethepoet
33
# linters
4-
flake8==4.*
4+
flake8
55
flake8-builtins

allure-pytest/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ pytest-xdist
88
pytest-lazy-fixture
99
poethepoet
1010
# linters
11-
flake8==5.*
11+
flake8
1212
flake8-builtins

allure-python-commons-test/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
pyhamcrest
33
poethepoet
44
# linters
5-
flake8==5.*
5+
flake8
66
flake8-builtins

allure-python-commons-test/src/container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def _matches(self, item):
2121
def describe_to(self, description):
2222
description.append_text('describe me later').append_list('[', ', ', ']', self.matchers)
2323

24-
def describe_mismatch(self, item, mismatch_descaription):
25-
self.matches(item, mismatch_descaription)
24+
def describe_mismatch(self, item, mismatch_description):
25+
self.matches(item, mismatch_description)
2626

2727

2828
def has_container(report, *matchers):

0 commit comments

Comments
 (0)