Write and run powerful Python tests with pytest - the testing framework that makes it easy to write small tests that scale to complex functional testing
A skill for writing and running tests with pytest, Python's most popular testing framework. pytest makes it easy to write small, readable tests and scales to support complex functional testing for applications and libraries.
This skill helps you leverage pytest's powerful testing capabilities including auto-discovery of tests, detailed assertion introspection, modular fixtures, and rich plugin ecosystem. It guides the AI to write idiomatic pytest tests that follow best practices and use pytest's full feature set.
When helping users with pytest testing, follow these guidelines:
1. **Writing Tests**
- Use simple `assert` statements instead of unittest-style assertions
- Name test files with `test_*.py` or `*_test.py` pattern
- Name test functions with `test_*` prefix
- Keep tests focused and atomic - one concept per test
- Use descriptive test names that explain what is being tested
2. **Using Fixtures**
- Create fixtures for shared test resources using `@pytest.fixture`
- Leverage pytest's fixture dependency injection by adding fixtures as function parameters
- Use fixture scopes (`function`, `class`, `module`, `session`) appropriately
- Consider using `conftest.py` for shared fixtures across multiple test files
3. **Assertions and Output**
- Use plain assert statements - pytest provides detailed introspection automatically
- Add custom error messages when needed: `assert x == y, "custom message"`
- Leverage pytest's assertion rewriting for detailed failure output
4. **Parametrization**
- Use `@pytest.mark.parametrize` to run the same test with different inputs
- Parametrize fixtures for reusable test data configurations
- Use `pytest.param()` to add IDs or marks to specific parameter sets
5. **Test Organization**
- Group related tests in classes (optional, but useful for organization)
- Use markers (`@pytest.mark.slow`, `@pytest.mark.integration`) to categorize tests
- Organize test files to mirror the structure of the source code
6. **Running Tests**
- Guide users on running: `pytest` (all tests), `pytest test_file.py` (specific file), `pytest -k "test_name"` (pattern matching)
- Use `-v` for verbose output, `-s` to show print statements
- Use `-x` to stop on first failure, `--lf` to run last failed
7. **Advanced Features**
- Use `pytest.raises()` context manager for testing exceptions
- Use `pytest.warns()` for testing warnings
- Use `pytest.approx()` for floating-point comparisons
- Leverage `monkeypatch` fixture for mocking
- Use `tmp_path` and `tmp_path_factory` for temporary directories
8. **Common Patterns**
- Setup/teardown via fixtures with yield
- Use `autouse=True` for fixtures that should always run
- Combine fixtures to build complex test scenarios
- Use `request` fixture to access test context
**Basic Test:**
```python
def add(a, b):
return a + b
def test_add_positive_numbers():
assert add(2, 3) == 5
def test_add_negative_numbers():
assert add(-1, -1) == -2
```
**Using Fixtures:**
```python
import pytest
@pytest.fixture
def sample_data():
return {"name": "test", "value": 42}
def test_data_access(sample_data):
assert sample_data["name"] == "test"
assert sample_data["value"] == 42
```
**Parametrized Tests:**
```python
@pytest.mark.parametrize("input,expected", [
(2, 4),
(3, 9),
(4, 16),
])
def test_square(input, expected):
assert input ** 2 == expected
```
**Testing Exceptions:**
```python
def test_division_by_zero():
with pytest.raises(ZeroDivisionError):
1 / 0
```
```bash
pip install pytest
```
For additional plugins and features, pytest has 1300+ plugins available in the ecosystem.
Full documentation: https://docs.pytest.org/
Leave a review
No reviews yet. Be the first to review this skill!