/dev-tools:writing-tests-pythonfrom dev-tools
Instructions for writing Python tests (pytest), including mocking, fixtures, and golden path standards.
Ships in dev-tools. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
If this plugin is not initialized or an Agent IX command fails, read the dev-tools setup guide for its prerequisites and local diagnosis.
This skill provides generic guidelines for writing Python tests using pytest, compliant with the Agent-IX Golden Path.
Source Spec: Standard Python best practices and Agent-IX conventions.
pytest.class TestUserService:).pytest-sqlmodel for fixtures for db setup and transactional isolation. READ writing-tests-pg-data for specific instructions.pytest-mock (the mocker fixture) for all mocking. Do NOT use unittest.mock.patch decorators.Follow the Agent-IX style for test docstrings.
Format:
Attach @pytest.mark.trace("FR-001-AC-01", "FR-005-AC-1") to each test, listing
the criteria it actually asserts. Import pytest. Quire reads these marker
attributes statically under the module's declared traceability model. Docstring
criterion mentions are prose, not a replacement for the marker. Do not add rows
to spec/tests.md.
This is the canonical Python form in Quire's trace-tag grammar, verified with Quire CLI 0.53.3.
import pytest
class TestMathOperations:
"""Tests for basic math operations."""
@pytest.mark.trace("FR-001-AC-01")
def test_addition(self):
"""
Description:
Test that addition returns the sum of two numbers.
Assumptions:
- Inputs are positive integers
Criteria:
- Result equals the mathematical sum of inputs
- Result is a positive integer
"""
assert add(1, 2) == 3
Use conftest.py for shared fixtures. Keep fixtures simple and specific to the application logic (not DB).
@pytest.fixture
def sample_config():
"""Provides a default configuration for testing."""
return {"timeout": 30, "retries": 3}
Design your classes to accept external dependencies in __init__. This avoids patching entirely and leads to cleaner, more testable code.
# Code
class Service:
def __init__(self, client: httpx.Client):
self.client = client
# Test
def test_service(mocker):
# Create a mock object directly
mock_client = mocker.Mock(spec=httpx.Client)
# Inject it
service = Service(client=mock_client)
# Act
service.do_something()
# Assert
mock_client.get.assert_called_once()
If DI is not possible, use mocker.patch to mock the External Edge where it is imported.
my_module.httpx.Client), not where it is defined.Do NOT use @patch decorators. They are prone to ordering errors.
def test_legacy_service(mocker):
# Patching the import in 'my_module'
mock_post = mocker.patch("my_module.httpx.post")
mock_post.return_value.json.return_value = {"id": "123", "status": "success"}
# Call the real internal service
result = service.create_order(item="book")
assert result.id == "123"
# BAD: Mocking internal logic
mocker.patch("my_app.services.calculate_total", return_value=100)
# BAD: Using decorators (confusing argument order)
@patch("my_module.foo")
def test_something(self, mock_foo):
pass
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub agent-ix/dev-tools --plugin dev-tools15 skills in this plugin
/audit-commentsAudit comments in a repository or bounded code section, remove tautologies and stale notes, convert decision records to ADRs, file issues for hack-enabling excuses, and add a ratcheting lint rule. Use when asked to audit or delete code comments./backport-codeA skill for systematically backporting bug fixes and features across specs, templates, and downstream repositories./code-reviewReview code for Agent-IX Golden Path compliance, focusing on test style, mock boundaries, implementation gaps, and code-test alignment. Dispatches by language — Python inline, Rust via `rust-review`, React via `review-react`./docs-figuresDraw diagrams, charts and CSS animations for GitHub READMEs and documentation as transparent light and dark SVG pairs, with text drawn as glyph outlines so every viewer renders it identically. Use when a README or doc page needs a mechanism or flow diagram, a chart, an animated walkthrough, figures in the ix-docs style, or when a repository's docs/images figures must be regenerated or checked./docs-styleWrite and review READMEs and docs pages in the Agent IX house style. Use when writing a README or docs page. Use it when asked to review docs for filler, "Claude-ish" wording, sprawl, indirect framing or implementation status. Use it when a README or docs folder must be brought to the agent-ix standard. Gives a page-type guide, a rule catalog, a Vale style and a structure checker. Figures are made with docs-figures./ix-buildRun or inspect shared-host build leases with ix-build. Use when coordinating Cargo builds, checking queued work, or diagnosing memory-cap outcomes.Other plugins' skills for the same job