Skip to content

Commit 4d67928

Browse files
authored
Don't convert built-in (reserved) pytest markers to allure tags (fix #817) (#862)
1 parent 9d12890 commit 4d67928

File tree

3 files changed

+120
-68
lines changed

3 files changed

+120
-68
lines changed

allure-pytest/src/utils.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from itertools import chain, islice
3-
from allure_commons.utils import represent, SafeFormatter, md5
3+
from allure_commons.utils import SafeFormatter, md5
44
from allure_commons.utils import format_exception, format_traceback
55
from allure_commons.model2 import Status
66
from allure_commons.model2 import StatusDetails
@@ -20,6 +20,15 @@
2020
LabelType.SUB_SUITE
2121
]
2222

23+
MARK_NAMES_TO_IGNORE = {
24+
"usefixtures",
25+
"filterwarnings",
26+
"skip",
27+
"skipif",
28+
"xfail",
29+
"parametrize",
30+
}
31+
2332

2433
def get_marker_value(item, keyword):
2534
marker = item.get_closest_marker(keyword)
@@ -81,27 +90,14 @@ def format_allure_link(config, url, link_type):
8190

8291

8392
def pytest_markers(item):
84-
for keyword in item.keywords.keys():
85-
if any([keyword.startswith('allure_'), keyword == 'parametrize']):
86-
continue
87-
marker = item.get_closest_marker(keyword)
88-
if marker is None:
89-
continue
90-
91-
yield mark_to_str(marker)
93+
for mark in item.iter_markers():
94+
if should_convert_mark_to_tag(mark):
95+
yield mark.name
9296

9397

94-
def mark_to_str(marker):
95-
args = [represent(arg) for arg in marker.args]
96-
kwargs = [f'{key}={represent(value)}' for key, value in marker.kwargs.items()]
97-
if marker.name in ('filterwarnings', 'skip', 'skipif', 'xfail', 'usefixtures', 'tryfirst', 'trylast'):
98-
markstr = f'@pytest.mark.{marker.name}'
99-
else:
100-
markstr = str(marker.name)
101-
if args or kwargs:
102-
parameters = ', '.join(args + kwargs)
103-
markstr = f'{markstr}({parameters})'
104-
return markstr
98+
def should_convert_mark_to_tag(mark):
99+
return mark.name not in MARK_NAMES_TO_IGNORE and \
100+
not mark.args and not mark.kwargs
105101

106102

107103
def allure_package(item):
Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
from hamcrest import assert_that, not_
1+
from hamcrest import assert_that, not_, anything
22
from tests.allure_pytest.pytest_runner import AllurePytestRunner
33

44
from allure_commons_test.report import has_test_case
55
from allure_commons_test.label import has_tag
66

77

8-
def test_pytest_marker(allure_pytest_runner: AllurePytestRunner):
8+
def test_pytest_simple_markers_are_converted_to_allure_tags(
9+
allure_pytest_runner: AllurePytestRunner
10+
):
911
"""
1012
>>> import pytest
1113
1214
>>> @pytest.mark.cool
1315
... @pytest.mark.stuff
14-
... def test_pytest_marker_example():
16+
... def test_pytest_simple_markers_are_converted_to_allure_tags_example():
1517
... pass
1618
"""
1719

@@ -20,25 +22,21 @@ def test_pytest_marker(allure_pytest_runner: AllurePytestRunner):
2022
assert_that(
2123
allure_results,
2224
has_test_case(
23-
"test_pytest_marker_example",
25+
"test_pytest_simple_markers_are_converted_to_allure_tags_example",
2426
has_tag("cool"),
2527
has_tag("stuff")
2628
)
2729
)
2830

2931

