pytest-skill: Enabling AI Assistants to Write pytest Following Production Practices

前言

Python 后端和 AI 工程里,pytest 几乎是默认的单元测试栈。实际写测试时,常见问题不是「不会 import pytest」,而是 fixture 生命周期、conftest.py 共享、parametrize 与 marker 的用法,以及 mock 该 patch 哪条路径。把这些交给通用对话,助手容易写出能跑但不像团队习惯的代码:滥用 unittest 风格断言、全局状态、或缺少 teardown。

下面介绍 TestMu AI(原 LambdaTest)在 agent-skills 仓库中提供的 pytest-skill:一份面向 AI 编码助手的 Skill,用来生成带 fixtures、parametrize、markers、mock 与 conftest 模式的 pytest 测试。它与 Python 后端、AI 工程的主流测试栈对齐,在同类 Agent Skill 里覆盖的开发者面较广。

这是什么

pytest-skill 是 category 为 unit-testing、语言为 Python 的 Agent Skill,MIT 许可,metadata 标注作者为 TestMu AI、版本 1.0。

官方描述概括为:生成生产级(production-grade)pytest 测试,涵盖 fixtures、parametrize、markers、mocking 与 conftest 模式。建议在用户提到 pytest、conftest、@pytest.fixture、@pytest.mark、Python test 等场景时使用;触发词包括:pytest、conftest、Python test、parametrize、Python unit test。

Skill 本体位于仓库路径 pytest-skill;更深层的生产模式说明在同目录下的 reference/playbook.md(资料中列出配置、fixtures、parametrize、mocking、async、异常、markers、CI/CD、排错与 checklist 等章节)。

核心功能

Skill 文档把 pytest 写法整理成可直接参照的模式,并附带反模式对照与命令速查。

基础测试与断言

支持函数式测试、类分组测试,以及 pytest.raises 校验异常。断言侧强调原生 assert(配合 pytest 断言重写)、pytest.approx 浮点比较、以及 pytest.raises 的 match 与 exc_info。

Fixtures 与 conftest

文档示例包括:普通 fixture、yield 后 teardown(数据库 rollback/close)、scope="module"、以及在 conftest.py 里用 autouse=True 做共享重置。反模式表明确建议:不要用 self.assertEqual(),而用 assert;不要在 __init__ 里做 setup,而用 @pytest.fixture;避免裸全局状态,而用带 yield 的 fixture 做清理。

Parametrize 与 Markers

@pytest.mark.parametrize 用于多组输入输出;markers 示例包含 slow、skip、skipif、xfail 等常见标记。

Mocking

示例覆盖 unittest.mock 的 patch/MagicMock,以及 pytest-mock 的 mocker.patch、对 HTTP 响应的 mock 等。

项目配置与运行

pyproject.toml 片段展示 [tool.pytest.ini_options]:testpaths、markers 注册、addopts 等。

Quick Reference 汇总常用命令,例如:

任务 命令
跑全部 pytest
指定文件 pytest tests/test_login.py
指定用例 pytest tests/test_login.py::test_login_success
按 marker pytest -m slow
按关键字 pytest -k "login and not invalid"
覆盖率 pytest --cov=myapp --cov-report=html
并行 pytest -n auto(需 pytest-xdist)

reference/playbook.md 进一步覆盖 pytest.ini / pyproject 配置、async(pytest-asyncio)、CI(如 GitHub Actions matrix)、以及调试与生产 checklist 等,适合需要「一整套工程习惯」而不仅是单文件示例时使用。

安装与启用

pytest-skill 随 LambdaTest/agent-skills 发布。父仓库说明:通过 Node.js / npm 运行安装器 npx agentskillsforall,并配合 TestMu AI 账号凭证(LT_USERNAME、LT_ACCESS_KEY 环境变量)。完整步骤见 TestMu AI Agent Skills 文档。

安装单个 Skill 的示例如下(父仓库 README 以 selenium、playwright、cypress 为例;pytest-skill 使用同一机制,将 --skill 换为 pytest-skill):

npx agentskillsforall add https://github.com/LambdaTest/agent-skills.git --skill pytest-skill

浏览仓库内全部 Skill:

npx agentskillsforall list https://github.com/LambdaTest/agent-skills.git

凭证示例(Linux / macOS):

export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR_ACCESS_KEY"

父仓库写明支持 Claude Code、GitHub Copilot、Cursor、Gemini CLI 等 AI 编码助手;各 IDE/CLI 在 Skill 目录挂载方式可能不同,以 TestMu AI 官方文档为准。启用后,在对话里自然提及 pytest、conftest、parametrize 等,即可对齐 Skill 的触发条件。

典型用法

安装并配置凭证后,可直接用自然语言描述测试需求,例如:为某个 Python 模块补充带 fixture 的单元测试、为 API 层写 mock 掉 requests.get 的用例、或在 conftest.py 里加 autouse 的状态重置。Skill 内嵌的模式会约束生成结果更接近文档中的「Good」列。

本地若只需对照模式手写或审阅 AI 输出,也可直接打开 Skill 仓库中的示例片段。例如 parametrize:

@pytest.mark.parametrize("input,expected", [
    ("hello", 5), ("", 0), ("pytest", 6),
])
def test_string_length(input, expected):
    assert len(input) == expected

fixture 与 teardown:

@pytest.fixture
def db_connection():
    conn = Database.connect("test_db")
    yield conn
    conn.rollback()
    conn.close()

项目级 pytest 配置:

[tool.pytest.ini_options]
testpaths = ["tests"]
markers = ["slow: slow tests", "integration: integration tests"]
addopts = "-v --tb=short"

运行侧仍使用本机已安装的 pytest;并行与覆盖率命令见上文 Quick Reference。

适用场景与注意

适合谁:维护 Python 服务、库或 AI 应用代码、希望助手生成的测试默认符合 pytest 惯例(fixture 作用域、conftest、marker 注册、mock 路径)的团队或个人。

限制与注意:

  1. Skill 指导的是测试代码形态与模式,不替代你在仓库里安装 pytest 及插件(如 pytest-mock、pytest-xdist、pytest-asyncio)。
  2. 通过 agentskillsforall 安装流程与 TestMu AI 账号绑定,属于 TestMu AI Agent Skills 生态的一部分;若仅需要静态参考,可直接阅读 GitHub 上的 SKILL.md 与 reference/playbook.md。
  3. Mock 示例中的模块路径(如 myapp.email.smtplib)需与你项目包结构一致,生成后仍要按实际 import 路径调整。
  4. 资料未单独列出 pytest-skill 与云测执行能力的绑定;E2E 或云端执行其他框架时,父仓库另有 selenium-skill、playwright-skill 等,勿与单元测试 Skill 混为一谈。

结尾

pytest-skill 把 pytest 的核心写法、反模式、命令速查和更深层的 playbook 收进一份 Agent Skill,让 Cursor 等助手在「写 Python 测试」时有统一参照。若你已在用 pytest 做后端或 AI 工程的质量门禁,可以把它当作生成与评审测试代码的默认约束。

官方目录:https://github.com/LambdaTest/agent-skills/tree/main/pytest-skill

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye