Skip to content
Open
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: 35 additions & 3 deletions pytest_asyncio_cooperative/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import collections.abc
import inspect
from typing import List, Union

Expand Down Expand Up @@ -68,15 +69,26 @@ async def fill_fixtures(item):

# Fill fixtures concurrently
fill_results = await asyncio.gather(
*(fill_fixture_fixtures(item._fixtureinfo, fixture, item) for fixture in fixtures)
*(fill_fixture_fixtures(item._fixtureinfo, fixture, item) for fixture in fixtures),
return_exceptions=True
)

for ((value, extra_teardowns), is_autouse) in zip(fill_results, are_autouse):
exceptions = []
for result, is_autouse in zip(fill_results, are_autouse):
if isinstance(result, BaseException):
exceptions.append(result)
continue

value, extra_teardowns = result
teardowns.extend(extra_teardowns)

if not is_autouse:
fixture_values.append(value)

if exceptions:
await do_teardowns(teardowns)
raise exceptions[0]

# Slight hack to stop the regular fixture logic from running
item.fixturenames = []

Expand All @@ -92,12 +104,32 @@ async def _fill_fixture_fixtures(_fixtureinfo, fixture, item):
except Ignore:
continue

value, teardowns = await fill_fixture_fixtures(_fixtureinfo, dep_fixture, item)
try:
value, teardowns = await fill_fixture_fixtures(_fixtureinfo, dep_fixture, item)
except:
await do_teardowns(all_teardowns)
raise

values.append(value)
all_teardowns.extend(teardowns)

return values, all_teardowns


async def do_teardowns(teardowns):
for teardown in teardowns:
if isinstance(teardown, collections.abc.Iterator):
try:
teardown.__next__()
except StopIteration:
pass
else:
try:
await teardown.__anext__()
except StopAsyncIteration:
pass


class CachedFunctionBase(object):
def __init__(self, wrapped_func):
self.lock = asyncio.Lock()
Expand Down
28 changes: 5 additions & 23 deletions pytest_asyncio_cooperative/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import collections.abc
import functools
import inspect
import time
Expand All @@ -11,7 +10,7 @@
from _pytest.skipping import Skip, evaluate_skip_marks

from .assertion import activate_assert_rewrite
from .fixtures import fill_fixtures
from .fixtures import fill_fixtures, do_teardowns


def pytest_addoption(parser):
Expand Down Expand Up @@ -91,33 +90,16 @@ async def test_wrapper(item):
if item.instance:
fixture_values.insert(0, item.instance)

async def do_teardowns():
item.start_teardown = time.time()
for teardown in teardowns:
if isinstance(teardown, collections.abc.Iterator):
try:
teardown.__next__()
except StopIteration:
pass
else:
try:
await teardown.__anext__()
except StopAsyncIteration:
pass
item.stop_teardown = time.time()

# Run test
item.start = time.time()
try:
await item.function(*fixture_values)
except:
finally:
# Teardown here otherwise we might leave fixtures with locks acquired
item.stop = time.time()
await do_teardowns()
raise

item.stop = time.time()
await do_teardowns()
item.start_teardown = time.time()
await do_teardowns(teardowns)
item.stop_teardown = time.time()


# TODO: move to hypothesis module
Expand Down
29 changes: 29 additions & 0 deletions tests/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,32 @@ async def test_b(shared_fixture):
# https://github.com/willemt/pytest-asyncio-cooperative/issues/42
else:
result.assert_outcomes(passed=2)


def test_teardown_on_fixture_error(testdir):
testdir.makepyfile(
"""
import pytest

import asyncio
from warnings import warn

@pytest.fixture
async def fixture_ok():
await asyncio.sleep(1)
yield
warn("fixture_ok teardown")

@pytest.fixture
async def fixture_fail():
await asyncio.sleep(2)
assert False

@pytest.mark.asyncio_cooperative
async def test(fixture_ok, fixture_fail):
assert True
"""
)

with pytest.warns(UserWarning, match="fixture_ok teardown"):
result = testdir.runpytest()