python-testing-patterns
Write Better Python Tests
也可從以下取得: wshobson,ActiveInferenceInstitute
Writing maintainable tests is challenging. This skill provides comprehensive pytest patterns including fixtures, mocking, parameterization, and TDD workflows to help you build reliable test suites.
下載技能 ZIP
在 Claude 中上傳
前往 設定 → 功能 → 技能 → 上傳技能
開啟並開始使用
測試它
正在使用「python-testing-patterns」。 Write tests for a calculate_discount function that takes price and discount_percentage, returns discounted price
預期結果:
Basic test file with multiple test cases covering normal discounts, zero discount, and invalid inputs
正在使用「python-testing-patterns」。 Create fixtures for testing a database connection with session scope
預期結果:
Fixture setup with proper yield/teardown pattern for database resource management
正在使用「python-testing-patterns」。 Mock HTTP requests to an API client that fetches user data
預期結果:
Tests using patch() to mock requests.get with different response scenarios
安全審計
安全All 87 static findings are false positives. The detected patterns (external_commands, network, env_access) are documentation examples of pytest commands, HTTP mocking patterns, and test fixture configurations - all legitimate testing education content. No actual security threats present.
風險因素
⚙️ 外部命令 (1)
🌐 網路存取 (1)
🔑 環境變數 (1)
偵測到的模式
品質評分
你能建構什麼
New Python Project Testing Setup
Set up a complete test suite for a new Python project with fixtures, conftest.py configuration, and CI/CD integration
Legacy Code Test Coverage
Add tests to existing untested code using mocking to isolate units and parameterization to cover edge cases
API Integration Testing
Write integration tests for REST APIs using fixtures for test data and mocking for external service calls
試試這些提示
Create pytest tests for a function called [FUNCTION_NAME] that [DESCRIPTION]. Use the AAA pattern.
Create pytest fixtures with [SCOPE] scope to provide [RESOURCE] for tests. Include proper setup and teardown.
Write tests that mock [EXTERNAL_SERVICE] calls using unittest.mock. Show both patch decorator and context manager approaches.
Create parameterized tests for [FUNCTION] using pytest.mark.parametrize to test [EDGE_CASES].
最佳實務
- Use descriptive test names that explain what is being tested and expected behavior
- Keep tests independent with proper setup/teardown - no shared state between tests
- Follow AAA pattern: Arrange (setup), Act (execute), Assert (verify) clearly separated
避免
- Avoid test names like test_1 or test_function that do not describe the test purpose
- Do not mix multiple assertions in one test when they could be separate tests
- Avoid testing implementation details instead of behavior - test what the code does, not how