Skip to content

Commit 0d28836

Browse files
authored
Wrong attachments for parameterized (fixes #207 fixes #223 via #228)
1 parent 2cd2d78 commit 0d28836

File tree

9 files changed

+166
-70
lines changed

9 files changed

+166
-70
lines changed

allure-pytest/src/listener.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,52 @@ def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
5656
status=get_status(exc_val),
5757
statusDetails=get_status_details(exc_type, exc_val, exc_tb))
5858

59-
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
60-
def pytest_runtest_protocol(self, item, nextitem):
59+
@pytest.hookimpl(hookwrapper=True)
60+
def pytest_runtest_protocol(self, item):
6161
uuid = self._cache.set(item.nodeid)
62+
test_result = TestResult(name=item.name, uuid=uuid)
63+
self.allure_logger.schedule_test(uuid, test_result)
64+
yield
65+
uuid = self._cache.pop(item.nodeid)
66+
self.allure_logger.close_test(uuid)
67+
68+
@pytest.hookimpl(hookwrapper=True)
69+
def pytest_runtest_setup(self, item):
70+
yield
71+
uuid = self._cache.get(item.nodeid)
72+
test_result = self.allure_logger.get_test(uuid)
6273
for fixturedef in _test_fixtures(item):
6374
group_uuid = self._cache.get(fixturedef)
6475
if not group_uuid:
6576
group_uuid = self._cache.set(fixturedef)
6677
group = TestResultContainer(uuid=group_uuid)
6778
self.allure_logger.start_group(group_uuid, group)
6879
self.allure_logger.update_group(group_uuid, children=uuid)
69-
7080
params = item.callspec.params if hasattr(item, 'callspec') else {}
7181

72-
test_result = TestResult(name=allure_name(item, params), uuid=uuid)
82+
test_result.name = allure_name(item, params)
7383
test_result.description = allure_description(item)
7484
test_result.descriptionHtml = allure_description_html(item)
7585
test_result.fullName = allure_full_name(item)
7686
test_result.historyId = md5(test_result.fullName)
77-
test_result.parameters.extend([Parameter(name=name, value=represent(value)) for name, value in params.items()])
78-
79-
self.allure_logger.schedule_test(uuid, test_result)
87+
test_result.parameters.extend(
88+
[Parameter(name=name, value=represent(value)) for name, value in params.items()])
8089

90+
@pytest.hookimpl(hookwrapper=True)
91+
def pytest_runtest_call(self, item):
92+
uuid = self._cache.get(item.nodeid)
93+
test_result = self.allure_logger.get_test(uuid)
94+
if test_result:
95+
test_result.start = now()
8196
yield
97+
if test_result:
98+
test_result.stop = now()
8299

100+
@pytest.hookimpl(hookwrapper=True)
101+
def pytest_runtest_teardown(self, item):
102+
yield
103+
uuid = self._cache.get(item.nodeid)
104+
test_result = self.allure_logger.get_test(uuid)
83105
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
84106
test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
85107
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
@@ -89,21 +111,6 @@ def pytest_runtest_protocol(self, item, nextitem):
89111
test_result.labels.append(Label(name='package', value=allure_package(item)))
90112
test_result.links.extend([Link(link_type, url, name) for link_type, url, name in allure_links(item)])
91113

92-
uuid = self._cache.pop(item.nodeid)
93-
self.allure_logger.close_test(uuid)
94-
95-
@pytest.hookimpl(hookwrapper=True)
96-
def pytest_runtest_call(self, item):
97-
uuid = self._cache.get(item.nodeid)
98-
test_result = self.allure_logger.get_test(uuid)
99-
if test_result:
100-
test_result.start = now()
101-
102-
yield
103-
104-
if test_result:
105-
test_result.stop = now()
106-
107114
@pytest.hookimpl(hookwrapper=True)
108115
def pytest_fixture_setup(self, fixturedef, request):
109116
fixture_name = fixturedef.argname

allure-pytest/test/attachments/attachments_fixtures_test.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
"""
2-
>>> allure_report = getfixture('allure_report')
3-
>>> assert_that(allure_report,
4-
... all_of(
5-
... has_property('test_cases', has_length(8)),
6-
... has_property('test_groups', has_length(2)),
7-
... has_property('attachments', has_length(6))
8-
... )) # doctest: +SKIP
9-
"""
10-
111
import pytest
12-
import tempfile
13-
from six import text_type
2+
import allure
143

154
TEXT = "attachment body"
165

176

187
@pytest.fixture
198
def attach_file_in_function_scope_fixture(svg_file):
20-
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
9+
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
2110

2211

