Replies: 2 comments 1 reply
|
Unfortunately its not safety possible at the moment A approximation woul be a pair of fixtures where the function scoped facade reports each test to the module scoped one |
|
The pair-fixture approximation mentioned above is the safe pattern. import pytest
service_key = pytest.StashKey[object]()
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
report = yield
service = item.stash.get(service_key, None)
if service is not None and report.failed:
service.saw_problem = True
return report
@pytest.fixture(scope="module")
def foo_service():
service = start_service("foo")
service.saw_problem = False
yield service
if service.saw_problem:
print(get_service_logs("foo"))
stop_service("foo")
@pytest.fixture
def foo(foo_service, request):
request.node.stash[service_key] = foo_service
return foo_serviceHave tests (and fixtures whose setup should count) depend on |
Uh oh!
There was an error while loading. Please reload this page.
Today, we advise our library users to set up a fixture that looks like this:
Which is fine, except it's imprecise.
The logs are dumped when any test in the session, not current module has failed.
(I'm also unclear about tests that errored out, e.g. some other fixture raised, rather than failed).
Ideally, the module-scoped fixture could get something like
requestormodule_requestargument that allows it to interrogate the tests for which this very instance of the fixture was set up.Is there a way to do this?
Would this be a good feature for pytest itself?
All reactions