30-
def test_show_reserved_pytest_markers_full_decorator(
31-
allure_pytest_runner: AllurePytestRunner
32+
def test_pytest_marker_with_args_is_not_converted_to_allure_tag(
33+
allure_pytest_runner: AllurePytestRunner
3234
):
3335
"""
3436
>>> import pytest
3537
36-
>>> @pytest.mark.usermark1
37-
... @pytest.mark.usermark2
38-
... @pytest.mark.parametrize("param", ["foo"])
39-
... @pytest.mark.skipif(False, reason="reason2")
40-
... @pytest.mark.skipif(False, reason="reason1")
41-
... def test_show_reserved_pytest_markers_full_decorator_example(param):
38+
>>> @pytest.mark.marker('cool', 'stuff')
39+
... def test_pytest_marker_with_args_is_not_converted_to_allure_tag_example():
4240
... pass
4341
"""
4442

@@ -47,26 +45,46 @@ def test_show_reserved_pytest_markers_full_decorator(
4745
assert_that(
4846
allure_results,
4947
has_test_case(
50-
"test_show_reserved_pytest_markers_full_decorator_example[foo]",
51-
has_tag("usermark1"),
52-
has_tag("usermark2"),
53-
has_tag("@pytest.mark.skipif(False, reason='reason1')"),
48+
"test_pytest_marker_with_args_is_not_converted_to_allure_tag_example",
5449
not_(
55-
has_tag("@pytest.mark.skipif(False, reason='reason2')")
56-
),
50+
has_tag(anything())
51+
)
52+
)
53+
)
54+
55+
56+
def test_pytest_marker_with_kwargs_is_not_converted_to_allure_tag(
57+
allure_pytest_runner: AllurePytestRunner
58+
):
59+
"""
60+
>>> import pytest
61+
62+
>>> @pytest.mark.marker(stuff='cool')
63+
... def test_pytest_marker_with_kwargs_is_not_converted_to_allure_tag_example():
64+
... pass
65+
"""
66+
67+
allure_results = allure_pytest_runner.run_docstring()
68+
69+
assert_that(
70+
allure_results,
71+
has_test_case(
72+
"test_pytest_marker_with_kwargs_is_not_converted_to_allure_tag_example",
5773
not_(
58-
has_tag("@pytest.mark.parametrize('param', ['foo'])")
74+
has_tag(anything())
5975
)
6076
)
6177
)
6278

6379

64-
def test_pytest_xfail_marker(allure_pytest_runner: AllurePytestRunner):
80+
def test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag(
81+
allure_pytest_runner: AllurePytestRunner
82+
):
6583
"""
6684
>>> import pytest
6785
68-
>>> @pytest.mark.xfail(reason='this is unexpect pass')
69-
... def test_pytest_xfail_marker_example():
86+
>>> @pytest.mark.usefixtures('test_fixture')
87+
... def test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag_example():
7088
... pass
7189
"""
7290

@@ -75,18 +93,22 @@ def test_pytest_xfail_marker(allure_pytest_runner: AllurePytestRunner):
7593
assert_that(
7694
allure_results,
7795
has_test_case(
78-
"test_pytest_xfail_marker_example",
79-
has_tag("@pytest.mark.xfail(reason='this is unexpect pass')")
96+
"test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag_example",
97+
not_(
98+
has_tag(anything())
99+
)
80100
)
81101
)
82102

83103

84-
def test_pytest_marker_with_args(allure_pytest_runner: AllurePytestRunner):
104+
def test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag(
105+
allure_pytest_runner: AllurePytestRunner
106+
):
85107
"""
86108
>>> import pytest
87109
88-
>>> @pytest.mark.marker('cool', 'stuff')
89-
... def test_pytest_marker_with_args_example():
110+
>>> @pytest.mark.filterwarnings('ignore:val')
111+
... def test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag_example():
90112
... pass
91113
"""
92114

@@ -95,18 +117,22 @@ def test_pytest_marker_with_args(allure_pytest_runner: AllurePytestRunner):
95117
assert_that(
96118
allure_results,
97119
has_test_case(
98-
"test_pytest_marker_with_args_example",
99-
has_tag("marker('cool', 'stuff')")
120+
"test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag_example",
121+
not_(
122+
has_tag(anything())
123+
)
100124
)
101125
)
102126

103127

104-
def test_pytest_marker_with_kwargs(allure_pytest_runner: AllurePytestRunner):
128+
def test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag(
129+
allure_pytest_runner: AllurePytestRunner
130+
):
105131
"""
106132
>>> import pytest
107133
108-
>>> @pytest.mark.marker(stuff='cool')
109-
... def test_pytest_marker_with_kwargs_example():
134+
>>> @pytest.mark.skip(reason='reason')
135+
... def test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag_example():
110136
... pass
111137
"""
112138

@@ -115,20 +141,22 @@ def test_pytest_marker_with_kwargs(allure_pytest_runner: AllurePytestRunner):
115141
assert_that(
116142
allure_results,
117143
has_test_case(
118-
"test_pytest_marker_with_kwargs_example",
119-
has_tag("marker(stuff='cool')")
144+
"test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag_example",
145+
not_(
146+
has_tag(anything())
147+
)
120148
)
121149
)
122150

123151

124-
def test_pytest_marker_with_kwargs_native_encoding(
125-
allure_pytest_runner: AllurePytestRunner
152+
def test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag(
153+
allure_pytest_runner: AllurePytestRunner
126154
):
127155
"""
128156
>>> import pytest
129157
130-
>>> @pytest.mark.marker(stuff='я')
131-
... def test_pytest_marker_with_kwargs_native_encoding_example():
158+
>>> @pytest.mark.skipif(False, reason='reason')
159+
... def test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag_example():
132160
... pass
133161
"""
134162

@@ -137,20 +165,22 @@ def test_pytest_marker_with_kwargs_native_encoding(
137165
assert_that(
138166
allure_results,
139167
has_test_case(
140-
"test_pytest_marker_with_kwargs_native_encoding_example",
141-
has_tag("marker(stuff='я')")
168+
"test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag_example",
169+
not_(
170+
has_tag(anything())
171+
)
142172
)
143173
)
144174

145175

146-
def test_pytest_marker_with_kwargs_utf_encoding(
147-
allure_pytest_runner: AllurePytestRunner
176+
def test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag(
177+
allure_pytest_runner: AllurePytestRunner
148178
):
149179
"""
150180
>>> import pytest
151181
152-
>>> @pytest.mark.marker(stuff='я')
153-
... def test_pytest_marker_with_kwargs_utf_encoding_example():
182+
>>> @pytest.mark.xfail(reason='this is unexpect pass')
183+
... def test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag_example():
154184
... pass
155185
"""
156186

@@ -159,7 +189,33 @@ def test_pytest_marker_with_kwargs_utf_encoding(
159189
assert_that(
160190
allure_results,
161191
has_test_case(
162-
"test_pytest_marker_with_kwargs_utf_encoding_example",
163-
has_tag("marker(stuff='я')")
192+
"test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag_example",
193+
not_(
194+
has_tag(anything())
195+
)
196+
)
197+
)
198+
199+
200+
def test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag(
201+
allure_pytest_runner: AllurePytestRunner
202+
):
203+
"""
204+
>>> import pytest
205+
206+
>>> @pytest.mark.parametrize("param", ["foo"])
207+
... def test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag_example(param):
208+
... pass
209+
"""
210+
211+
allure_results = allure_pytest_runner.run_docstring()
212+
213+
assert_that(
214+
allure_results,
215+
has_test_case(
216+
"test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag_example[foo]",
217+
not_(
218+
has_tag(anything())
219+
)
164220
)
165221
)

tests/allure_pytest/externals/pytest_rerunfailures/pytest_rerunfailures_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ def __count_labels(tc, name):
6565

6666
assert len(output.test_cases) == 2
6767
assert __count_labels(output.test_cases[0], "suite") == 1
68-
assert __count_labels(output.test_cases[0], "tag") == 1
68+
assert __count_labels(output.test_cases[0], "tag") == 0
6969
assert __count_labels(output.test_cases[1], "suite") == 1
70-
assert __count_labels(output.test_cases[1], "tag") == 1
70+
assert __count_labels(output.test_cases[1], "tag") == 0

0 commit comments

Comments
 (0)