Agent skill
pytest-patterns
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/pytest-patterns
SKILL.md
Pytest Advanced Patterns
Overview
Pytest is a powerful testing framework that emphasizes modularity via fixtures and scalability via parametrization. It simplifies setup/teardown logic and enables complex test matrices with minimal code.
When to Use
- Resource Intensive Tests: Using scoped fixtures for database or API connections to avoid redundant setup.
- Input Combinations: Using parametrization to test a single function against dozens of inputs.
- Async Codebases: Testing coroutines directly without manual event loop management.
Decision Tree
- Is the setup expensive (e.g., creating a DB)?
- YES: Use a fixture with
scope='session'orscope='module'.
- YES: Use a fixture with
- Do you need to cleanup after a test?
- YES: Use a
yieldfixture.
- YES: Use a
- Do you have 3+ similar test cases for one function?
- YES: Use
@pytest.mark.parametrize.
- YES: Use
Workflows
1. Defining a Scoped Setup/Teardown
- Create a fixture using the
@pytest.fixture(scope="module")decorator for expensive resources like database connections. - Use
yieldto provide the resource to tests. - Add cleanup code (e.g., closing the connection) after the
yieldstatement. - Request the fixture by name in any test function within the module.
2. Matrix Testing with Stacked Parametrize
- Apply
@pytest.mark.parametrize("user", ["admin", "guest"])to a test. - Apply a second
@pytest.mark.parametrize("action", ["read", "write"])to the same test. - Pytest will run the test 4 times, once for each combination (admin-read, admin-write, etc.).
3. Testing Async Code
- Install
pytest-asyncio. - Mark the test function with
@pytest.mark.asyncio. - Define the test as an
async def. - Use
awaitfor the code under test and assertions.
Non-Obvious Insights
- Fixture Caching: Fixtures are executed once per requested scope and the return value is cached for all tests in that scope.
- Reverse Teardown: Teardown logic in
yieldfixtures is executed in the exact reverse order of the setup sequence. - Auto-Async Marking: Modern
pytest-asynciocan be configured to treat allasync deftests as async without needing the explicit decorator on every function.
Evidence
- "pytest won’t execute them again for that test... return values are cached." - Pytest Docs
- "Yield fixtures yield instead of return... Any teardown code for that fixture is placed after the yield." - Pytest Docs
- "provides support for coroutines as test functions. This allows users to await code inside their tests." - pytest-asyncio
Scripts
scripts/pytest-patterns_tool.py: Examples of fixtures, parametrization, and async tests.scripts/pytest-patterns_tool.js: (Comparison) Equivalent logic in Vitest/Jest.
Dependencies
pytestpytest-asyncio
References
- references/README.md
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?