Skip to content

Commit 26141d0

Browse files
Sup3rGeosseliverstov
authored andcommitted
feature clean alluredir (fixes #175 via #234)
1 parent 82a3d51 commit 26141d0

File tree

7 files changed

+78
-8
lines changed

7 files changed

+78
-8
lines changed

allure-pytest/src/plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def pytest_addoption(parser):
1919
default=None,
2020
help="Generate Allure report in the specified directory (may not exist)")
2121

22+
parser.getgroup("reporting").addoption('--clean-alluredir',
23+
action="store_true",
24+
dest="clean_alluredir",
25+
help="Clean alluredir folder if it exists")
26+
2227
def label_type(type_name, legal_values=set()):
2328
def a_label_type(string):
2429
atoms = set(string.split(','))
@@ -97,6 +102,7 @@ def clean_up():
97102

98103
def pytest_configure(config):
99104
report_dir = config.option.allure_report_dir
105+
clean = config.option.clean_alluredir
100106

101107
test_helper = AllureTestHelper(config)
102108
# TODO: Why helper is present anyway?
@@ -109,7 +115,7 @@ def pytest_configure(config):
109115
allure_commons.plugin_manager.register(test_listener)
110116
config.add_cleanup(cleanup_factory(test_listener))
111117

112-
file_logger = AllureFileLogger(report_dir)
118+
file_logger = AllureFileLogger(report_dir, clean)
113119
allure_commons.plugin_manager.register(file_logger)
114120
config.add_cleanup(cleanup_factory(file_logger))
115121

allure-pytest/test/clean/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def test_two_runs_clean():
2+
"""
3+
>>> report_fixture = getfixture('allure_report_with_params')
4+
>>> allure_report_first_run = report_fixture('--clean-alluredir', cache=False)
5+
>>> allure_report_second_run = report_fixture('--clean-alluredir', cache=False)
6+
>>> assert_that(allure_report_second_run,
7+
... has_only_n_test_cases('test_two_runs_clean', 1)
8+
... )
9+
"""
10+
assert True
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def test_two_runs_no_clean():
2+
"""
3+
>>> report_fixture = getfixture('allure_report_with_params')
4+
>>> allure_report_first_run = report_fixture(cache=False)
5+
>>> allure_report_second_run = report_fixture(cache=False)
6+
>>> assert_that(allure_report_second_run,
7+
... has_only_n_test_cases('test_two_runs_no_clean', 2)
8+
... )
9+
"""
10+
assert True

allure-pytest/test/conftest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ def allure_report_with_params(request, tmpdir_factory):
5252
module = request.module.__file__
5353
tmpdir = tmpdir_factory.mktemp('data')
5454

55-
def run_with_params(*params):
55+
def run_with_params(*params, **kwargs):
56+
cache = kwargs.get("cache", True)
5657
key = _get_hash('{thread}{module}{param}'.format(thread=thread_tag(), module=module, param=''.join(params)))
5758
if not request.config.cache.get(key, False):
5859
_runner(tmpdir.strpath, module, *params)
59-
request.config.cache.set(key, True)
60+
if cache:
61+
request.config.cache.set(key, True)
6062

61-
def clear_cache():
62-
request.config.cache.set(key, False)
63-
request.addfinalizer(clear_cache)
63+
def clear_cache():
64+
request.config.cache.set(key, False)
65+
request.addfinalizer(clear_cache)
6466

6567
return AllureReport(tmpdir.strpath)
6668
return run_with_params

allure-python-commons-test/src/report.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
from hamcrest import has_item
7676
from hamcrest import has_entry
7777
from hamcrest import ends_with
78-
78+
from hamcrest.core.base_matcher import BaseMatcher
7979

8080
if sys.version_info[0] < 3:
8181
from io import open
@@ -107,3 +107,40 @@ def has_test_case(name, *matchers):
107107
)
108108
)
109109
)
110+
111+
112+
class ContainsExactly(BaseMatcher):
113+
114+
def __init__(self, num, matcher):
115+
self.matcher = matcher
116+
self.count = 0
117+
self.num = num
118+
119+
def _matches(self, item):
120+
self.count = 0
121+
for subitem in item:
122+
if self.matcher.matches(subitem):
123+
self.count += 1
124+
125+
if self.count == self.num:
126+
return True
127+
else:
128+
return False
129+
130+
def describe_to(self, description):
131+
description.append_text('exactly {} item(s) matching '.format(self.num)).append_text(self.matcher)
132+
133+
134+
def has_only_n_test_cases(name, num, *matchers):
135+
return has_property('test_cases',
136+
ContainsExactly(num,
137+
all_of(
138+
any_of(
139+
has_entry('fullName', ends_with(name)),
140+
has_entry('name', ends_with(name))
141+
),
142+
*matchers
143+
)
144+
)
145+
)
146+

allure-python-commons/src/logger.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313

1414
class AllureFileLogger(object):
1515

16-
def __init__(self, report_dir):
16+
def __init__(self, report_dir, clean=False):
1717
self._report_dir = report_dir
1818

1919
if not os.path.exists(report_dir):
2020
os.makedirs(report_dir)
21+
elif clean:
22+
for f in os.listdir(report_dir):
23+
f = os.path.join(report_dir, f)
24+
if os.path.isfile(f):
25+
os.unlink(f)
2126

2227
def _report_item(self, item):
2328
indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None

0 commit comments

Comments
 (0)