Skip to content

Commit f48d440

Browse files
KillAChickensseliverstov
authored andcommitted
close pytest test from pytest_runtest_logfinish hook (via #361)
1 parent 248a910 commit f48d440

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

allure-pytest/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
]
2727

2828
install_requires = [
29-
"pytest>=3.3.0",
29+
"pytest>=3.4.0",
3030
"six>=1.9.0",
3131
"allure-python-commons==2.6.1"
3232
]

allure-pytest/src/listener.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ def pytest_runtest_makereport(self, item, call):
199199
self.attach_data(report.capstdout, "stdout", AttachmentType.TEXT, None)
200200
self.attach_data(report.capstderr, "stderr", AttachmentType.TEXT, None)
201201

202-
uuid = self._cache.pop(item.nodeid)
203-
self.allure_logger.close_test(uuid)
202+
@pytest.hookimpl(hookwrapper=True)
203+
def pytest_runtest_logfinish(self, nodeid, location):
204+
yield
205+
uuid = self._cache.pop(nodeid)
206+
self.allure_logger.close_test(uuid)
204207

205208
@allure_commons.hookimpl
206209
def attach_data(self, body, name, attachment_type, extension):
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from hamcrest import assert_that
2+
from allure_commons_test.report import has_test_case
3+
from allure_commons_test.result import has_attachment
4+
5+
6+
def test_attach_from_runtest_teardown(allured_testdir):
7+
allured_testdir.testdir.makeconftest("""
8+
import allure
9+
10+
11+
def pytest_runtest_teardown(*args, **kwargs):
12+
allure.attach(body="body", name="attachment from teardown")
13+
""")
14+
15+
allured_testdir.testdir.makepyfile("""
16+
def test_attach_from_runtest_teardown():
17+
pass
18+
""")
19+
20+
allured_testdir.run_with_allure()
21+
22+
assert_that(allured_testdir.allure_report,
23+
has_test_case("test_attach_from_runtest_teardown",
24+
has_attachment(name="attachment from teardown"),
25+
)
26+
)
27+
28+
29+
def test_attach_from_runtest_logfinish(allured_testdir):
30+
allured_testdir.testdir.makeconftest("""
31+
import allure
32+
33+
34+
def pytest_runtest_logfinish(*args, **kwargs):
35+
allure.attach(body="body", name="attachment from logfinish")
36+
""")
37+
38+
allured_testdir.testdir.makepyfile("""
39+
def test_attach_from_runtest_logfinish():
40+
pass
41+
""")
42+
43+
allured_testdir.run_with_allure()
44+
45+
assert_that(allured_testdir.allure_report,
46+
has_test_case("test_attach_from_runtest_logfinish",
47+
has_attachment(name="attachment from logfinish"),
48+
)
49+
)

0 commit comments

Comments
 (0)