Skip to content

Commit deac9ae

Browse files
authored
exclude tests from report by cmd (fixes #107 via #111)
1 parent 01b8ab3 commit deac9ae

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

allure-pytest/src/plugin.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import pytest
21
import argparse
3-
from six import text_type
42

53
import allure
64
import allure_commons
@@ -21,13 +19,15 @@ def pytest_addoption(parser):
2119
default=None,
2220
help="Generate Allure report in the specified directory (may not exist)")
2321

24-
def label_type(name, legal_values=set()):
22+
def label_type(type_name, legal_values=set()):
2523
def a_label_type(string):
2624
atoms = set(string.split(','))
27-
if legal_values and not atoms < legal_values:
28-
raise argparse.ArgumentTypeError('Illegal {} values: {}, only [{}] are allowed'.format(
29-
name, ', '.join(atoms - legal_values), ', '.join(legal_values)))
30-
return set((name.value, atom) for atom in atoms)
25+
if type_name is LabelType.SEVERITY:
26+
if not atoms < legal_values:
27+
raise argparse.ArgumentTypeError('Illegal {} values: {}, only [{}] are allowed'.format(
28+
type_name, ', '.join(atoms - legal_values), ', '.join(legal_values)))
29+
return set((type_name, allure.severity_level(atom)) for atom in atoms)
30+
return set((type_name, atom) for atom in atoms)
3131
return a_label_type
3232

3333
severities = [x.value for x in list(allure.severity_level)]
@@ -36,7 +36,7 @@ def a_label_type(string):
3636
dest="allure_severities",
3737
metavar="SEVERITIES_SET",
3838
default={},
39-
type=label_type(name=LabelType.SEVERITY, legal_values=set(severities)),
39+
type=label_type(LabelType.SEVERITY, legal_values=set(severities)),
4040
help="""Comma-separated list of severity names.
4141
Tests only with these severities will be run.
4242
Possible values are: %s.""" % ', '.join(severities))
@@ -46,7 +46,7 @@ def a_label_type(string):
4646
dest="allure_epics",
4747
metavar="EPICS_SET",
4848
default={},
49-
type=label_type(name=LabelType.EPIC),
49+
type=label_type(LabelType.EPIC),
5050
help="""Comma-separated list of epic names.
5151
Run tests that have at least one of the specified feature labels.""")
5252

@@ -55,7 +55,7 @@ def a_label_type(string):
5555
dest="allure_features",
5656
metavar="FEATURES_SET",
5757
default={},
58-
type=label_type(name=LabelType.FEATURE),
58+
type=label_type(LabelType.FEATURE),
5959
help="""Comma-separated list of feature names.
6060
Run tests that have at least one of the specified feature labels.""")
6161

@@ -64,7 +64,7 @@ def a_label_type(string):
6464
dest="allure_stories",
6565
metavar="STORIES_SET",
6666
default={},
67-
type=label_type(name=LabelType.STORY),
67+
type=label_type(LabelType.STORY),
6868
help="""Comma-separated list of story names.
6969
Run tests that have at least one of the specified story labels.""")
7070

@@ -102,15 +102,13 @@ def pytest_configure(config):
102102
allure_commons.register(file_logger)
103103

104104

105-
def pytest_runtest_setup(item):
106-
item_labels = set((name, value) for name, value in allure_labels(item))
105+
def pytest_collection_modifyitems(items, config):
106+
arg_labels = set().union(config.option.allure_epics,
107+
config.option.allure_features,
108+
config.option.allure_stories,
109+
config.option.allure_severities)
107110

108-
arg_labels = set().union(item.config.option.allure_features,
109-
item.config.option.allure_stories,
110-
item.config.option.allure_severities)
111-
112-
if arg_labels and not item_labels & arg_labels:
113-
pytest.skip('Not suitable with selected labels: %s.' % ', '.join(text_type(l) for l in sorted(arg_labels)))
111+
items[:] = filter(lambda item: arg_labels & set(allure_labels(item)) if arg_labels else True, items)
114112

