Skip to content

feat(skills): Standardize pytest-mock as Python mocking framework #893

@WilliamBerryiii

Description

@WilliamBerryiii

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

Metadata

Metadata

Labels

documentationImprovements or additions to documentationtestingTest infrastructure and test files

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions