Skip to content

Commit d3188a1

Browse files
committed
Invoke teardowns of already filled fixtures on fixture errors
1 parent 07944b5 commit d3188a1

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

pytest_asyncio_cooperative/fixtures.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,26 @@ async def fill_fixtures(item):
6969

7070
# Fill fixtures concurrently
7171
fill_results = await asyncio.gather(
72-
*(fill_fixture_fixtures(item._fixtureinfo, fixture, item) for fixture in fixtures)
72+
*(fill_fixture_fixtures(item._fixtureinfo, fixture, item) for fixture in fixtures),
73+
return_exceptions=True
7374
)
7475

75-
for ((value, extra_teardowns), is_autouse) in zip(fill_results, are_autouse):
76+
exceptions = []
77+
for result, is_autouse in zip(fill_results, are_autouse):
78+
if isinstance(result, BaseException):
79+
exceptions.append(result)
80+
continue
81+
82+
value, extra_teardowns = result
7683
teardowns.extend(extra_teardowns)
7784

7885
if not is_autouse:
7986
fixture_values.append(value)
8087

88+
if exceptions:
89+
await do_teardowns(teardowns)
90+
raise exceptions[0]
91+
8192
# Slight hack to stop the regular fixture logic from running
8293
item.fixturenames = []
8394

@@ -93,9 +104,15 @@ async def _fill_fixture_fixtures(_fixtureinfo, fixture, item):
93104
except Ignore:
94105
continue
95106

96-
value, teardowns = await fill_fixture_fixtures(_fixtureinfo, dep_fixture, item)
107+
try:
108+
value, teardowns = await fill_fixture_fixtures(_fixtureinfo, dep_fixture, item)
109+
except:
110+
await do_teardowns(all_teardowns)
111+
raise
112+
97113
values.append(value)
98114
all_teardowns.extend(teardowns)
115+
99116
return values, all_teardowns
100117

101118

0 commit comments

Comments
 (0)