python-testing-patterns

Implement pytest test suites with fixtures, mocking, parameterization, and coverage reporting.

3|1|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/PALabs-v1/AI_friend --skill python-testing-patterns-palabs-v1
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-testing-patterns
Source: https://github.com/PALabs-v1/AI_friend/tree/main/.agents/skills/python-testing-patterns
Command: npx skills add https://github.com/PALabs-v1/AI_friend --skill python-testing-patterns-palabs-v1

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest, pytest-asyncio, pytest-cov, hypothesis, sqlalchemy, requests.

What problem does it solve? Writing reliable Python tests requires knowing pytest fixtures, mocking strategies, async testing, and coverage configuration, which developers often implement inconsistently or skip entirely. ## Core Features & Use Cases - Pytest Patterns: Provides ready-to-use patterns for fixtures, parameterized tests, exception testing, monkeypatching, and temporary file handling. - Mocking & Async Testing: Demonstrates unittest.mock usage for API clients and pytest-asyncio patterns for coroutines and concurrent operations. - CI/CD & Coverage Setup: Includes GitHub Actions workflow examples, pytest.ini/pyproject.toml configuration, and coverage thresholds. - Use Case: When building a new API client, apply the mocking pattern to test HTTP calls without network access, then add parameterized tests to cover edge cases and wire coverage reporting into CI. ## Quick Start Write pytest tests for my Python module using fixtures, mocking for external API calls, and parameterized cases for edge conditions.

Frequently Asked Questions about python-testing-patterns

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

FAQPage Schema
How do I write unit tests with pytest in Python?

Write functions prefixed with test_ that follow the Arrange-Act-Assert pattern, then run them with the pytest command. Use plain assert statements for verification and pytest.raises to check that exceptions are thrown correctly.

How to mock API requests in Python tests?

Use unittest.mock's patch to replace requests.get or requests.post with a Mock object whose return_value simulates the HTTP response. Configure json.return_value and raise_for_status on the mock, then assert the mock was called with the expected URL.

What is the difference between pytest fixture scopes?

Fixture scopes control how often a fixture is created: function scope runs per test, module scope once per file, and session scope once per test run. Use broader scopes for expensive resources like database connections or app configuration.

Does pytest support testing async functions?

Yes, with the pytest-asyncio plugin. Mark coroutine tests with @pytest.mark.asyncio and await the async code directly inside the test. Async fixtures are also supported for setting up resources like clients.

How do I measure test coverage with pytest?

Install pytest-cov and run pytest with --cov=yourpackage to measure coverage. Add --cov-report=term-missing to see uncovered lines, or --cov-fail-under=80 to enforce a minimum coverage threshold in CI.

When should I use property-based testing instead of example-based tests?

Use hypothesis for property-based testing when you want to verify invariants across many generated inputs, such as reversing a string twice returning the original. It complements example-based tests by exploring edge cases you did not anticipate.