Skip to content

Commit 30b36ae

Browse files
authored
fix duration for broken fixtures and improve pytest-rerunfailures - now reruns in report (fixes #244 via #357)
1 parent 2548969 commit 30b36ae

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed

allure-pytest/src/listener.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,22 @@ def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
5858
status=get_status(exc_val),
5959
statusDetails=get_status_details(exc_type, exc_val, exc_tb))
6060

61-
@pytest.hookimpl(hookwrapper=True)
62-
def pytest_runtest_setup(self, item):
61+
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
62+
def pytest_runtest_protocol(self, item, nextitem):
6363
uuid = self._cache.push(item.nodeid)
64-
test_result = TestResult(name=item.name, uuid=uuid)
64+
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
6565
self.allure_logger.schedule_test(uuid, test_result)
66+
yield
67+
68+
@pytest.hookimpl(hookwrapper=True)
69+
def pytest_runtest_setup(self, item):
70+
if not self._cache.get(item.nodeid):
71+
uuid = self._cache.push(item.nodeid)
72+
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
73+
self.allure_logger.schedule_test(uuid, test_result)
6674

6775
yield
76+
6877
uuid = self._cache.get(item.nodeid)
6978
test_result = self.allure_logger.get_test(uuid)
7079
for fixturedef in _test_fixtures(item):
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from hamcrest import assert_that
3+
from allure_commons_test.report import has_test_case
4+
from allure_commons_test.result import has_attachment
5+
6+
7+
@pytest.mark.parametrize("param", ["first", "second"])
8+
def test_parametrized_attachment(executed_docstring_source, param):
9+
"""
10+
>>> import pytest
11+
12+
>>> @pytest.mark.parametrize("param", ["first", "second"])
13+
... def test_parametrized_attachment_example(param):
14+
... assert param
15+
"""
16+
17+
assert_that(executed_docstring_source.allure_report,
18+
has_test_case("test_parametrized_attachment_example[{param}]".format(param=param),
19+
has_attachment()
20+
)
21+
)

allure-pytest/test/acceptance/duration/__init__.py

Whitespace-only changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import allure
2+
import pytest
3+
from hamcrest import assert_that, has_entry, greater_than, all_of
4+
from allure_commons_test.report import has_test_case
5+
from allure_commons.utils import now
6+
7+
8+
snippets = [
9+
"pass",
10+
"assert False",
11+
"raise(RuntimeError())",
12+
"pytest.skip()",
13+
"pytest.fail()",
14+
"pytest.xfail()",
15+
pytest.param("pytest.exit('msg')", marks=pytest.mark.skip)
16+
]
17+
18+
19+
@pytest.mark.parametrize("snipped", snippets)
20+
def test_duration(allured_testdir, snipped):
21+
allured_testdir.testdir.makepyfile("""
22+
def test_duration_example():
23+
{snipped}
24+
""".format(snipped=snipped))
25+
26+
timestamp = now()
27+
allured_testdir.run_with_allure()
28+
29+
assert_that(allured_testdir.allure_report,
30+
has_test_case("test_duration_example",
31+
all_of(
32+
has_entry("start", greater_than(timestamp)),
33+
has_entry("stop", greater_than(timestamp))
34+
))
35+
)
36+
37+
38+
@allure.issue("244")
39+
@pytest.mark.parametrize("snipped", snippets)
40+
def test_with_fixture_duration(allured_testdir, snipped):
41+
allured_testdir.testdir.makepyfile("""
42+
import pytest
43+
44+
@pytest.fixture
45+
def fixture():
46+
{snipped}
47+
48+
def test_with_fixture_duration_example(fixture):
49+
pass
50+
""".format(snipped=snipped))
51+
52+
timestamp = now()
53+
allured_testdir.run_with_allure()
54+
55+
assert_that(allured_testdir.allure_report,
56+
has_test_case("test_with_fixture_duration_example",
57+
all_of(
58+
has_entry("start", greater_than(timestamp)),
59+
has_entry("stop", greater_than(timestamp))
60+
))
61+
)
62+
63+
64+
@allure.issue("244")
65+
@pytest.mark.parametrize("snipped", snippets)
66+
def test_with_fixture_finalizer_duration(allured_testdir, snipped):
67+
allured_testdir.testdir.makepyfile("""
68+
import pytest
69+
70+
@pytest.fixture
71+
def fixture(request):
72+
def finalizer():
73+
{snipped}
74+
request.addfinalizef(finalizer)
75+
76+
def test_with_fixture_finalizer_duration(fixture):
77+
pass
78+
""".format(snipped=snipped))
79+
80+
timestamp = now()
81+
allured_testdir.run_with_allure()
82+
83+
assert_that(allured_testdir.allure_report,
84+
has_test_case("test_with_fixture_finalizer_duration",
85+
all_of(
86+
has_entry("start", greater_than(timestamp)),
87+
has_entry("stop", greater_than(timestamp))
88+
))
89+
)

allure-pytest/test/integration/pytest_rerunfailures/pytest_rerunfailures_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import allure
12
import pytest
23
from hamcrest import assert_that
34
from allure_commons_test.report import has_test_case
45
from allure_commons_test.result import with_status
56

67

8+
@allure.issue("140")
9+
@allure.feature("Integration")
710
@pytest.mark.parametrize("countdown", [2, 4])
811
def test_pytest_rerunfailures(allured_testdir, countdown):
912
allured_testdir.testdir.makepyfile("""

0 commit comments

Comments
 (0)