home / skills / partme-ai / full-stack-skills / pytest

pytest skill

/skills/pytest

This skill helps you write and configure pytest tests efficiently, with fixtures, parametrization, mocking, and plugin usage.

npx playbooks add skill partme-ai/full-stack-skills --skill pytest

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
670 B
---
name: pytest
description: Provides comprehensive guidance for pytest testing framework including test writing, fixtures, parametrization, mocking, and plugins. Use when the user asks about pytest, needs to write Python tests, use pytest fixtures, or configure pytest for Python projects.
license: Complete terms in LICENSE.txt
---

## When to use this skill

Use this skill whenever the user wants to:
- [待完善:根据具体工具添加使用场景]

## How to use this skill

[待完善:根据具体工具添加使用指南]

## Best Practices

[待完善:根据具体工具添加最佳实践]

## Keywords

[待完善:根据具体工具添加关键词]

Overview

This skill provides practical, hands-on guidance for using the pytest testing framework in Python projects. It covers test structure, fixtures, parametrization, mocking, plugins, and configuration. The goal is to help you write reliable, maintainable tests and integrate pytest into CI pipelines quickly.

How this skill works

The skill explains what to test, how to organize test files and functions, and how to use fixtures for setup and teardown. It shows parametrization patterns to cover multiple cases, demonstrates mocking techniques for isolating units, and recommends useful plugins and config settings. It also includes examples and troubleshooting tips for common pytest errors.

When to use it

  • You want to write unit, integration, or functional tests for Python code.
  • You need reusable setup/teardown using pytest fixtures.
  • You want to run the same test against multiple inputs with parametrization.
  • You need to mock dependencies or external services in tests.
  • You are configuring pytest in CI or need plugin recommendations.

Best practices

  • Name test files and functions clearly (test_*.py, def test_*).
  • Keep tests small and focused on one behavior per test.
  • Use fixtures for reusable setup and keep fixture scope appropriate (function/module/session).
  • Prefer parametrization over loops inside tests for clearer reports.
  • Mock only external side effects; prefer dependency injection for easier testing.
  • Enable strict assertions and use pytest.raises for exception checks.

Example use cases

  • Create unit tests for a data-processing function with multiple edge cases using @pytest.mark.parametrize.
  • Use fixtures to create a temporary database state and tear it down automatically between tests.
  • Mock HTTP requests with monkeypatch or requests-mock to test API client behavior without hitting the network.
  • Write integration tests that run against a Dockerized service using pytest and a fixture that manages container lifecycle.
  • Configure pytest.ini to set markers, test paths, and minimum logging for consistent CI runs.

FAQ

How do I share fixtures across multiple test files?

Declare fixtures in conftest.py at the package root. pytest will discover them and make them available to any test under that root.

When should I use monkeypatch vs unittest.mock?

Use monkeypatch for simple, targeted attribute or environment changes; use unittest.mock for more complex patching, call assertions, or when you need Mock objects with rich behavior.