pytest-skill:讓 AI 助手按生產習慣寫 pytest

前言

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

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

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

小夜