pytest-dev / pytest-dev/pytest-asyncio
Allow erroring on remaining tasks after test exit as an option
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 5h 35m
- Merged PRs (30d)
- 9
Description
Problem
In ophyd-async, we want a test environment where if there are unfinished tasks after any test has finished then the test will error. We had quite a lot "task destroyed but it was pending" teardown errors which would pop up in later tests, with no description of which test the task was started in.
To fix this, we made an autouse fixture which would add a finalizer to every test and raise an error if there are any tasks still running after the test finishes, and cancel the task.
This gives us a much more precise error on the exact task/test which hasn't been handled, but most importantly ensures later tests don't raise errors solely by virtue of running when the previous tests task is garbage collected:
ERROR tests/epics/adaravis/test_aravis.py::test_unsupported_trigger_excepts - RuntimeError: Not all tasks closed during test test_unsupported_trigger_excepts:
{<Task cancelling name='Task-2282' coro=<_wrap_awaitable() running at /dls_sw/apps/python/miniforge/4.10.0-0/envs/python3.11/lib/python3.11/asyncio/tasks.py:684> wait_for=<Task finished name='Task-2281' coro=<SignalW.set() done, defined at /scratch/twj43146/Programming/ophyd-async/src/ophyd_async/core/_signal.py:228> result=None created at /dls_sw/apps/python/miniforge/4.10.0-0/envs/python3.11/lib/python3.11/asyncio/tasks.py:374> cb=[gather.<locals>._done_callback() at /dls_sw/apps/python/miniforge/4.10.0-0/envs/python3.11/lib/python3.11/asyncio/tasks.py:754] created at /dls_sw/apps/python/miniforge/4.10.0-0/envs/python3.11/lib/python3.11/asyncio/tasks.py:670>}
Suggestion
I couldn't find details on options to get this behaviour in pytest out of the box, but it seems quite useful. Could something like this be a configurable option and packaged with pytest?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the referenced ophyd-async tests/conftest.py fixture at lines 72-137 to understand the desired cleanup behavior. Then inspect pytest-asyncio's existing fixture and event-loop handling and determine where a configurable option belongs. Done means the option detects and reports unfinished tasks after each test, cancels them, and has focused tests for enabled and disabled behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100