python-testing-patterns

Design pytest-based unit, integration, and functional tests with fixtures and mocking for Python codebases.

1|Updated Aug 31, 2024
One-click install
npx skills add https://github.com/aRustyDev/dotfiles --skill python-testing-patterns-arustydev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-testing-patterns
Source: https://github.com/aRustyDev/dotfiles/tree/main/.ai/plugins/python-development/skills/python-testing-patterns
Command: npx skills add https://github.com/aRustyDev/dotfiles --skill python-testing-patterns-arustydev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest, hypothesis, requests.

What problem does it solve?

This Skill provides a comprehensive guide to implementing robust testing strategies in Python, covering unit, integration, and functional tests using pytest, fixtures, mocking, and TDD.

Core Features & Use Cases

  • pytest Fundamentals: Learn basic test writing, fixtures for setup/teardown, and parameterized tests.
  • Mocking External Dependencies: Use unittest.mock to isolate code and simulate external services.
  • Async & Exception Testing: Patterns for testing asynchronous Python code and verifying exception handling.
  • Advanced Techniques: Explores monkeypatching, temporary files, custom fixtures, and property-based testing.
  • Use Case: Set up a new test suite for a Python application, including unit tests for individual functions, integration tests for API interactions using mocks, and parameterized tests for various input scenarios.

Quick Start

Generate a basic pytest test function to verify that an add(a, b) function correctly returns the sum of two numbers.

Frequently Asked Questions about python-testing-patterns

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

FAQPage Schema
How do I set up a pytest test suite for a Python project?

pytest is a framework for writing and running Python tests. Start by installing pytest, then create test files with functions prefixed by `test_`, use fixtures for setup/teardown, and run `pytest` to execute your suite. Fixtures provide reusable test data and configuration, while the AAA pattern (Arrange, Act, Assert) structures individual tests for clarity and maintainability.

What's the best way to mock external dependencies in Python tests?

Use `unittest.mock` to isolate units under test by simulating external services and APIs. Mocking replaces real objects with test doubles, letting you verify interactions and handle failure scenarios without calling live endpoints. This ensures tests run fast, stay deterministic, and don't depend on external systems.

Can I test asynchronous Python code with pytest?

Yes. pytest supports async test functions and fixtures through plugins and async/await syntax. You can write coroutine-based tests that properly handle async setup, teardown, and execution, enabling you to test concurrent code and async libraries without blocking or race conditions.

How do I use parameterized tests to cover multiple input scenarios?

Parameterized tests run the same test logic across different input sets using `pytest.mark.parametrize`. This reduces duplication, improves coverage of edge cases, and makes it easy to add new scenarios without writing separate test functions.

What's the difference between unit, integration, and functional tests?

Unit tests verify individual functions in isolation using mocks. Integration tests check how components work together with real or partially mocked dependencies. Functional tests validate end-to-end workflows against actual system behavior. pytest supports all three patterns within a single framework.

How do I measure and track test coverage in Python?

Test coverage identifies which lines of code are executed by tests, measured as a percentage. Use coverage tools with pytest to generate reports showing untested code paths. High coverage reduces bugs, but meaningful coverage requires testing actual behavior, not just code execution.