Fix errors · Skill
Write Python tests with pytest
write-python-tests-with-pytest
Writes clear, fast Python tests with pytest - arrange-act-assert structure, fixtures in conftest.py, parametrize for many cases, mocking only at the edges, and coverage reports. Use when the user wants to add Python tests, set up pytest, test a function or API, or fix flaky or slow Python tests.
Works with: Any AI Not yet model-tested; testing Jan 2027
Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works
Install in Claude Code, Cursor or Codex (optional)
Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.
Claude Code (all your projects)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/raw' -o ~/'.claude/skills/write-python-tests-with-pytest/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/files/LICENSE.txt' -o ~/'.claude/skills/write-python-tests-with-pytest/LICENSE.txt'Cursor (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/raw' -o '.cursor/skills/write-python-tests-with-pytest/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/files/LICENSE.txt' -o '.cursor/skills/write-python-tests-with-pytest/LICENSE.txt'Codex and other Agent Skills tools (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/raw' -o '.agents/skills/write-python-tests-with-pytest/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-python-tests-with-pytest/files/LICENSE.txt' -o '.agents/skills/write-python-tests-with-pytest/LICENSE.txt'
Or connect the whole library over MCP
Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):
{
"mcpServers": {
"theskillvault": {
"url": "https://theskillvault.ai/mcp"
}
}
}pytest finds files named test_*.py and runs every function that starts with test_. Plain assert is all you need.
When to use
- Your Python code has no tests and you want a safety net.
- You need to test a FastAPI, Flask or Django endpoint.
- Tests are flaky, slow or hard to read.
When not to use
- You want to write the test before the code as a habit. Use test-first-with-tdd, then come back for pytest details.
Steps
- Install:
pip install pytest pytest-cov(addpytest-asynciofor async code). - Make a
tests/folder that mirrors your package, with aconftest.pyfor shared fixtures. - Write each test as arrange, act, assert. One behaviour per test, named for it:
test_refund_fails_when_order_is_shipped. - Use fixtures for setup you repeat, such as a test database or client. Keep their scope as small as works.
- Use
@pytest.mark.parametrizeto run one test over many inputs. - Check errors with
with pytest.raises(ValueError):. - Mock only at the edges: network calls, time, payment providers. Use
monkeypatchorunittest.mock. - Run
pytest -q, thenpytest --cov=yourpackage --cov-report=term-missingto see untested lines. - Add the test command to CI.
import pytest
from shop.pricing import apply_discount
@pytest.mark.parametrize("price, code, expected", [(100, "TEN", 90), (100, None, 100)])
def test_apply_discount(price, code, expected):
assert apply_discount(price, code) == expected
Gotchas
- Tests that share state leak into each other. Use fixtures, not module-level globals.
- Never point tests at a production database or real API keys. Use a test database and fake credentials.
- Over-mocking tests your mocks, not your code. Prefer real objects inside your own code.
- Use
tmp_pathfor files, not real folders.
Portability
Works with any Python project and framework. If you use unittest today, pytest runs those tests too, so you can switch gradually instead of rewriting.
Example prompt
"Add pytest tests for my pricing module, including edge cases, and show me the coverage."
Done when
pytestpasses locally and in CI.- Each important behaviour and error path has a named test.
- No test touches real services, real money or real secrets.
---
Adapted from "python-testing-patterns" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.
Bundled files
Licence
Adapted from Seth Hobson (wshobson/agents, python-testing-patterns) (Copyright (c) 2024 Seth Hobson), MIT License. Adapted by TheSkillVault: rewritten as a short beginner workflow with original examples; dropped the long pattern catalogue; added safety notes, gotchas and done-when.
Licence: MIT License. Attribution when you reuse it: Based on work by Seth Hobson (wshobson/agents, python-testing-patterns) (Copyright (c) 2024 Seth Hobson, MIT License), https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/python-development/skills/python-testing-patterns; adapted by TheSkillVault, https://theskillvault.ai