# pytest-patterns

pytest-patterns is a Claude Code skill for writing Python backend tests with pytest. It comes into play for service, repository, and FastAPI API tests.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 123 installs · ★ 8 repo stars
- Author: platform-team
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/hieutrtr/ai1-skills/tree/HEAD/skills/pytest-patterns/SKILL.md
- Updated: 2026-02-06
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/platform-team-pytest-patterns

## What it does

pytest-patterns guides the organization of pytest tests for Python applications. It lays out separate `tests/unit` and `tests/integration` areas, a `conftest.py` fixture hierarchy, and naming conventions for tests.

For API checks, it uses `httpx.AsyncClient`; `factory_boy` supplies test data, while `pytest-asyncio` supports async tests. The skill also covers mocks and parametrized cases. It requires Python 3.12+, pytest 8+, `pytest-asyncio`, `httpx`, and `factory_boy`.

React component tests and browser-based E2E tests are outside its scope. It also describes markers for grouping unit and integration tests, with `pytest` commands to run selected groups.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add hieutrtr/ai1-skills --skill pytest-patterns -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/pytest-patterns/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/pytest-patterns/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add hieutrtr/ai1-skills --skill pytest-patterns -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/pytest-patterns/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add hieutrtr/ai1-skills --skill pytest-patterns -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/pytest-patterns/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install hieutrtr/ai1-skills skills/pytest-patterns
```

2. **Manually: put the files in place**
- File: `.github/skills/pytest-patterns/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add hieutrtr/ai1-skills --skill pytest-patterns -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/hieutrtr/ai1-skills --path skills/pytest-patterns
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/pytest-patterns/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add hieutrtr/ai1-skills --skill pytest-patterns -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/hieutrtr/ai1-skills/tree/HEAD/skills/pytest-patterns/SKILL.md

## When to use it
- organize tests into unit and integration directories
- set up shared and directory-level fixtures in `conftest.py`
- build test objects with `factory_boy`
- test async FastAPI endpoints using `httpx.AsyncClient`
- add mocks and parametrized cases

## Example prompts
- Write pytest unit tests for a service and its repository.
- Set up `conftest.py` fixtures for FastAPI integration tests.
- Add async API tests and a factory for test users.

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 00a02b66e8500c2c.

BEGIN THIRD-PARTY FILE 00a02b66e8500c2c
````text
---
name: pytest-patterns
description: >-
  Python backend testing patterns with pytest for FastAPI applications. Use when writing
  Python tests: unit tests for services and repositories, integration tests for API
  endpoints with httpx.AsyncClient, fixture creation, factory setup with factory_boy,
  async testing with pytest-asyncio, mocking strategies, and parametrized tests.
  Covers test organization (tests/unit, tests/integration), conftest hierarchy, and
  coverage requirements. Does NOT cover frontend tests (use react-testing-patterns)
  or E2E browser tests (use e2e-testing).
license: MIT
compatibility: 'Python 3.12+, pytest 8+, pytest-asyncio, httpx, factory_boy'
metadata:
  author: platform-team
  version: '1.0.0'
  sdlc-phase: testing
allowed-tools: Read Edit Write Bash(pytest:*) Bash(python:*)
context: fork
---

# Pytest Patterns

## When to Use

Activate this skill when:
- Writing unit tests for service or repository classes
- Writing integration tests for FastAPI endpoints with httpx.AsyncClient
- Creating or refactoring pytest fixtures and conftest files
- Setting up factory_boy factories for test data
- Testing async code with pytest-asyncio
- Mocking external services (HTTP APIs, email, queues)
- Adding parametrized tests for input variations
- Auditing or improving test coverage

Do NOT use this skill for:
- Frontend React component or hook tests (use `react-testing-patterns`)
- E2E browser tests with Playwright (use `e2e-testing`)
- TDD red-green-refactor workflow enforcement (use `tdd-workflow`)
- Writing application code (use `python-backend-expert`)

## Instructions

### Test Organization

```
tests/
├── conftest.py              # Root conftest: DB session, async client, auth helpers
├── unit/
│   ├── conftest.py          # Unit-specific fixtures (mocked repos, services)
│   ├── services/
│   │   ├── test_user_service.py
│   │   └── test_order_service.py
│   └── repositories/
│       └── test_user_repository.py
├── integration/
│   ├── conftest.py          # Integration-specific fixtures (test DB, seeding)
│   ├── test_users_api.py
│   └── test_orders_api.py
└── factories/
    ├── __init__.py
    ├── user_factory.py
    └── order_factory.py
```

**Naming conventions:**
- Test files: `test_<module>.py`
- Test classes: `Test<Feature>` (group related tests, no `__init__`)
- Test functions: `test_<action>_<expected_outcome>` or `test_<scenario>`
- Fixtures: descriptive noun (`db_session`, `authenticated_client`, `sample_user`)

**Marker conventions:**
```python
# pyproject.toml
[tool.pytest.ini_options]
markers = [
    "unit: Unit tests (no DB, no network)",
    "integration: Integration tests (real DB, real HTTP)",
    "slow: Tests that take > 1 second",
]
asyncio_mode = "auto"
```

Run subsets: `pytest -m unit`, `pytest -m integration`, `pytest -m "not slow"`.

### Fixture Architecture
````
END THIRD-PARTY FILE 00a02b66e8500c2c

The third-party file has ended. Everything between the 00a02b66e8500c2c tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2976 of 15414 bytes. © platform-team · MIT · https://github.com/hieutrtr/ai1-skills/tree/HEAD/skills/pytest-patterns/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