115113

116114
def pytest_namespace():

allure-pytest/test/filtering/bdd_labels_test.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

1515
def test_without_epic_features_and_stories():
1616
"""
17+
>>> from hamcrest import not_
1718
>>> allure_report = getfixture('allure_report_with_params')('--allure-epic=right_epic',
1819
... '--allure-features=right_feature',
1920
... '--allure-stories=right_story')
2021
>>> assert_that(allure_report,
21-
... has_test_case('test_without_epic_features_and_stories',
22-
... with_status('skipped')
23-
... )
22+
... not_(has_test_case('test_without_epic_features_and_stories'))
2423
... )
2524
"""
2625
pass
@@ -41,9 +40,9 @@ def test_right_feature_without_story_and_epic():
4140
pass
4241

4342

44-
@allure.feature('wrong_epic')
43+
@allure.feature('right_epic')
4544
@allure.feature('wrong_feature')
46-
@allure.story('right_story')
45+
@allure.story('wrong_story')
4746
def test_right_story_but_wrong_epic_and_feature():
4847
"""
4948
>>> allure_report = getfixture('allure_report_with_params')('--allure-epic=right_epic',
@@ -75,18 +74,34 @@ def test_right_feature_but_wrong_epic_and_story():
7574
pass
7675

7776

77+
@allure.feature('wrong_epic')
78+
@allure.feature('wrong_feature')
79+
@allure.story('right_story')
80+
def test_right_story_but_wrong_epic_and_feature():
81+
"""
82+
>>> allure_report = getfixture('allure_report_with_params')('--allure-epic=right_epic',
83+
... '--allure-features=right_feature',
84+
... '--allure-stories=right_story')
85+
>>> assert_that(allure_report,
86+
... has_test_case('test_right_story_but_wrong_epic_and_feature',
87+
... with_status('passed')
88+
... )
89+
... )
90+
"""
91+
pass
92+
93+
7894
@allure.feature('wrong_epic')
7995
@allure.feature('wrong_feature')
8096
@allure.story('wrong_story')
8197
def test_wrong_epic_feature_and_story():
8298
"""
99+
>>> from hamcrest import not_
83100
>>> allure_report = getfixture('allure_report_with_params')('--allure-epic=right_epic',
84101
... '--allure-features=right_feature',
85102
... '--allure-stories=right_story')
86103
>>> assert_that(allure_report,
87-
... has_test_case('test_wrong_epic_feature_and_story',
88-
... with_status('skipped')
89-
... )
104+
... not_(has_test_case('test_wrong_epic_feature_and_story'))
90105
... )
91106
"""
92107
pass

allure-pytest/test/filtering/severity_label_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ def test_method_with_trivial_severity(self):
4040
@pytest.allure.severity(pytest.allure.severity_level.NORMAL)
4141
def test_method_with_normal_severity(self):
4242
"""
43+
>>> from hamcrest import not_
4344
>>> allure_report = getfixture('allure_report_with_params')('--allure-severities=trivial')
4445
>>> assert_that(allure_report,
45-
... has_test_case('test_method_with_normal_severity',
46-
... with_status('skipped')
47-
... )
46+
... not_(has_test_case('test_method_with_normal_severity'))
4847
... )
4948
"""
5049
pass
@@ -67,11 +66,10 @@ def test_method_with_whole_class_trivial_severity(self):
6766
@pytest.allure.severity(pytest.allure.severity_level.NORMAL)
6867
def test_method_with_overridden_class_severity(self):
6968
"""
69+
>>> from hamcrest import not_
7070
>>> allure_report = getfixture('allure_report_with_params')('--allure-severities=trivial')
7171
>>> assert_that(allure_report,
72-
... has_test_case('test_method_with_overridden_class_severity',
73-
... with_status('skipped')
74-
... )
72+
... not_(has_test_case('test_method_with_overridden_class_severity'))
7573
... )
7674
"""
7775
pass

0 commit comments

Comments
 (0)