
Python Testing
Structure Python features with pytest and TDD—fixtures, mocks, parametrization, and coverage gates—so solo builders ship backends and CLIs with confidence.
Overview
Python Testing is an agent skill most often used in Ship (also Build) that teaches pytest-first TDD, fixtures, mocking, and 80%+ coverage workflows for Python apps.
Install
npx skills add https://github.com/affaan-m/everything-claude-code --skill python-testingWhat is this skill?
- TDD cycle documented: RED → GREEN → REFACTOR with minimal worked example
- Coverage targets: 80%+ overall with 100% on critical paths via `pytest --cov`
- pytest fundamentals: structure, fixtures, mocking, and parametrization patterns
- Activation triggers for new code, suite design, coverage review, and infra setup
- CLI recipe for terminal-missing and HTML coverage reports
- 80%+ code coverage target
- 100% coverage required on critical paths
- 3-step TDD cycle: RED, GREEN, REFACTOR
Adoption & trust: 5.9k installs on skills.sh; 210k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You ship Python changes fast with an agent but lack a consistent pytest/TDD ritual, so bugs slip through and nobody tracks coverage on critical paths.
Who is it for?
Solo builders on Python backends or CLIs who want agent-assisted TDD and pytest coverage gates baked into every feature.
Skip if: Teams standardized on unittest only with no pytest adoption, or frontend-only projects with no Python test harness.
When should I use this skill?
Writing new Python code (TDD), designing test suites, reviewing coverage, or setting up pytest infrastructure.
What do I get? / Deliverables
You get pytest tests aligned to RED-GREEN-REFACTOR, coverage commands, and suite patterns so merges are backed by measurable tests before release.
- pytest test modules with assertions
- Coverage reports via `pytest --cov` (terminal and HTML)
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Primary shelf is Ship/testing because the skill defines pytest suites, coverage targets, and QA workflows. Content is test design and execution (`pytest --cov`), not app feature code or production monitoring.
Where it fits
You add a FastAPI endpoint and the agent writes the failing pytest first per RED-GREEN-REFACTOR.
Before tagging a release you run `pytest --cov` and fill gaps on payment or auth critical paths.
A production bug gets a regression test added with fixtures before the hotfix ships.
How it compares
Use for pytest/TDD procedure—not as a one-off 'generate a single test file' snippet without coverage or fixture strategy.
Common Questions / FAQ
Who is python-testing for?
Indie developers and small teams building Python packages, APIs, or automation who want structured pytest and TDD guidance in the agent loop.
When should I use python-testing?
During Build when writing new Python modules (TDD from the first test); during Ship when expanding suites, measuring coverage, or reviewing test gaps before release.
Is python-testing safe to install?
It describes test commands that run locally; review the Security Audits panel on this Prism page and restrict agent shell access when running pytest in untrusted repos.
SKILL.md
READMESKILL.md - Python Testing
# Python Testing Patterns Comprehensive testing strategies for Python applications using pytest, TDD methodology, and best practices. ## When to Activate - Writing new Python code (follow TDD: red, green, refactor) - Designing test suites for Python projects - Reviewing Python test coverage - Setting up testing infrastructure ## Core Testing Philosophy ### Test-Driven Development (TDD) Always follow the TDD cycle: 1. **RED**: Write a failing test for the desired behavior 2. **GREEN**: Write minimal code to make the test pass 3. **REFACTOR**: Improve code while keeping tests green ```python # Step 1: Write failing test (RED) def test_add_numbers(): result = add(2, 3) assert result == 5 # Step 2: Write minimal implementation (GREEN) def add(a, b): return a + b # Step 3: Refactor if needed (REFACTOR) ``` ### Coverage Requirements - **Target**: 80%+ code coverage - **Critical paths**: 100% coverage required - Use `pytest --cov` to measure coverage ```bash pytest --cov=mypackage --cov-report=term-missing --cov-report=html ``` ## pytest Fundamentals ### Basic Test Structure ```python import pytest def test_addition(): """Test basic addition.""" assert 2 + 2 == 4 def test_string_uppercase(): """Test string uppercasing.""" text = "hello" assert text.upper() == "HELLO" def test_list_append(): """Test list append.""" items = [1, 2, 3] items.append(4) assert 4 in items assert len(items) == 4 ``` ### Assertions ```python # Equality assert result == expected # Inequality assert result != unexpected # Truthiness assert result # Truthy assert not result # Falsy assert result is True # Exactly True assert result is False # Exactly False assert result is None # Exactly None # Membership assert item in collection assert item not in collection # Comparisons assert result > 0 assert 0 <= result <= 100 # Type checking assert isinstance(result, str) # Exception testing (preferred approach) with pytest.raises(ValueError): raise ValueError("error message") # Check exception message with pytest.raises(ValueError, match="invalid input"): raise ValueError("invalid input provided") # Check exception attributes with pytest.raises(ValueError) as exc_info: raise ValueError("error message") assert str(exc_info.value) == "error message" ``` ## Fixtures ### Basic Fixture Usage ```python import pytest @pytest.fixture def sample_data(): """Fixture providing sample data.""" return {"name": "Alice", "age": 30} def test_sample_data(sample_data): """Test using the fixture.""" assert sample_data["name"] == "Alice" assert sample_data["age"] == 30 ``` ### Fixture with Setup/Teardown ```python @pytest.fixture def database(): """Fixture with setup and teardown.""" # Setup db = Database(":memory:") db.create_tables() db.insert_test_data() yield db # Provide to test # Teardown db.close() def test_database_query(database): """Test database operations.""" result = database.query("SELECT * FROM users") assert len(result) > 0 ``` ### Fixture Scopes ```python # Function scope (default) - runs for each test @pytest.fixture def temp_file(): with open("temp.txt", "w") as f: yield f os.remove("temp.txt") # Module scope - runs once per module @pytest.fixture(scope="module") def module_db(): db = Database(":memory:") db.create_tables() yield db db.close() # Session scope - runs once per test session @pytest.fixture(scope="session") def shared_resource(): resource = ExpensiveResource() yield resource resource.cleanup() ``` ### Fixture with Parameters ```python @pytest.fixture(params=[1, 2, 3]) def number(request): """Parameterized fixture.""" return request.param def test_numbers(numbe