A solid reference for pytest patterns that covers the essentials without fluff. You get fixture scoping and teardown examples, mocking with patch and MagicMock, parametrize syntax for data-driven tests, and marker configurations. The conftest.py section is brief but shows the shared fixture pattern most projects need. The async test example is almost too minimal, just showing the decorator, but the commands section at the end is genuinely handy for things like running tests in parallel or filtering by marker. This is the kind of cheat sheet you want open when you're setting up a new test suite or can't remember the exact syntax for fixture scopes.

skills.sh installs
291
GitHub stars
665
last updated
Aug 21, 2026

Install

$ npx -y skills add gentleman-programming/gentleman-skills --skill pytest --agent claude-code

Installs into .claude/skills of the current project.

Files

Basic Test Structure

import pytest

class TestUserService:
    def test_create_user_success(self):
        user = create_user(name="John", email="john@test.com")
        assert user.name == "John"
        assert user.email == "john@test.com"

    def test_create_user_invalid_email_fails(self):
        with pytest.raises(ValueError, match="Invalid email"):
            create_user(name="John", email="invalid")

Fixtures

import pytest

@pytest.fixture
def user():
    """Create a test user."""
    return User(name="Test User", email="test@example.com")

@pytest.fixture
def authenticated_client(client, user):
    """Client with authenticated user."""
    client.force_login(user)
    return client

# Fixture with teardown
@pytest.fixture
def temp_file():
    path = Path("/tmp/test_file.txt")
    path.write_text("test content")
    yield path  # Test runs here
    path.unlink()  # Cleanup after test

# Fixture scopes
@pytest.fixture(scope="module")  # Once per module
@pytest.fixture(scope="class")   # Once per class
@pytest.fixture(scope="session") # Once per test session

conftest.py

# tests/conftest.py - Shared fixtures
import pytest

Sponsors

Details

Repository
github.com/gentleman-programming/gentleman-skills
Categories
Testing & QA, Documentation, Python
First seen
Jun 3, 2026

More from gentleman-programming/gentleman-skills

All 13
1zeroz-lab avatarcc-designzeroz-lab/cc-designHigh-fidelity HTML design and prototype creation. Use this skill whenever the user asks to design, prototype, mock up, or build visual artifacts in HTML — inclu287skills.sh installs
2patricio0312rev avatargithub-actions-pipeline-creatorpatricio0312rev/skillsCreates comprehensive GitHub Actions CI/CD workflows for linting, testing, building, and deploying. Includes caching strategies, matrix builds, artifact handlin284skills.sh installs
3patricio0312rev avatarevaluation-harnesspatricio0312rev/skillsBuilds repeatable evaluation systems with golden datasets, scoring rubrics, pass/fail thresholds, and regression reports. Use for "LLM evaluation", "testing AI 272skills.sh installs
4pproenca avatarios-chaos-monkeypproenca/dot-skillsCrash-hunter skill for iOS 26 / Swift 6.2 clinic-architecture codebases that finds and fixes concurrency, memory, and I/O bugs using TDD. Covers data races, act267skills.sh installs
5awslabs avataraws-lambdaawslabs/agent-pluginsDesign, build, deploy, test, and debug serverless applications with AWS Lambda. Triggers on phrases like: Lambda function, event source, serverless application,260skills.sh installs
6casper-studios avataragent-browsercasper-studios/casper-marketplaceBrowser automation and testing using Vercel's agent-browser CLI with ref-based element targeting257skills.sh installs