Skip to content

Commit 36d6db7

Browse files
committed
fix: don't convert built-in (reserved) pytest markers to allure tags (allure-framework#817)
1 parent 3cd731a commit 36d6db7

File tree

3 files changed

+170
-58
lines changed

3 files changed

+170
-58
lines changed

allure-pytest/src/utils.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

tests/allure_pytest/acceptance/label/tag/tag_test.py

Lines changed: 153 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
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,15 +22,63 @@ 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
34+
):
35+
"""
36+
>>> import pytest
37+
38+
>>> @pytest.mark.marker('cool', 'stuff')
39+
... def test_pytest_marker_with_args_is_not_converted_to_allure_tag_example():
40+
... pass
41+
"""
42+
43+
allure_results = allure_pytest_runner.run_docstring()
44+
45+
assert_that(
46+
allure_results,
47+
has_test_case(
48+
"test_pytest_marker_with_args_is_not_converted_to_allure_tag_example",
49+
not_(
50+
has_tag("marker('cool', 'stuff')")
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",
73+
not_(
74+
has_tag("marker(stuff='cool')")
75+
)
76+
)
77+
)
78+
79+
80+
def test_pytest_multiple_simple_and_reserved_markers_to_allure_tags(
81+
allure_pytest_runner: AllurePytestRunner
3282
):
3383
"""
3484
>>> import pytest
@@ -38,7 +88,7 @@ def test_show_reserved_pytest_markers_full_decorator(
3888
... @pytest.mark.parametrize("param", ["foo"])
3989
... @pytest.mark.skipif(False, reason="reason2")
4090
... @pytest.mark.skipif(False, reason="reason1")
41-
... def test_show_reserved_pytest_markers_full_decorator_example(param):
91+
... def test_pytest_multiple_simple_and_reserved_markers_to_allure_tags_example(param):
4292
... pass
4393
"""
4494

@@ -47,26 +97,54 @@ def test_show_reserved_pytest_markers_full_decorator(
4797
assert_that(
4898
allure_results,
4999
has_test_case(
50-
"test_show_reserved_pytest_markers_full_decorator_example[foo]",
100+
"test_pytest_multiple_simple_and_reserved_markers_to_allure_tags_example[foo]",
51101
has_tag("usermark1"),
52102
has_tag("usermark2"),
53-
has_tag("@pytest.mark.skipif(False, reason='reason1')"),
54103
not_(
55-
has_tag("@pytest.mark.skipif(False, reason='reason2')")
104+
has_tag("skipif(False, reason='reason1')")
105+
),
106+
not_(
107+
has_tag("skipif(False, reason='reason2')")
56108
),
57109
not_(
58-
has_tag("@pytest.mark.parametrize('param', ['foo'])")
110+
has_tag("parametrize('param', ['foo'])")
59111
)
60112
)
61113
)
62114

63115

64-
def test_pytest_xfail_marker(allure_pytest_runner: AllurePytestRunner):
116+
def test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag(
117+
allure_pytest_runner: AllurePytestRunner
118+
):
65119
"""
66120
>>> import pytest
67121
68-
>>> @pytest.mark.xfail(reason='this is unexpect pass')
69-
... def test_pytest_xfail_marker_example():
122+
>>> @pytest.mark.usefixtures('test_fixture')
123+
... def test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag_example():
124+
... pass
125+
"""
126+
127+
allure_results = allure_pytest_runner.run_docstring()
128+
129+
assert_that(
130+
allure_results,
131+
has_test_case(
132+
"test_pytest_reserved_marker_usefixtures_is_not_converted_to_allure_tag_example",
133+
not_(
134+
has_tag("usefixtures('test_fixture')")
135+
)
136+
)
137+
)
138+
139+
140+
def test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag(
141+
allure_pytest_runner: AllurePytestRunner
142+
):
143+
"""
144+
>>> import pytest
145+
146+
>>> @pytest.mark.filterwarnings('ignore:val')
147+
... def test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag_example():
70148
... pass
71149
"""
72150

@@ -75,18 +153,22 @@ def test_pytest_xfail_marker(allure_pytest_runner: AllurePytestRunner):
75153
assert_that(
76154
allure_results,
77155
has_test_case(
78-
"test_pytest_xfail_marker_example",
79-
has_tag("@pytest.mark.xfail(reason='this is unexpect pass')")
156+
"test_pytest_reserved_marker_filterwarnings_is_not_converted_to_allure_tag_example",
157+
not_(
158+
has_tag("filterwarnings('ignore:val')")
159+
)
80160
)
81161
)
82162

83163

84-
def test_pytest_marker_with_args(allure_pytest_runner: AllurePytestRunner):
164+
def test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag(
165+
allure_pytest_runner: AllurePytestRunner
166+
):
85167
"""
86168
>>> import pytest
87169
88-
>>> @pytest.mark.marker('cool', 'stuff')
89-
... def test_pytest_marker_with_args_example():
170+
>>> @pytest.mark.skip(reason='reason')
171+
... def test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag_example():
90172
... pass
91173
"""
92174

@@ -95,18 +177,22 @@ def test_pytest_marker_with_args(allure_pytest_runner: AllurePytestRunner):
95177
assert_that(
96178
allure_results,
97179
has_test_case(
98-
"test_pytest_marker_with_args_example",
99-
has_tag("marker('cool', 'stuff')")
180+
"test_pytest_reserved_marker_skip_is_not_converted_to_allure_tag_example",
181+
not_(
182+
has_tag("skip(reason='reason')")
183+
)
100184
)
101185
)
102186

103187

104-
def test_pytest_marker_with_kwargs(allure_pytest_runner: AllurePytestRunner):
188+
def test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag(
189+
allure_pytest_runner: AllurePytestRunner
190+
):
105191
"""
106192
>>> import pytest
107193
108-
>>> @pytest.mark.marker(stuff='cool')
109-
... def test_pytest_marker_with_kwargs_example():
194+
>>> @pytest.mark.skipif(False, reason='reason')
195+
... def test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag_example():
110196
... pass
111197
"""
112198

@@ -115,20 +201,22 @@ def test_pytest_marker_with_kwargs(allure_pytest_runner: AllurePytestRunner):
115201
assert_that(
116202
allure_results,
117203
has_test_case(
118-
"test_pytest_marker_with_kwargs_example",
119-
has_tag("marker(stuff='cool')")
204+
"test_pytest_reserved_marker_skipif_is_not_converted_to_allure_tag_example",
205+
not_(
206+
has_tag("skipif(False, reason='reason')")
207+
)
120208
)
121209
)
122210

123211

124-
def test_pytest_marker_with_kwargs_native_encoding(
125-
allure_pytest_runner: AllurePytestRunner
212+
def test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag(
213+
allure_pytest_runner: AllurePytestRunner
126214
):
127215
"""
128216
>>> import pytest
129217
130-
>>> @pytest.mark.marker(stuff='я')
131-
... def test_pytest_marker_with_kwargs_native_encoding_example():
218+
>>> @pytest.mark.xfail(reason='this is unexpect pass')
219+
... def test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag_example():
132220
... pass
133221
"""
134222

@@ -137,20 +225,47 @@ def test_pytest_marker_with_kwargs_native_encoding(
137225
assert_that(
138226
allure_results,
139227
has_test_case(
140-
"test_pytest_marker_with_kwargs_native_encoding_example",
141-
has_tag("marker(stuff='я')")
228+
"test_pytest_reserved_marker_xfail_is_not_converted_to_allure_tag_example",
229+
not_(
230+
has_tag("xfail(reason='this is unexpect pass')")
231+
)
232+
)
233+
)
234+
235+
236+
def test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag(
237+
allure_pytest_runner: AllurePytestRunner
238+
):
239+
"""
240+
>>> import pytest
241+
242+
>>> @pytest.mark.parametrize("param", ["foo"])
243+
... def test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag_example(param):
244+
... pass
245+
"""
246+
247+
allure_results = allure_pytest_runner.run_docstring()
248+
249+
assert_that(
250+
allure_results,
251+
has_test_case(
252+
"test_pytest_reserved_marker_parametrize_is_not_converted_to_allure_tag_example[foo]",
253+
not_(
254+
has_tag("parametrize('param', ['foo'])")
255+
)
142256
)
143257
)
144258

145259

146-
def test_pytest_marker_with_kwargs_utf_encoding(
147-
allure_pytest_runner: AllurePytestRunner
260+
def test_pytest_simple_markers_utf_encoding_are_converted_to_allure_tags(
261+
allure_pytest_runner: AllurePytestRunner
148262
):
149263
"""
150264
>>> import pytest
151265
152-
>>> @pytest.mark.marker(stuff='я')
153-
... def test_pytest_marker_with_kwargs_utf_encoding_example():
266+
>>> @pytest.mark.классная
267+
>>> @pytest.mark.штука
268+
... def test_pytest_simple_markers_utf_encoding_are_converted_to_allure_tags_example():
154269
... pass
155270
"""
156271

@@ -159,7 +274,8 @@ def test_pytest_marker_with_kwargs_utf_encoding(
159274
assert_that(
160275
allure_results,
161276
has_test_case(
162-
"test_pytest_marker_with_kwargs_utf_encoding_example",
163-
has_tag("marker(stuff='я')")
277+
"test_pytest_simple_markers_utf_encoding_are_converted_to_allure_tags_example",
278+
has_tag("классная"),
279+
has_tag("штука")
164280
)
165281
)

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)