Summary
No standard mocking framework is defined for Python skills. Without an established convention, individual skill authors will make inconsistent choices across unittest.mock, monkeypatch, and third-party libraries. Standardizing on pytest-mock provides consistent patterns, automatic cleanup via fixtures, and alignment with the existing pytest test framework choice.
Context
The repository has established conventions for test frameworks (Pester for PowerShell, pytest for Python) and linting tools (PSScriptAnalyzer for PowerShell, ruff for Python). The mocking framework for Python is the remaining gap.
pytest-mock wraps unittest.mock with a mocker fixture that integrates naturally with pytest's fixture lifecycle. It provides automatic cleanup (no manual patch.stopall()), concise syntax (mocker.patch() instead of decorators or context managers), and autospec support for catching API mismatches at test time.
Recommended Convention
Include pytest-mock as a standard dev dependency in every Python skill's pyproject.toml:
[dependency-groups]
dev = ["pytest>=8.0", "ruff>=0.4", "pytest-mock>=3.14"]
Usage guidance:
| Scenario |
Tool |
Example |
| Call assertions, spies, return value stubs |
mocker (pytest-mock) |
mock_api = mocker.patch("skill.api.client.send") |
| Simple attribute or env var patching |
monkeypatch (built-in) |
monkeypatch.setenv("API_KEY", "test") |
Direct unittest.mock usage |
Avoid |
Use mocker instead for fixture integration |
Changes Required
| File |
Change |
| Python coding standards or instructions |
Document pytest-mock as the standard mocking framework |
Existing Python skill pyproject.toml files |
Add pytest-mock>=3.14 to dev dependencies |
Acceptance Criteria
Related
Summary
No standard mocking framework is defined for Python skills. Without an established convention, individual skill authors will make inconsistent choices across
unittest.mock,monkeypatch, and third-party libraries. Standardizing onpytest-mockprovides consistent patterns, automatic cleanup via fixtures, and alignment with the existing pytest test framework choice.Context
The repository has established conventions for test frameworks (Pester for PowerShell, pytest for Python) and linting tools (PSScriptAnalyzer for PowerShell, ruff for Python). The mocking framework for Python is the remaining gap.
pytest-mockwrapsunittest.mockwith amockerfixture that integrates naturally with pytest's fixture lifecycle. It provides automatic cleanup (no manualpatch.stopall()), concise syntax (mocker.patch()instead of decorators or context managers), and autospec support for catching API mismatches at test time.Recommended Convention
Include
pytest-mockas a standard dev dependency in every Python skill'spyproject.toml:Usage guidance:
mocker(pytest-mock)mock_api = mocker.patch("skill.api.client.send")monkeypatch(built-in)monkeypatch.setenv("API_KEY", "test")unittest.mockusagemockerinstead for fixture integrationChanges Required
pyproject.tomlfilespytest-mock>=3.14to dev dependenciesAcceptance Criteria
pytest-mock>=3.14is included in dev dependencies for all Python skillsmockervsmonkeypatchmockerwhere applicableRelated