2312
def test_attach_file_in_function_scope_fixture(attach_file_in_function_scope_fixture):
@@ -55,7 +44,7 @@ def test_attach_file_in_reused_function_scope_fixture(attach_file_in_function_sc
5544
@pytest.fixture
5645
def attach_file_in_function_scope_finalizer(svg_file, request):
5746
def finalizer_function_scope_fixture():
58-
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
47+
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
5948
request.addfinalizer(finalizer_function_scope_fixture)
6049

6150

@@ -93,7 +82,7 @@ def test_attach_file_in_reused_function_scope_finalizer(attach_file_in_function_
9382

9483
@pytest.fixture(scope='module')
9584
def attach_data_in_module_scope_fixture():
96-
pytest.allure.attach(TEXT, attachment_type='text/plain')
85+
allure.attach(TEXT, attachment_type='text/plain')
9786

9887

9988
def test_attach_data_in_module_scope_fixture(attach_data_in_module_scope_fixture):
@@ -131,7 +120,7 @@ def test_attach_data_in_reused_module_scope_fixture(attach_data_in_module_scope_
131120
@pytest.fixture(scope='module')
132121
def attach_data_in_module_scope_finalizer(request):
133122
def finalizer_module_scope_fixture():
134-
pytest.allure.attach(TEXT, attachment_type='text/plain')
123+
allure.attach(TEXT, attachment_type='text/plain')
135124
request.addfinalizer(finalizer_module_scope_fixture)
136125

137126

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
import allure
3+
4+
5+
PARAMS = ["first", "second", "third"]
6+
7+
8+
@pytest.fixture(scope='module', params=PARAMS)
9+
def attach_data_in_parametrized_fixture(request):
10+
allure.attach(request.param, name=request.param, attachment_type='text/plain')
11+
12+
13+
def test_attach_data_in_parametrized_fixture(attach_data_in_parametrized_fixture):
14+
"""
15+
>>> allure_report = getfixture('allure_report')
16+
>>> for param in PARAMS:
17+
... assert_that(allure_report,
18+
... has_test_case('test_attach_data_in_parametrized_fixture[{param}]'.format(param=param),
19+
... has_container(allure_report,
20+
... has_before('attach_data_in_parametrized_fixture',
21+
... has_attachment(attach_type='text/plain', name=param)
22+
... )
23+
... )
24+
... )
25+
... )
26+
"""
27+
pass

allure-pytest/test/attachments/attachments_in_parametrized_test.py renamed to allure-pytest/test/attachments/attachments_parametrized_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
"""
2-
>>> allure_report = getfixture('allure_report')
3-
>>> assert_that(allure_report,
4-
... all_of(
5-
... has_property('test_cases', has_length(9)),
6-
... has_property('test_groups', has_length(0)),
7-
... has_property('attachments', has_length(11))
8-
... )) # doctest: +SKIP
9-
"""
10-
111
import allure
122
import pytest
133

allure-pytest/test/attachments/attachments_test.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
"""
2-
>>> allure_report = getfixture('allure_report')
3-
>>> assert_that(allure_report,
4-
... all_of(
5-
... has_property('test_cases', has_length(9)),
6-
... has_property('test_groups', has_length(0)),
7-
... has_property('attachments', has_length(11))
8-
... )) # doctest: +SKIP
9-
"""
10-
111
import pytest
2+
import allure
123

134

145
def test_attach_file_from_test(xml_file):
@@ -20,7 +11,7 @@ def test_attach_file_from_test(xml_file):
2011
... )
2112
... )
2213
"""
23-
pytest.allure.attach.file(xml_file)
14+
allure.attach.file(xml_file)
2415

2516

2617
def test_attach_data_from_test(xml_body):
@@ -32,7 +23,7 @@ def test_attach_data_from_test(xml_body):
3223
... )
3324
... )
3425
"""
35-
pytest.allure.attach(xml_body)
26+
allure.attach(xml_body)
3627

3728

3829
def test_attach_file_from_test_with_name_and_type(xml_file):
@@ -43,7 +34,7 @@ def test_attach_file_from_test_with_name_and_type(xml_file):
4334
... has_attachment(attach_type='application/xml', name='my name')
4435
... ))
4536
"""
46-
pytest.allure.attach.file(xml_file, name='my name', attachment_type=pytest.allure.attachment_type.XML)
37+
allure.attach.file(xml_file, name='my name', attachment_type=pytest.allure.attachment_type.XML)
4738

4839

4940
def test_attach_data_from_test_with_name_and_type(xml_body):
@@ -54,7 +45,7 @@ def test_attach_data_from_test_with_name_and_type(xml_body):
5445
... has_attachment(attach_type='application/xml', name='my name')
5546
... ))
5647
"""
57-
pytest.allure.attach(xml_body, name='my name', attachment_type=pytest.allure.attachment_type.XML)
48+
allure.attach(xml_body, name='my name', attachment_type=pytest.allure.attachment_type.XML)
5849

5950

