Skip to content

Instantly share code, notes, and snippets.

@AivanF
Created March 6, 2025 03:22
Show Gist options
  • Save AivanF/2001bbcbd70abe76c469c247a8165b44 to your computer and use it in GitHub Desktop.
Save AivanF/2001bbcbd70abe76c469c247a8165b44 to your computer and use it in GitHub Desktop.
Workaround for not working pytest-timeout
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
@AivanF
Copy link
Author

AivanF commented Mar 6, 2025

Just copy the file to your project, call setup_aivanf_timeout() in your conftest.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment