Testing in Python
This file covers testing strategies, frameworks, and best practices for Python applications.
Testing Frameworks​
pytest (Recommended)​
- Most popular Python testing framework
- Simple syntax and powerful features
- Extensive plugin ecosystem
- Great assertion introspection
unittest (Built-in)​
- Part of Python standard library
- Class-based test structure
- Good for traditional unit testing
doctest​
- Tests embedded in docstrings
- Great for simple examples
- Documentation and testing combined
pytest Examples​
# test_math.py
def test_addition():
assert 2 + 2 == 4
def test_division():
assert 10 / 2 == 5
# Using fixtures
import pytest
@pytest.fixture
def sample_data():
return {"name": "John", "age": 30}
def test_sample_data(sample_data):
assert sample_data["name"] == "John"
Testing Types​
Unit Tests​
- Test individual functions/methods
- Fast execution
- Mock external dependencies
Integration Tests​
- Test component interactions
- Database connections
- API endpoints
End-to-End Tests​
- Test complete workflows
- User scenarios
- Browser automation (Selenium)
Best Practices​
- Write tests first (TDD)
- Use descriptive test names
- Test edge cases and error conditions
- Mock external services
- Maintain test independence
- Use fixtures for setup/teardown
- Achieve good test coverage
Coverage Analysis​
# Install coverage
pip install coverage
# Run tests with coverage
coverage run -m pytest
# Generate report
coverage report
coverage html
Continuous Integration​
Integrate testing into CI/CD pipelines for automated quality assurance.