pytest-patterns
Skill110 starsChanged 9 months agoApache-2.0View on GitHub
pytest best practices for writing comprehensive test suites. Use when writing or running Python tests, setting up test fixtures, mocking, or achieving good test coverage.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- hieutrtr/ai1-skillstheir own version, first seen Feb 5, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- hieutrtr/ai1-skills★ 8same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository75 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- algorithmic-artSkill
- api-designSkill
- apple-notesSkill
- apple-remindersSkill
- article-writingSkill
- async-pythonSkill
- brand-guidelinesSkill
- camsnapSkill
- canvas-designSkill
- celery-tasksSkill
- claude-apiSkill
- coding-agentSkill
- content-engineSkill
- diffsSkill
- doc-coauthoringSkill
- docker-deploymentSkill
- docxSkill
- e2e-testingSkill
- email-serviceSkill
- fastapi-backendSkill
- frontend-designSkill
- frontend-slidesSkill
- gh-issuesSkill
- githubSkill
- git-workflowSkill
- graphql-apiSkill
- healthcheckSkill
- internal-commsSkill
- investor-materialsSkill
- jwt-authenticationSkill
- market-researchSkill
- mcp-builderSkill
- mermaid-diagramsSkill
- microservicesSkill
- mobile-react-nativeSkill
- model-usageSkill
- nextjs-fullstackSkill
- nodejs-expressSkill
- obsidianSkill
- openai-whisperSkill
- oracleSkill
- pdfSkill
- peekabooSkill
- planning-with-filesSkill
- postgres-databaseSkill
- pptxSkill
- prisma-ormSkill
- proseSkill
- react-typescriptSkill
- redis-cachingSkill
- s3-file-storageSkill
- security-reviewSkill
- session-logsSkill
- skill-creatorSkill
- slack-gif-creatorSkill
- sqlalchemy-ormSkill
- state-managementSkill
- strategic-compactSkill
- stripe-paymentsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 105
Every fixture that creates DB data must rollback after test
- Distinctive: line 107
Test both success AND failure paths for every endpoint
- Distinctive: line 108
Use parametrize for boundary conditions
- Distinctive: line 109
Keep unit tests pure (no I/O, no DB) — they must run in < 1ms each
- Distinctive: line 111
Test names must describe WHAT they test: test_login_with_wrong_password_returns_401
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
pytest Patterns
Structure
tests/
conftest.py # Shared fixtures
unit/ # Pure unit tests (no I/O)
integration/ # DB, Redis, API tests
e2e/ # Full flow tests
test_models.py # Test naming: test_<module>.pyconftest.py (Fixtures)
import pytest
from httpx import AsyncClient, ASGITransport
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
@pytest.fixture(scope="session")
def event_loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
async def engine():
engine = create_async_engine("postgresql+asyncpg://test:test@localhost/test_db")
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
@pytest.fixture
async def db(engine):
async with AsyncSession(engine) as session:
yield session
await session.rollback()
@pytest.fixture
async def client(db):
app.dependency_overrides[get_db] = lambda: db
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
app.dependency_overrides.clear()
@pytest.fixture
async def user(db):
u = User(email="test@example.com", name="Test")
db.add(u)
await db.flush()
return uTest Patterns
# Parametrize for edge cases
@pytest.mark.parametrize("email,valid", [
("user@example.com", True),
("invalid", False),
("", False),
("a" * 300 + "@x.com", False),
])
async def test_email_validation(email, valid):
...
# Mock external services
from unittest.mock import AsyncMock, patch
async def test_sends_email(client, user):
with patch("app.services.email.send") as mock_send:
mock_send.return_value = None
resp = await client.post("/users/verify", json={"email": user.email})
assert resp.status_code == 200
mock_send.assert_called_once_with(user.email, subject="Verify your account")
# Test error cases
async def test_get_user_not_found(client):
resp = await client.get("/users/99999")
assert resp.status_code == 404
assert "not found" in resp.json()["detail"].lower()Running Tests
# Full suite with coverage
pytest tests/ -v --tb=short --cov=app --cov-report=term-missing
# Fast: only unit tests
pytest tests/unit/ -v
# Specific test
pytest tests/test_auth.py::test_login_success -v
# Stop on first failure
pytest -xRules
- Every fixture that creates DB data must rollback after test
- Mock ALL external APIs (email, Stripe, S3) — never call real ones in tests
- Test both success AND failure paths for every endpoint
- Use parametrize for boundary conditions
- Keep unit tests pure (no I/O, no DB) — they must run in < 1ms each
- Coverage goal: 80%+ for services, 60%+ overall
- Test names must describe WHAT they test: test_login_with_wrong_password_returns_401
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

