python-testing

Enforces mandatory pytest conventions including AAA pattern, naming rules, and mock verification.

138|5|Updated Sep 19, 2024
One-click install
npx skills add https://github.com/macalbert/envilder --skill python-testing-macalbert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-testing
Source: https://github.com/macalbert/envilder/tree/main/.github/skills/python-testing
Command: npx skills add https://github.com/macalbert/envilder --skill python-testing-macalbert

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest, pytest-asyncio, polyfactory.

What problem does it solve? Python test suites often become inconsistent and unmaintainable when each developer follows different conventions for structure, naming, and assertions. This Skill enforces a single mandatory testing standard so every test follows the same AAA pattern, naming scheme, and verification rules. ## Core Features & Use Cases - AAA Pattern Enforcement: Every test must separate Arrange, Act, and Assert phases with explicit comments, with no combined Act & Assert blocks or try/finally cleanup. - Strict Naming and Structure: Tests must use the Should_{ExpectedBehavior}When{Condition} format, mirror the production code hierarchy, and use sut/actual/expected variable names. - Mocking and Test Data Rules: All mocks must be verified, and test data should be created with Mother or Builder patterns (polyfactory-based), with async support via pytest-asyncio. - Use Case: When writing unit, integration, or acceptance tests for a Python service, activate this Skill so the AI generates tests that pass code review on the first attempt. ## Quick Start Write unit tests for my Python service class following the mandatory AAA pattern and Should_When naming conventions.

Frequently Asked Questions about python-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write pytest tests using the AAA pattern?

Structure each test with explicit # Arrange, # Act, and # Assert comment markers, each appearing at most once. Never combine Act and Assert, avoid conditionals inside phases, and use fixtures with yield instead of try/finally for cleanup.

How to name pytest test functions with a custom convention?

Use the Should_{ExpectedBehavior}_When_{Condition} format in PascalCase without a test_ prefix. Configure pytest with python_functions = ["Should_*"] in pyproject.toml so pytest discovers these functions.

Does pytest support async test functions?

Yes, pytest-asyncio enables async test functions with the @pytest.mark.asyncio decorator or asyncio_mode = "auto" configuration. Use AsyncMock for async dependencies and verify calls with assert_awaited_once.

How do I test exceptions in pytest without combining act and assert?

Extract the failing call into a lambda during the Act phase, then invoke it inside pytest.raises during the Assert phase. This keeps Act and Assert separate instead of wrapping the call directly in the raises context manager.

What is the Builder pattern for test data in Python?

The Builder pattern wraps polyfactory ModelFactory classes to create type-safe test data with fluent with_* methods. Define a factory for your Pydantic model, subclass a generic Builder, then call methods like with_name("Test").build() to generate instances.

Why should mocks always be verified in unit tests?

Unverified mocks let tests pass even when the code never calls expected collaborators, hiding real defects. Always assert interactions with methods like assert_called_once, assert_called_once_with, or assert_not_called to confirm behavior.