py-testing-asynclisted
Install: claude install-skill aiskillstore/marketplace
# Python Async Testing
## Problem Statement
Async testing requires specific patterns. pytest-asyncio has modes that affect behavior. Database tests need isolation. Mocking async functions differs from sync. Get these wrong and tests are flaky or don't catch bugs.
---
## Pattern: pytest-asyncio Configuration
**Problem:** Pytest needs configuration for async tests.
```toml
# pyproject.toml
[tool.pytest.ini_options]
asyncio_mode = "strict" # Requires explicit @pytest.mark.asyncio
# OR
asyncio_mode = "auto" # All async tests run automatically
```
```python
import pytest
# With asyncio_mode = "strict" (this codebase)
@pytest.mark.asyncio
async def test_something():
result = await some_async_function()
assert result == expected
# Without the marker = test won't run as async!
```
---
## Pattern: Async Fixtures
**Problem:** Fixtures that provide async resources need specific handling.
```python
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
# ✅ CORRECT: Async fixture for session
@pytest.fixture
async def session() -> AsyncGenerator[AsyncSession, None]:
async with async_session() as session:
yield session
await session.rollback() # Clean up after test
# ✅ CORRECT: Async fixture for test data
@pytest.fixture
async def test_user(session: AsyncSession) -> User:
user = User(email="test@example.com", hashed_password="...")
session.add(user)
await session.commit()
await session.refresh(user)
return user
# ✅