pytest-dev / pytest-dev/pytest-asyncio

Test timeout via marker kwargs

Open
#215 2 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
1.7k
Forks
207
Avg merge
5h 35m
Merged PRs (30d)
9

Description

At dask.distributed we started using pytest-asyncio for increasingly many tests. Before this, we've been decorating our plain asyncio tests using our own decorator which is not unlike to what pytest-asyncio is using under the hood, see https://github.com/dask/distributed/blob/833c5f6c040feaa4550fa343d8e6e4feef3f84d5/distributed/utils_test.py#L753-L772 but that has the obvious shortcoming that it doesn't integrate that nicely into pytest.

For our use case the timeout argument is very important since many tests are waiting until certain conditions are true. Moving to pytest-asyncio required us to use https://pypi.org/project/pytest-timeout/ but for portability reasons this plugin terminates the entire test process if a single test timeouts (see thread vs signal in their docs) which is not great.

A better integration with asyncio would be relatively easy to achieve and would allow us to improve our integration with pytest further. The syntax I had in mind is simply

@pytest.mark.asyncio(timeout=30)
async def test_requires_timeout():
    await stuff()

which, under the hood, would wrap the coro with https://docs.python.org/3/library/asyncio-task.html#asyncio.wait_for and would raise an asyncio.TimeoutError if the test runs for too long.
A default value for this timeout could be provided via a configuration file or similar.

If this overlap in scope is acceptable for this repo I would prepare and open a PR.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing pytest-asyncio marker behavior and the proposed @pytest.mark.asyncio(timeout=30) syntax. Check how the coroutine can use asyncio.wait_for and how a default timeout could come from configuration; done means an overlong async test raises asyncio.TimeoutError without terminating the entire test process.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.