Python Testing Patterns
Comprehensive testing strategies for Python applications using pytest, TDD methodology, and best practices.
When to Activate
- Writing new Python code (follow TDD: red, green, refactor)
- Designing test suites for Python projects
- Reviewing Python test coverage
- Setting up testing infrastructure
Core Testing Philosophy
Test-Driven Development (TDD)
Always follow the TDD cycle:
- RED: Write a failing test for the desired behavior
- GREEN: Write minimal code to make the test pass
- REFACTOR: Improve code while keeping tests green
python# Step 1: Write failing test (RED) def test_add_numbers(): result = add(2, 3) assert result == 5 # Step 2: Write minimal implementation (GREEN) def add(a, b): return a + b # Step 3: Refactor if needed (REFACTOR)
Coverage Requirements
- Target: 80%+ code coverage
- Critical paths: 100% coverage required
- Use
pytest --covto measure coverage
bashpytest --cov=mypackage --cov-report=term-missing --cov-report=html
pytest Fundamentals
Basic Test Structure
pythonimport pytest def test_addition(): """Test basic addition.""" assert 2 + 2 == 4 def test_string_uppercase(): """Test string uppercasing.""" text = "hello" assert text.upper() == "HELLO" def test_list_append(): """Test list append.""" items = [1, 2, 3] items.append(4) assert 4 in items assert len(items) == 4
Assertions
python# Equality assert result == expected # Inequality assert result != unexpected # Truthiness assert result # Truthy assert not result # Falsy assert result is True # Exactly True assert result is None # Exactly None # Membership assert item in collection assert item not in collection # Type checking assert isinstance(result, str) # Exception testing (preferred approach) with pytest.raises(ValueError): raise ValueError("error message") # Check exception message with pytest.raises(ValueError, match="invalid input"): raise ValueError("invalid input provided")
Fixtures
Basic Fixture Usage
pythonimport pytest @pytest.fixture def sample_data(): """Fixture providing sample data.""" return {"name": "Alice", "age": 30} def test_sample_data(sample_data): """Test using the fixture.""" assert sample_data["name"] == "Alice" assert sample_data["age"] == 30
Fixture with Setup/Teardown
python@pytest.fixture def database(): """Fixture with setup and teardown.""" # Setup db = Database(":memory:") db.create_tables() db.insert_test_data() yield db # Provide to test # Teardown db.close() def test_database_query(database): """Test database operations.""" result = database.query("SELECT * FROM users") assert len(result) > 0
Fixture Scopes
python# Function scope (default) - runs for each test @pytest.fixture def temp_file(): with open("temp.txt", "w") as f: yield f os.remove("temp.txt") # Module scope - runs once per module @pytest.fixture(scope="module") def module_db(): db = Database(":memory:") db.create_tables() yield db db.close() # Session scope - runs once per test session @pytest.fixture(scope="session") def shared_resource(): resource = ExpensiveResource() yield resource resource.cleanup()
Conftest.py for Shared Fixtures
python# tests/conftest.py import pytest @pytest.fixture def client(): """Shared fixture for all tests.""" app = create_app(testing=True) with app.test_client() as client: yield client @pytest.fixture def auth_headers(client): """Generate auth headers for API testing.""" response = client.post("/api/login", json={ "username": "test", "password": "test" }) token = response.json["token"] return {"Authorization": f"Bearer {token}"}
Parametrization
Basic Parametrization
python@pytest.mark.parametrize("input,expected", [ ("hello", "HELLO"), ("world", "WORLD"), ("PyThOn", "PYTHON"), ]) def test_uppercase(input, expected): """Test runs 3 times with different inputs.""" assert input.upper() == expected
Parametrize with IDs
python@pytest.mark.parametrize("input,expected", [ ("valid@email.com", True), ("invalid", False), ("@no-domain.com", False), ], ids=["valid-email", "missing-at", "missing-domain"]) def test_email_validation(input, expected): """Test email validation with readable test IDs.""" assert is_valid_email(input) is expected
Mocking and Patching
Mocking Functions
pythonfrom unittest.mock import patch, Mock @patch("mypackage.external_api_call") def test_with_mock(api_call_mock): """Test with mocked external API.""" api_call_mock.return_value = {"status": "success"} result = my_function() api_call_mock.assert_called_once() assert result["status"] == "success"
Mocking Exceptions
python@patch("mypackage.api_call") def test_api_error_handling(api_call_mock): """Test error handling with mocked exception.""" api_call_mock.side_effect = ConnectionError("Network error") with pytest.raises(ConnectionError): api_call() api_call_mock.assert_called_once()
Testing Async Code
Async Tests with pytest-asyncio
pythonimport pytest @pytest.mark.asyncio async def test_async_function(): """Test async function.""" result = await async_add(2, 3) assert result == 5 @pytest.mark.asyncio async def test_async_with_fixture(async_client): """Test async with async fixture.""" response = await async_client.get("/api/users") assert response.status_code == 200
Test Organization
Directory Structure
tests/ ├── conftest.py # Shared fixtures ├── __init__.py ├── unit/ # Unit tests │ ├── __init__.py │ ├── test_models.py │ ├── test_utils.py │ └── test_services.py ├── integration/ # Integration tests │ ├── __init__.py │ ├── test_api.py │ └── test_database.py └── e2e/ # End-to-end tests ├── __init__.py └── test_user_flow.py
Best Practices
DO
- Follow TDD: Write tests before code (red-green-refactor)
- Test one thing: Each test should verify a single behavior
- Use descriptive names:
test_user_login_with_invalid_credentials_fails - Use fixtures: Eliminate duplication with fixtures
- Mock external dependencies: Don't depend on external services
- Test edge cases: Empty inputs, None values, boundary conditions
- Aim for 80%+ coverage: Focus on critical paths
- Keep tests fast: Use marks to separate slow tests
DON'T
- Don't test implementation: Test behavior, not internals
- Don't use complex conditionals in tests: Keep tests simple
- Don't ignore test failures: All tests must pass
- Don't test third-party code: Trust libraries to work
- Don't share state between tests: Tests should be independent
Running Tests
bash# Run all tests pytest # Run specific file pytest tests/test_utils.py # Run specific test pytest tests/test_utils.py::test_function # Run with verbose output pytest -v # Run with coverage pytest --cov=mypackage --cov-report=html # Run only fast tests pytest -m "not slow" # Run until first failure pytest -x # Run last failed tests pytest --lf # Run tests with pattern pytest -k "test_user" # Run with debugger on failure pytest --pdb
Quick Reference
| Pattern | Usage |
|---|---|
pytest.raises() | Test expected exceptions |
@pytest.fixture() | Create reusable test fixtures |
@pytest.mark.parametrize() | Run tests with multiple inputs |
@pytest.mark.slow | Mark slow tests |
@patch() | Mock functions and classes |
tmp_path fixture | Automatic temp directory |
pytest --cov | Generate coverage report |
assert | Simple and readable assertions |
Remember: Tests are code too. Keep them clean, readable, and maintainable. Good tests catch bugs; great tests prevent them.


