Created
March 6, 2025 03:22
-
-
Save AivanF/2001bbcbd70abe76c469c247a8165b44 to your computer and use it in GitHub Desktop.
Workaround for not working pytest-timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import functools | |
import pytest | |
def aivanf_timeout(timeout: float = 60): | |
def inner(func): | |
@functools.wraps(func) | |
async def wrapper(*args, **kwargs): | |
return await asyncio.wait_for(func(*args, **kwargs), timeout=timeout) | |
return wrapper | |
return inner | |
def setup_aivanf_timeout(): | |
""" | |
IDK why, by pytest-timeout doesn't work in some Python versions. | |
https://github.com/pytest-dev/pytest-timeout/issues/113 | |
""" | |
pytest.mark.timeout = aivanf_timeout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just copy the file to your project, call
setup_aivanf_timeout()
in yourconftest.py
, and use@pytest.mark.timeout(_N_)
on your tests. Actually, the pytest-timeout lib isn't even needed to be installed, although my approach only works for async coroutines (and not for sync functions).License: MIT, also I'd be glad to see your feedback here 😁 If this would be helpful to many people, I can publish it as a standalone lib on PyPI.