Skip to content
Skills Library
Programming · Testing

python-testing-patterns

/python-testing-patterns

Python testing patterns and best practices using pytest, mocking, and property-based testing. Use when writing unit tests, integration tests, or implementing test-driven development in Python projects.

Licence
MIT
Last repository change
28.06.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
13
Scripts
0
HTML comments
0
URLs in SKILL.md
3
Deprecation

No signals detected. Repository changed on 28.06.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Python testing patterns and best practices using pytest, mocking, and property-based testing. Use when writing unit tests, integration tests, or implementing test-driven development in Python projects.

Original author description

Python testing patterns and best practices using pytest, mocking, and property-based testing. Use when writing unit tests, integration tests, or implementing test-driven development in Python projects.

How it works

  1. When to Use This Skill
  2. Core Concepts
  3. Quick Reference
  4. Workflow
  5. Common Patterns
  6. Common Mistakes
  7. Resources
Original SKILL.md headings · 203 lines, 670 words
SKILL.md preview
---
name: python-testing-patterns
description: Python testing patterns and best practices using pytest, mocking, and property-based testing. Use when writing unit tests, integration tests, or implementing test-driven development in Python projects.
keywords:
  - TDD
  - hypothesis testing
  - integration test
  - mocking
  - parametrize
  - pytest
  - test coverage
  - test fixture
  - test-driven development
  - unit test
file_patterns:
  - '**/*_test.py'
  - '**/test_*.py'
  - '**/tests/**/*.py'
confidence: 0.85
---

# Python Testing Patterns

Comprehensive guide to implementing robust testing strategies in Python using pytest, fixtures, mocking, parameterization, and property-based testing.

## When to Use This Skill

- Writing unit tests for Python functions and classes
- Setting up comprehensive test suites and infrastructure
- Implementing test-driven development (TDD) workflows
- Creating integration tests for APIs, databases, and services
- Mocking external dependencies and third-party services
- Testing async code and concurrent operations
- Implementing property-based testing with Hypothesis
- Setting up CI/CD test automation
- Debugging failing tests and improving test coverage

## Core Concepts

**Test Discovery**: Files matching `test_*.py` or `*_test.py`, functions starting with `test_`

**Fixtures**: Reusable test resources with setup and teardown
- Scopes: `function` (default), `class`, `module`, `session`
- Composition: Build complex fixtures from simple ones
- Share via `conftest.py` for project-wide availability

**Assertions**: Use `assert` statements, `pytest.raises()` for exceptions

**Organization**: Separate `unit/`, `integration/`, `e2e/` directories

## Quick Reference

Load detailed references for specific topics:

| Task | Reference File |
|------|----------------|
| Pytest basics, test structure, AAA pattern | `skills/python-testing-patterns/references/pytest-fundamentals.md` |
| Fixtures, scopes, setup/teardown, conftest.py | `skills/python-testing-patterns/references/fixtures.md` |
| Parametrization, multiple test cases | `skills/python-testing-patterns/references/parametrized-tests.md` |
| Mocking, patching, unittest.mock, pytest-mock | `skills/python-testing-patterns/references/mocking.md` |
| Async tests, pytest-asyncio, event loops | `skills/python-testing-patterns/references/async-testing.md` |
| Property-based testing, Hypothesis, strategies | `skills/python-testing-patterns/references/property-based-testing.md` |
| Monkeypatch, environment variables, attributes | `skills/python-testing-patterns/references/monkeypatch.md` |
| Test structure, markers, conftest.py patterns | `skills/python-testing-patterns/references/test-organization.md` |
| Coverage measurement, reports, thresholds | `skills/python-testing-patterns/references/coverage.md` |
| Database, API, Redis, message queue testing | `skills/python-testing-patterns/references/integration-testing.md` |
| Best practices, test quality, fixture design | `skills/python-testing-patterns/references/best-practices.md` |

## Workflow

### 1. Basic Test Setup
```python
# test_example.py
import pytest

def test_something():
    """Descriptive test name."""
    # Arrange
    expected = 5

    # Act
    result = 2 + 3

    # Assert
    assert result == expected
```

**Run tests:**
```bash
pytest                    # Run all tests
pytest -v                 # Verbose output
pytest tests/unit/        # Specific directory
pytest -k "test_user"     # Match pattern
pytest -m unit            # Run marked tests
```

### 2. Using Fixtures
```python
@pytest.fixture
def sample_data():
    """Provide test data."""
    data = {"key": "value"}
    yield data
    # Cleanup if needed

def test_with_fixture(sample_data):
    assert sample_data["key"] == "value"
```

### 3. Parametrized Tests
```python
@pytest.mark.parametrize("input,expected", [
    (2, 4),
    (3, 9),
    (4, 16),
])
def test_square(input, expected):
    assert input ** 2 == expected
```

### 4. Mocking External Dependencies
```python
from unittest.mock import patch

@patch("module.external_api_call")
def test_with_mock(mock_api):
    mock_api.return_value = {"status": "ok"}

    result = my_function()

    assert result["status"] == "ok"
    mock_api.assert_called_once()
```

### 5. Coverage Measurement
```bash
pytest --cov=src --cov-report=term-missing
pytest --cov=src --cov-report=html
pytest --cov=src --cov-fail-under=80
```

### 6. Test Configuration
**pytest.ini:**
```ini
[pytest]
testpaths = tests
python_files = test_*.py
addopts = -v --strict-markers --cov=src
markers =
    unit: Unit tests
    integration: Integration tests
    slow: Slow tests
```

## Common Patterns

**Exception testing:**
```python
with pytest.raises(ValueError, match="error message"):
    function_that_raises()
```

**Async testing:**
```python
@pytest.mark.asyncio
async def test_async_function():
    result = await async_operation()
    assert result is not None
```

**Temporary files:**
```python
def test_file_operation(tmp_path):
    test_file = tmp_path / "test.txt"
    test_file.write_text("content")
    assert test_file.read_text() == "content"
```

**Markers for test selection:**
```python
@pytest.mark.slow
@pytest.mark.integration
def test_database_operation():
    pass
```

## Common Mistakes

1. **Not using fixtures**: Repeating setup code across tests
   - Solution: Create fixtures in conftest.py

2. **Tests depending on order**: Global state pollution
   - Solution: Ensure test independence with proper fixtures

3. **Over-mocking**: Mocking internal implementation
   - Solution: Mock only external boundaries (APIs, databases)

4. **Missing edge cases**: Only testing happy path
   - Solution: Test boundary conditions, errors, and invalid inputs

5. **Slow tests**: Running full integration tests frequently
   - Solution: Separate unit/integration, use markers, optimize fixtures

6. **Ignoring coverage gaps**: Not measuring test coverage
   - Solution: Use pytest-cov and track metrics

7. **Poor test names**: Generic names like `test_1()`
   - Solution: Use descriptive names: `test_<behavior>_<condition>_<expected>`

8. **No cleanup**: Resources not released
   - Solution: Use fixtures with proper teardown (yield pattern)

## Resources

- **pytest**: https://docs.pytest.org/
- **unittest.mock**: https://docs.python.org/3/library/unittest.mock.html
- **pytest-asyncio**: Testing async code
- **pytest-cov**: Coverage reporting
- **pytest-mock**: pytest wrapper for mock
- **Hypothesis**: https://hypothesis.readthedocs.io/
- **pytest-xdist**: Parallel test execution
- **testcontainers**: Docker containers for testing
Files in this package (13)
  • SKILL.md 6 KB
  • references/async-testing.md 3 KB
  • references/best-practices.md 8 KB
  • references/coverage.md 6 KB
  • references/fixtures.md 3 KB
  • references/integration-testing.md 8 KB
  • references/mocking.md 4 KB
  • references/monkeypatch.md 5 KB
  • references/parametrized-tests.md 2 KB
  • references/property-based-testing.md 6 KB
  • references/pytest-fundamentals.md 2 KB
  • references/test-organization.md 6 KB
  • validation/rubric.yaml 2 KB

Versions

RepositoryScoreLicence
This page · NickCrew/Claude-Cortex100MIT
FrancoStino/opencode-skills-collection100MIT
rootcastleco/rei-skills100MIT
rmyndharis/antigravity-skills100MIT
wshobson/agents100MIT
jerelvelarde/chalk-skills100MIT
vladolaru/claude-code-plugins95MIT
HermeticOrmus/LibreUIUX-Claude-Code91MIT
LeoYeAI/openclaw-master-skills91MIT

Identical content appears in 1 other GitHub folders. We show one copy with the highest score.

Database snapshot: 13.11.2026 · package version c799488bc462 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100python-testing
    Python testing patterns with pytest, fixtures, TDD, mocking, async and integration tests. Use when writing or auditing a Python test suite.
  • 100python-testing
    Python testing patterns with pytest including unit tests, integration tests, fixtures, mocking, and coverage. Use when writing Python tests.
  • 100python-testing
    Python testing with pytest, coverage, fixtures, and mocking. Use when the user mentions pytest, unit tests, coverage, fixtures, mocking, or writing Python tests.
  • 100python-testing
    Write and run Python tests using pytest with fixtures, mocking, coverage, and the AAA pattern. Use when writing Python tests, creating test files, or setting up pytest configuration.
  • 100python-testing
    Python testing best practices using pytest including fixtures, parametrization, mocking, coverage analysis, async testing, and test organization. Use when writing or improving Python tests.
  • 100testing-best-practices
    Unit testing, integration testing, and test-driven development principles. Use when writing tests, reviewing test code, improving test coverage, or setting up testing strategy. Triggers on "write tests", "review tests", "testing best practices", or "TDD".

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.