Skip to content

Commit

Permalink
Merge pull request allure-framework#637 from ShurikMen/master
Browse files Browse the repository at this point in the history
Fix missing dynamic called fixtures in reports
  • Loading branch information
skhomuti authored Sep 5, 2022
2 parents a7a8aa1 + b186b93 commit aa7a29a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 10 deletions.
26 changes: 16 additions & 10 deletions allure-pytest/src/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
status=get_status(exc_val),
statusDetails=get_status_details(exc_type, exc_val, exc_tb))

def _update_fixtures_children(self, item):
uuid = self._cache.get(item.nodeid)
for fixturedef in _test_fixtures(item):
group_uuid = self._cache.get(fixturedef)
if group_uuid:
group = self.allure_logger.get_item(group_uuid)
else:
group_uuid = self._cache.push(fixturedef)
group = TestResultContainer(uuid=group_uuid)
self.allure_logger.start_group(group_uuid, group)
if uuid not in group.children:
self.allure_logger.update_group(group_uuid, children=uuid)

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_protocol(self, item, nextitem):
uuid = self._cache.push(item.nodeid)
Expand All @@ -71,20 +84,11 @@ def pytest_runtest_setup(self, item):
uuid = self._cache.push(item.nodeid)
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
self.allure_logger.schedule_test(uuid, test_result)

yield

self._update_fixtures_children(item)
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
for fixturedef in _test_fixtures(item):
group_uuid = self._cache.get(fixturedef)
if not group_uuid:
group_uuid = self._cache.push(fixturedef)
group = TestResultContainer(uuid=group_uuid)
self.allure_logger.start_group(group_uuid, group)
self.allure_logger.update_group(group_uuid, children=uuid)
params = item.callspec.params if hasattr(item, 'callspec') else {}

test_result.name = allure_name(item, params)
full_name = allure_full_name(item)
test_result.fullName = full_name
Expand All @@ -104,12 +108,14 @@ def pytest_runtest_call(self, item):
self.allure_logger.schedule_test(uuid, test_result)
test_result.start = now()
yield
self._update_fixtures_children(item)
if test_result:
test_result.stop = now()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(self, item):
yield
self._update_fixtures_children(item)
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
Expand Down
100 changes: 100 additions & 0 deletions allure-pytest/test/acceptance/fixture/fixture_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,103 @@ def test_with_redefined_fixture(my_fixture):
),
)
)


@pytest.mark.parametrize(
["parent_scope", "child_scope"],
list(combinations_with_replacement(fixture_scopes, 2))
)
def test_dynamically_called_fixture(allured_testdir, parent_scope, child_scope):
allured_testdir.testdir.makepyfile("""
import pytest
@pytest.fixture(scope="{parent_scope}", autouse=True)
def parent_auto_call_fixture():
yield
@pytest.fixture(scope="{child_scope}")
def child_manual_call_fixture():
yield
@pytest.fixture(scope="{parent_scope}")
def parent_dyn_call_fixture():
yield
@pytest.fixture(scope="{child_scope}")
def child_dyn_call_fixture(request):
request.getfixturevalue('parent_dyn_call_fixture')
def test_one(child_manual_call_fixture):
pass
def test_two(request):
request.getfixturevalue('child_dyn_call_fixture')
def test_three(request):
request.getfixturevalue('parent_dyn_call_fixture')
""".format(parent_scope=parent_scope, child_scope=child_scope))

allured_testdir.run_with_allure()

assert_that(allured_testdir.allure_report,
has_test_case("test_one",
has_container(allured_testdir.allure_report,
has_before("parent_auto_call_fixture"),
has_after("parent_auto_call_fixture::0"),
),
has_container(allured_testdir.allure_report,
has_before("child_manual_call_fixture"),
has_after("child_manual_call_fixture::0"),
),
not_(has_container(allured_testdir.allure_report,
has_before("parent_dyn_call_fixture"),
has_after("parent_dyn_call_fixture::0"),
),
),
not_(has_container(allured_testdir.allure_report,
has_before("child_dyn_call_fixture"),
),
)
)
)
assert_that(allured_testdir.allure_report,
has_test_case("test_two",
has_container(allured_testdir.allure_report,
has_before("parent_auto_call_fixture"),
has_after("parent_auto_call_fixture::0"),
),
not_(has_container(allured_testdir.allure_report,
has_before("child_manual_call_fixture"),
has_after("child_manual_call_fixture::0"),
),
),
has_container(allured_testdir.allure_report,
has_before("parent_dyn_call_fixture"),
has_after("parent_dyn_call_fixture::0"),
),
has_container(allured_testdir.allure_report,
has_before("child_dyn_call_fixture"),
),
),
)
assert_that(allured_testdir.allure_report,
has_test_case("test_three",
has_container(allured_testdir.allure_report,
has_before("parent_auto_call_fixture"),
has_after("parent_auto_call_fixture::0"),
),
not_(has_container(allured_testdir.allure_report,
has_before("child_manual_call_fixture"),
has_after("child_manual_call_fixture::0"),
),
),
has_container(allured_testdir.allure_report,
has_before("parent_dyn_call_fixture"),
has_after("parent_dyn_call_fixture::0"),
),
not_(has_container(allured_testdir.allure_report,
has_before("child_dyn_call_fixture"),
),
)
)
)

0 comments on commit aa7a29a

Please sign in to comment.