Skip to content

Commit efbebf7

Browse files
betapl3bskhomuti
andauthored
Add --allure-label command line option to filter test cases by custom labels (implements #725) (#690)
Co-authored-by: Sergey Khomutinin <[email protected]>
1 parent c1a4b00 commit efbebf7

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Select test by label
2+
-----------------------------
3+
4+
By using ``--allure-labels`` commandline option with a ``lablel_name=label1,label2`` format, only tests with
5+
corresponding labels will be run.
6+
7+
8+
For example, if you want to run tests with label 'Application' equals to 'desktop' or 'mobile' only,
9+
run pytest with ``--allure-labels Application=desktop,mobile`` option.
10+
11+
To filter tests with several different labels multiple ``--allure-labels`` options can be specified:
12+
13+
``--allure-labels Application=desktop,mobile --allure-labels layer=api``
14+
15+
>>> import allure
16+
17+
>>> @allure.label("Application", "desktop")
18+
... def test_custom_label_one():
19+
... pass
20+
21+
>>> @allure.label("Application", "mobile")
22+
... def test_custom_label_another():
23+
... pass
24+
25+
>>> @allure.label("Application", "mobile", "desktop")
26+
... def test_custom_label_both():
27+
... pass
28+
29+
>>> @allure.label("layer", "api")
30+
... def test_layer_label():
31+
... pass
32+
33+
>>> def test_no_labels():
34+
... pass

allure-pytest/src/plugin.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ def a_label_type(string):
102102
help="""Comma-separated list of IDs.
103103
Run tests that have at least one of the specified id labels.""")
104104

105+
def cf_type(string):
106+
type_name, values = string.split("=", 1)
107+
atoms = set(values.split(","))
108+
return [(type_name, atom) for atom in atoms]
109+
110+
parser.getgroup("general").addoption('--allure-label',
111+
action="append",
112+
dest="allure_labels",
113+
metavar="LABELS_SET",
114+
default=[],
115+
type=cf_type,
116+
help="""List of labels to run in format label_name=value1,value2.
117+
"Run tests that have at least one of the specified labels.""")
118+
105119
def link_pattern(string):
106120
pattern = string.split(':', 1)
107121
if not pattern[0]:
@@ -165,7 +179,8 @@ def select_by_labels(items, config):
165179
config.option.allure_features,
166180
config.option.allure_stories,
167181
config.option.allure_ids,
168-
config.option.allure_severities)
182+
config.option.allure_severities,
183+
*config.option.allure_labels)
169184
if arg_labels:
170185
selected, deselected = [], []
171186
for item in items:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" ./examples/label/custom/select_tests_by_label.rst """
2+
3+
import pytest
4+
from hamcrest import assert_that, ends_with, contains_inanyorder
5+
6+
7+
@pytest.mark.parametrize(
8+
["labels", "expected_tests"],
9+
[
10+
(
11+
{"Application": ["desktop"]},
12+
[
13+
"test_custom_label_one",
14+
"test_custom_label_both"
15+
]
16+
),
17+
(
18+
{"Application": ["mobile"]},
19+
[
20+
"test_custom_label_another",
21+
"test_custom_label_both"
22+
]
23+
),
24+
(
25+
{"Application": ["desktop", "mobile"]},
26+
[
27+
"test_custom_label_one",
28+
"test_custom_label_another",
29+
"test_custom_label_both"
30+
]
31+
),
32+
(
33+
{"Application": ["mobile"], "layer": ["api"]},
34+
[
35+
"test_custom_label_another",
36+
"test_custom_label_both",
37+
"test_layer_label"
38+
]
39+
)
40+
]
41+
)
42+
def test_select_by_custom_label(allured_testdir, labels, expected_tests):
43+
allured_testdir.parse_docstring_path()
44+
allure_labels = []
45+
for label_name, label_values in labels.items():
46+
allure_labels.extend(["--allure-label", f"{label_name}={','.join(label_values)}"])
47+
allured_testdir.run_with_allure(*allure_labels)
48+
test_cases = [test_case["fullName"] for test_case in allured_testdir.allure_report.test_cases]
49+
assert_that(test_cases, contains_inanyorder(*[ends_with(name) for name in expected_tests]))

0 commit comments

Comments
 (0)