Comprehensive pytest skill for async Python testing with proper mocking, fixtures, and patterns from production-ready test suites. Use when writing or improving async tests for Python applications, especially FastAPI backends with database interactions.
Comprehensive pytest skill for async Python testing with proper mocking, fixtures, and patterns from a production-ready 387-test FastAPI backend.
Activate this skill when you need to:
Simply invoke the skill when working on async testing:
/skill async-testing-expert
Or let Claude automatically detect when you need it by mentioning async testing in your prompts.
Use the included template generator:
# Generate DAO layer test template
python ~/.claude/skills/async-testing-expert/generate_test_template.py \
--module user \
--layer dao
# Generate Service layer test template
python ~/.claude/skills/async-testing-expert/generate_test_template.py \
--module publication \
--layer service
# Generate Router (API) layer test template
python ~/.claude/skills/async-testing-expert/generate_test_template.py \
--module deadline \
--layer router
This generates test files in your tests/ directory with:
The skill includes production-ready mock objects. Copy them to your project:
# Create fakes.py in your tests directory
cp ~/.claude/skills/async-testing-expert/examples/fakes.py tests/
# Create conftest.py with fixtures
cp ~/.claude/skills/async-testing-expert/examples/conftest.py tests/
async-testing-expert/
├── SKILL.md # Main skill instructions for Claude
├── README.md # This file
├── generate_test_template.py # Test template generator script
└── examples/ # Example files (coming soon)
├── fakes.py # Mock objects
├── conftest.py # Pytest fixtures
└── test_examples.py # Complete test examples
FakeConnection - Full database connection mock:
FakeRecord - Query result mock:
FakeTransaction - Transaction context mock:
DAO Layer (Data Access):
__wrapped__ to bypass connection decoratorsService Layer (Business Logic):
Router Layer (API Endpoints):
test_<what>_<scenario> (e.g., test_create_calls_execute, test_fetch_by_id_error_maps_to_500)@pytest.mark.asyncio
async def test_create_calls_execute(faker):
"""Test that create method calls execute with correct SQL and parameters."""
# Arrange
create_dto = UserDTO.Create(
name=faker.name(),
email=faker.email()
)
conn = FakeConnection()
# Act
await UserDAO.create.__wrapped__(conn, create_dto)
# Assert
assert len(conn.execute_calls) == 1
stmt, params = conn.execute_calls[0]
assert 'INSERT INTO users' in stmt
@pytest.mark.asyncio
async def test_service_coordinates_dao():
"""Test that service properly coordinates DAO calls."""
dao = DummyUserDAO()
service = UserService(user_dao=dao)
result = await service.get_all_users()
assert dao.fetch_all_called
assert isinstance(result[0], UserDTO.Read)
@pytest.mark.asyncio
async def test_get_users_endpoint(async_client, monkeypatch):
"""Test GET /users endpoint returns proper response."""
async def mock_get_users():
return [UserDTO.Read(id=1, name='Test', email='test@example.com')]
monkeypatch.setattr('src.api.path.users.get_all', mock_get_users)
response = await async_client.get('/users')
assert response.status_code == 200
assert len(response.json()) == 1
# Run all tests
pytest tests/ -v
# Run specific test file
pytest tests/test_user_dao.py -v
# Run with coverage
pytest --cov=src --cov-report=html tests/
# Run single test
pytest tests/test_user_dao.py::test_create_calls_execute -v
# Stop on first failure
pytest -x tests/
# Run only failed tests from last run
pytest --lf tests/
# Show local variables on failure
pytest --showlocals tests/
# Copy mock objects
mkdir -p tests
cat > tests/fakes.py << 'EOF'
# Paste FakeConnection, FakeRecord, FakeTransaction from SKILL.md
EOF
# Copy fixtures
cat > tests/conftest.py << 'EOF'
# Paste fixtures from SKILL.md
EOF
pip install pytest pytest-asyncio httpx faker
# or with rye
rye add --dev pytest pytest-asyncio httpx faker
Create pyproject.toml or pytest.ini:
[tool.pytest.ini_options]
asyncio_mode = "auto"
testpaths = ["tests"]
python_files = "test_*.py"
python_classes = "Test*"
python_functions = "test_*"
This skill is based on patterns extracted from the SISJUR backend project:
This skill is provided as-is for educational and development purposes. Feel free to adapt and modify for your needs.
Found an issue or have suggestions? This skill is designed to evolve with your testing needs. Share feedback to improve it!
npx skills add rafaelkamimura/Async Testing Expert下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer