python-testing-patterns

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

Updated Mar 3, 2026
One-click install
npx skills add https://github.com/Devil-2621/gsr-research-model --skill python-testing-patterns-devil-2621
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-testing-patterns
Source: https://github.com/Devil-2621/gsr-research-model/tree/main/.cursor/skills/python-testing-patterns
Command: npx skills add https://github.com/Devil-2621/gsr-research-model --skill python-testing-patterns-devil-2621

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable Python tests requires knowing many pytest patterns—fixtures, mocking, parameterization, async testing, and coverage setup—and developers often struggle to structure test suites correctly or handle edge cases like external APIs, databases, and time-dependent code. ## Core Features & Use Cases - Pytest Patterns: Provides ready-to-use templates for unit tests, fixtures with setup/teardown, parameterized tests, and exception testing. - Mocking & Isolation: Covers unittest.mock, monkeypatch, temporary directories, and freezegun for testing code with external dependencies, environment variables, and time-based logic. - Advanced Testing: Includes async test patterns with pytest-asyncio, property-based testing with hypothesis, database testing with SQLAlchemy, and CI/CD integration with coverage thresholds. - Use Case: When building a new API client, use this Skill to generate a complete test suite that mocks HTTP requests, tests retry logic, validates error handling, and enforces 80% coverage in CI. ## Quick Start Write a pytest test suite for my Python module with fixtures, mocked external API calls, and parameterized edge case tests.

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 test functions prefixed with test_ that follow the Arrange-Act-Assert pattern, then run them with the pytest command. Use assert statements directly and pytest.raises to verify exceptions are raised with expected messages.

How to mock API requests in Python tests?

Use unittest.mock's patch to replace requests.get or requests.post with Mock objects that return controlled responses. Configure mock_response.json.return_value and verify calls with assert_called_once_with to test API clients without network access.

What is the difference between pytest fixtures and mocking?

Fixtures provide setup and teardown for test resources like database connections using yield, while mocking replaces external dependencies with controlled fakes. Fixtures manage real test infrastructure; mocks simulate behavior of external services.

Does pytest support testing async functions?

Yes, pytest supports async tests through the pytest-asyncio plugin using the @pytest.mark.asyncio decorator on async test functions. Async fixtures can also be defined with yield for setup and teardown of asynchronous resources.

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 and --cov-fail-under=80 to enforce a minimum coverage threshold in CI pipelines.

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

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