Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache exceptions raised by shared fixtures #43

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions pytest_asyncio_cooperative/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ async def __call__(self, *args, **kwargs):
async with self.lock:
if hasattr(self, "value"):
return self.value
if inspect.iscoroutinefunction(self.wrapped_func):
self.value = await self.wrapped_func(*args, **kwargs)
else:
self.value = self.wrapped_func(*args, **kwargs)
if hasattr(self, "exception"):
raise self.exception
try:
if inspect.iscoroutinefunction(self.wrapped_func):
self.value = await self.wrapped_func(*args, **kwargs)
else:
self.value = self.wrapped_func(*args, **kwargs)
except Exception as e:
self.exception = e
raise
return self.value


Expand Down Expand Up @@ -161,10 +167,16 @@ def __next__(self):
return self.gen.__next__()
if hasattr(self, "value"):
return self.value
if hasattr(self, "exception"):
raise self.exception
else:
gen = self.wrapped_func(*self.args)
self.gen = gen
self.value = gen.__next__()
try:
gen = self.wrapped_func(*self.args)
self.gen = gen
self.value = gen.__next__()
except Exception as e:
self.exception = e
raise
return self.value


Expand Down Expand Up @@ -206,10 +218,16 @@ async def __anext__(self):
async with self.lock:
if hasattr(self, "value"):
return self.value
if hasattr(self, "exception"):
raise self.exception
else:
gen = self.wrapped_func(*self.args)
self.gen = gen
self.value = await gen.__anext__()
try:
gen = self.wrapped_func(*self.args)
self.gen = gen
self.value = await gen.__anext__()
except Exception as e:
self.exception = e
raise
return self.value


Expand Down
43 changes: 43 additions & 0 deletions tests/test_fixture.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

def test_function_fixture(testdir):
testdir.makepyfile(
"""
Expand Down Expand Up @@ -456,3 +458,44 @@ async def test_a(aaa, bbb):
result = testdir.runpytest("--asyncio-task-timeout=3")

result.assert_outcomes(passed=1)


@pytest.mark.parametrize("scope", ["module", "session"])
@pytest.mark.parametrize("def_", ["def", "async def"])
@pytest.mark.parametrize("ret", ["return", "yield"])
@pytest.mark.parametrize("fail", [False, True])
def test_shared_fixture_caching(testdir, scope, def_, ret, fail):
testdir.makepyfile(
f"""
import pytest
import time

called = False
@pytest.fixture({scope=})
{def_} shared_fixture():
global called
if called:
assert {fail}
else:
called = True
assert not {fail}
{ret}

@pytest.mark.asyncio_cooperative
async def test_a(shared_fixture):
assert True

@pytest.mark.asyncio_cooperative
async def test_b(shared_fixture):
assert True
"""
)

result = testdir.runpytest()

if fail:
result.assert_outcomes(failed=2)
# Should be errors instead of failures
# https://github.com/willemt/pytest-asyncio-cooperative/issues/42
else:
result.assert_outcomes(passed=2)