python-testing-patterns
快速建立可靠的 Python 測試
也可从以下获取: sickn33,ActiveInferenceInstitute
撰寫 Python 測試可能很耗時,且團隊間的實作方式不一致。本技能提供清晰的 pytest 模式和範例,可加速測試設計並提高覆蓋率。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“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
安全审计
安全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.
风险因素
🌐 网络访问 (17)
⚙️ 外部命令 (37)
🔑 环境变量 (23)
质量评分
你能构建什么
強化單元測試
應用 pytest 模式來為核心業務邏輯建立清晰、可維護的測試。
標準化測試套件
使用 fixture、標記和參數化來建立一致的測試基礎設施。
驗證資料工具程式
為資料處理函式新增單元測試和屬性檢查。
试试这些提示
使用 Arrange Act Assert 模式,為一個將兩個數字相加的函式撰寫簡單的 pytest 測試。
建立一個用於資料庫用戶端的 pytest fixture,並展示一個使用它且具有適當解構的測試。
提供一個模擬 requests.get 並斷言 URL 和參數的 pytest 範例。
展示如何測試非同步函式,並為字串工具程式新增一個 Hypothesis 屬性測試。
最佳实践
- 保持測試隔離並清理共用狀態
- 使用 fixture 進行可重複使用的設定和解構
- 參數化測試以用最少的重複覆蓋邊界情況
避免
- 在測試之間共用可變的全域狀態
- 在單元測試中存取即時外部服務
- 在一個測試中斷言多種行為