技能 python-testing-patterns
📦

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.

支援: Claude Codex Code(CC)
🥉 75 青銅
1

下載技能 ZIP

2

在 Claude 中上傳

前往 設定 → 功能 → 技能 → 上傳技能

3

開啟並開始使用

測試它

正在使用「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

安全審計

安全
v1 • 2/24/2026

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.

2
已掃描檔案
947
分析行數
3
發現項
1
審計總數

偵測到的模式

External Commands DocumentationNetwork Mocking ExamplesTest Fixture Configurations
審計者: claude

品質評分

38
架構
100
可維護性
87
內容
50
社群
100
安全
100
規範符合性

你能建構什麼

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

試試這些提示

Basic Test Creation
Create pytest tests for a function called [FUNCTION_NAME] that [DESCRIPTION]. Use the AAA pattern.
Fixture Setup
Create pytest fixtures with [SCOPE] scope to provide [RESOURCE] for tests. Include proper setup and teardown.
Mocking External Calls
Write tests that mock [EXTERNAL_SERVICE] calls using unittest.mock. Show both patch decorator and context manager approaches.
Parameterized Testing
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

常見問題

What is the difference between unit tests and integration tests?
Unit tests test individual functions or classes in isolation. Integration tests test how multiple components work together, such as testing API endpoints with a real database.
How do I mock a function that does not use requests library?
Use unittest.mock.patch decorator or context manager to replace any function or class with a Mock object. Import the function path as a string: @patch('module.function_name').
What is fixture scope and when should I use each?
Function scope (default) creates fixture for each test. Class scope once per test class. Module scope once per module. Session scope once per entire test session. Use session/module for expensive resources.
How do I test exceptions with pytest?
Use pytest.raises() as a context manager: with pytest.raises(ExpectedException): your_function(). Optionally pass match parameter to check exception message.
What is test-driven development (TDD)?
TDD is a development approach where you write tests before code. Cycle: Write failing test (red), write minimal code to pass (green), refactor code (refactor). Helps ensure testable design.
How do I run only tests matching a keyword?
Use pytest -k flag: pytest -k 'test_name_pattern' runs only tests with names matching the pattern. Use pytest -m marker to run tests with specific markers.