技能 python-testing-patterns
🧪

python-testing-patterns

安全 🌐 网络访问⚙️ 外部命令🔑 环境变量

快速建立可靠的 Python 測試

也可从以下获取: sickn33,ActiveInferenceInstitute

撰寫 Python 測試可能很耗時,且團隊間的實作方式不一致。本技能提供清晰的 pytest 模式和範例,可加速測試設計並提高覆蓋率。

支持: Claude Codex Code(CC)
📊 69 充足
1

下载技能 ZIP

2

在 Claude 中上传

前往 设置 → 功能 → 技能 → 上传技能

3

开启并开始使用

测试它

正在使用“python-testing-patterns”。 I need tests for a service that calls an API and caches results

预期结果:

  • Create a fixture for the service with a temporary cache
  • Mock the HTTP client and assert it is called once
  • Add a second call test to confirm cached behavior

正在使用“python-testing-patterns”。 Write a test for a division function that handles zero

预期结果:

  • Use pytest.raises to check ZeroDivisionError is raised
  • Add a test case for normal division
  • Assert the error message matches expected text

正在使用“python-testing-patterns”。 How do I test email validation with many inputs

预期结果:

  • Use @pytest.mark.parametrize with test email cases
  • Include valid and invalid examples
  • Run all cases with single test function

安全审计

安全
v4 • 1/17/2026

Pure documentation skill containing only instructional examples for Python testing patterns. No executable code, scripts, or network operations in the skill logic itself. Static findings are false positives from example code blocks in documentation.

2
已扫描文件
1,084
分析行数
3
发现项
4
审计总数
审计者: claude 查看审计历史 →

质量评分

38
架构
100
可维护性
85
内容
22
社区
100
安全
83
规范符合性

你能构建什么

強化單元測試

應用 pytest 模式來為核心業務邏輯建立清晰、可維護的測試。

標準化測試套件

使用 fixture、標記和參數化來建立一致的測試基礎設施。

驗證資料工具程式

為資料處理函式新增單元測試和屬性檢查。

试试这些提示

基本單元測試
使用 Arrange Act Assert 模式,為一個將兩個數字相加的函式撰寫簡單的 pytest 測試。
Fixture 設定
建立一個用於資料庫用戶端的 pytest fixture,並展示一個使用它且具有適當解構的測試。
模擬 HTTP 呼叫
提供一個模擬 requests.get 並斷言 URL 和參數的 pytest 範例。
非同步和屬性測試
展示如何測試非同步函式,並為字串工具程式新增一個 Hypothesis 屬性測試。

最佳实践

  • 保持測試隔離並清理共用狀態
  • 使用 fixture 進行可重複使用的設定和解構
  • 參數化測試以用最少的重複覆蓋邊界情況

避免

  • 在測試之間共用可變的全域狀態
  • 在單元測試中存取即時外部服務
  • 在一個測試中斷言多種行為

常见问题

Which Python versions are supported
範例針對現代 Python 和 pytest,通常是 3.9 或更新版本。
What test types are covered
專注於單元、整合、非同步和屬性測試模式。
Can it integrate with CI pipelines
它提供您可以改編用於 CI 的 pytest 和覆蓋率指令。
Does it access my data or files
不會。它僅提供指導和範例程式碼。
How do I get help with failing tests
分享失敗的輸出和最少的程式碼片段以獲得針對性的建議。
How does it compare to generic advice
它提供具體的 pytest 模式、fixture 和模擬範例。