pytest

GitHub

提供 pytest 编写和组织测试的最佳实践,涵盖 Fixtures、Parametrize、插件使用及断言规范,旨在提升测试代码质量与可维护性。

.claude/skills/pytest/SKILL.md microsoft/debugpy

触发场景

编写单元测试或集成测试 配置 pytest 夹具和参数化 优化测试结构和断言

安装

npx skills add microsoft/debugpy --skill pytest -g -y
更多选项

非标准路径

npx skills add https://github.com/microsoft/debugpy/tree/main/.claude/skills/pytest -g -y

不安装直接使用

npx skills use microsoft/debugpy@pytest

指定 Agent (Claude Code)

npx skills add microsoft/debugpy --skill pytest -a claude-code -g -y

安装 repo 全部 skill

npx skills add microsoft/debugpy --all -g -y

预览 repo 内 skill

npx skills add microsoft/debugpy --list

SKILL.md

Frontmatter
{
    "name": "pytest",
    "description": "Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins."
}

Skill: pytest

Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins.

When to Use

Apply this skill when writing and organizing tests with pytest — fixtures, parametrize, markers, plugins, and test structure.

Test Organization

  • Place tests in a tests/ directory mirroring the source structure.
  • Name test files test_<module>.py and test functions test_<behavior>().
  • Group related tests in classes only when they share fixtures/setup.

Fixtures

  • Define fixtures at the narrowest scope needed (function > class > module > session).
  • Use conftest.py for shared fixtures; put it at the appropriate directory level.
  • Prefer factory fixtures over complex fixture inheritance.
  • Use yield fixtures for setup/teardown; prefer tmp_path over tempfile.

Parametrize

  • Use @pytest.mark.parametrize for data-driven tests with multiple inputs.
  • Give test IDs (ids=...) for readable test output.
  • Combine parametrize with fixtures for cross-product testing.

Assertions

  • Use plain assert statements — pytest rewrites them for clear failure messages.
  • Use pytest.raises(ExceptionType, match=...) for exception testing.
  • Use pytest.approx() for floating-point comparisons.

Plugins

  • Common plugins: pytest-cov, pytest-mock, pytest-asyncio, pytest-xdist, pytest-timeout.
  • Use pytest-mock's mocker fixture over raw unittest.mock.patch.

Pitfalls

  • Don't use session-scoped fixtures for mutable state.
  • Don't assert on implementation details — test observable behavior.
  • Avoid test interdependence; each test should be runnable in isolation.

版本历史

  • e5743d3 当前 2026-08-20 17:01

同 Skill 集合

.claude/skills/click/SKILL.md
.claude/skills/django/SKILL.md
.claude/skills/flask/SKILL.md
.claude/skills/jinja2/SKILL.md
.claude/skills/numpy/SKILL.md
.claude/skills/requests/SKILL.md

元信息

文件数
0
版本
e5743d3
Hash
2946c883
收录时间
2026-08-20 17:01

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-29 03:16
浙ICP备14020137号-1 $访客地图$