Skip to content

Pytest testing patterns for Python covering fixtures, mocking, parametrization, and markers in one reference.

CCM score
56 of 100
skills.sh installs
301
GitHub stars
681
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers test structure, fixtures with teardown and scopes, conftest.py sharing, mocking with unittest.mock, and async tests
  • Includes parametrize examples for input/expected pairs, custom markers for slow and integration, and a list of common pytest command flags
  • Provides guidance on pytest.raises for error assertions and skip/skipif for conditional test execution
  • Designed for Python projects that need consistent test organization, with pytest-asyncio and pytest-xdist noted for async and parallel runs

Install

$ npx -y skills add gentleman-programming/gentleman-skills --skill pytest -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

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

Details

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

Questions

What does the pytest skill help an AI agent do?

The pytest skill teaches an AI agent Pytest testing patterns for Python. It covers writing tests with fixtures, mocking, parametrize, markers, and async tests, plus common pytest commands for running them.

How do I install the pytest skill?

Run npx -y skills add gentleman-programming/gentleman-skills --skill pytest -y in your project. Once installed, the agent can follow these Pytest patterns when you write Python tests.

Where does the pytest skill stand in the Testing & QA category?

The pytest skill sits in the Testing & QA category on Claude Marketplaces. Other skills in that category include tdd, test-driven-development, webapp-testing and break-ui, which cover different testing approaches.

Does the pytest skill cover mocking and fixtures?

Yes, the skill covers fixtures with setup and teardown, fixture scopes, conftest.py shared fixtures, and mocking with unittest.mock patch and MagicMock. It also shows parametrized tests and custom markers.

More from gentleman-programming/gentleman-skills

All 13

Similar to Pytest

More Testing & QA