6051
def test_attach_file_from_test_with_user_type(xml_file):
@@ -65,7 +56,7 @@ def test_attach_file_from_test_with_user_type(xml_file):
6556
... has_attachment(attach_type='text/xml')
6657
... ))
6758
"""
68-
pytest.allure.attach.file(xml_file, attachment_type='text/xml')
59+
allure.attach.file(xml_file, attachment_type='text/xml')
6960

7061

7162
def test_attach_data_from_test_with_user_type(xml_body):
@@ -76,11 +67,11 @@ def test_attach_data_from_test_with_user_type(xml_body):
7667
... has_attachment(attach_type='text/xml')
7768
... ))
7869
"""
79-
pytest.allure.attach(xml_body, attachment_type='text/xml')
70+
allure.attach(xml_body, attachment_type='text/xml')
8071

8172

8273
def attach_svg_file(svg_file):
83-
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
74+
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
8475

8576

8677
def test_attach_file_from_function(svg_file):
@@ -105,7 +96,7 @@ def test_many_attaches(svg_file, xml_body):
10596
... ))
10697
"""
10798
attach_svg_file(svg_file)
108-
pytest.allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
99+
allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
109100

110101

111102
def test_attach_from_step(svg_file, xml_body):
@@ -123,7 +114,7 @@ def test_attach_from_step(svg_file, xml_body):
123114
... )
124115
... )
125116
"""
126-
with pytest.allure.step('Step with attachment'):
127-
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
117+
with allure.step('Step with attachment'):
118+
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
128119
with pytest.allure.step('Nested step with attachment'):
129-
pytest.allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
120+
allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)

allure-pytest/test/fixtures/function_scope/fixtures_parametrization_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ def test_two_function_scope_parametrized_fixture(parametrized_fixture, parametri
6868
... )
6969
... )
7070
"""
71-
assert parametrized_fixture_with_ids and parametrized_fixture
71+
assert parametrized_fixture_with_ids and parametrized_fixture

allure-pytest/test/fixtures/module_scope/__init__.py

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
import allure
3+
4+
5+
PARAMS = ["first", "second", "third"]
6+
7+
8+
@pytest.fixture(scope='module', params=PARAMS)
9+
def module_scope_parametrized_fixture():
10+
pass
11+
12+
13+
def test_module_scope_parametrized_fixture(module_scope_parametrized_fixture):
14+
"""
15+
>>> allure_report = getfixture('allure_report')
16+
17+
>>> for param in PARAMS:
18+
... assert_that(allure_report,
19+
... has_test_case('test_module_scope_parametrized_fixture[{param}]'.format(param=param),
20+
... has_container(allure_report,
21+
... has_before('module_scope_parametrized_fixture')
22+
... )
23+
... )
24+
... )
25+
"""
26+
pass
27+
28+
29+
def test_reuse_module_scope_parametrized_fixture(module_scope_parametrized_fixture):
30+
"""
31+
>>> allure_report = getfixture('allure_report')
32+
33+
>>> for param in PARAMS:
34+
... assert_that(allure_report,
35+
... has_test_case('test_reuse_module_scope_parametrized_fixture[{param}]'.format(param=param),
36+
... has_container(allure_report,
37+
... has_before('module_scope_parametrized_fixture')
38+
... )
39+
... )
40+
... )
41+
42+
>>> for param in PARAMS:
43+
... assert_that(allure_report,
44+
... has_same_container('test_module_scope_parametrized_fixture[{param}]'.format(param=param),
45+
... 'test_reuse_module_scope_parametrized_fixture[{param}]'.format(param=param),
46+
... has_before('module_scope_parametrized_fixture')
47+
... )
48+
... )
49+
"""
50+
pass
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope='module')
5+
def module_scope_simple_fixture():
6+
pass
7+
8+
9+
def test_module_scope_simple_fixture(module_scope_simple_fixture):
10+
"""
11+
>>> allure_report = getfixture('allure_report')
12+
>>> assert_that(allure_report,
13+
... has_test_case('test_module_scope_simple_fixture',
14+
... has_container(allure_report,
15+
... has_before('module_scope_simple_fixture')
16+
... )
17+
... )
18+
... )
19+
"""
20+
pass
21+
22+
23+
def test_reuse_module_scope_simple_fixture(module_scope_simple_fixture):
24+
"""
25+
>>> allure_report = getfixture('allure_report')
26+
27+
>>> assert_that(allure_report,
28+
... has_test_case('test_reuse_module_scope_simple_fixture',
29+
... has_container(allure_report,
30+
... has_before('module_scope_simple_fixture')
31+
... )
32+
... )
33+
... )
34+
35+
>>> assert_that(allure_report,
36+
... has_same_container('test_module_scope_simple_fixture',
37+
... 'test_reuse_module_scope_simple_fixture',
38+
... has_before('module_scope_simple_fixture')
39+
... )
40+
... )
41+
"""
42+
pass

0 commit comments

Comments
 (0)