Running Tests
Basic Test Execution
Run all tests with verbose output:Coverage Reports
Run tests with coverage analysis:- Terminal coverage summary
- HTML coverage report in
htmlcov/ - XML coverage report for CI/CD
Running Specific Tests
Test Configuration
Test configuration is defined inpyproject.toml:
Key Features
- Strict mode: Catches configuration and marker errors
- Auto coverage: Coverage runs automatically with all tests
- Async support:
asyncio_mode = "auto"enables automatic async test detection - Multiple formats: Terminal, HTML, and XML coverage reports
Coverage Configuration
Coverage settings inpyproject.toml:
Excluded from Coverage
- Test files themselves
- Debug code blocks
- Abstract methods and protocols
if __name__ == "__main__"blocks- NotImplementedError raises
Test Structure
Directory Layout
Test File Naming
Tests must follow these patterns:- Files:
test_*.pyor*_test.py - Functions:
test_* - Classes:
Test*
Writing Tests
Basic Test Example
Async Test Example
Using Fixtures
Mocking with pytest-mock
Parametrized Tests
Available Test Dependencies
Development dependencies for testing (frompyproject.toml):
- pytest
^8.4.0- Core testing framework - pytest-asyncio
^1.0.0- Async test support - pytest-cov
^6.1.1- Coverage plugin - pytest-mock
^3.14.1- Mocking utilities
Code Quality Checks
Run All Quality Checks
- Formatting (ruff format)
- Linting (ruff + pylint)
- Type checking (mypy + pyright)
- Security (bandit)
Individual Quality Commands
Pre-commit Hooks
Pre-commit hooks run automatically before each commit:Continuous Integration
Tests run automatically in CI/CD pipelines:- ✅ All tests must pass
- ✅ Code coverage must meet threshold
- ✅ Type checking must pass (mypy + pyright)
- ✅ Linting must pass (ruff + pylint)
- ✅ Security checks must pass (bandit)
Testing Best Practices
Write descriptive test names
Write descriptive test names
Test names should clearly describe what is being tested:
Use fixtures for common setup
Use fixtures for common setup
Define reusable fixtures in
conftest.py:Test edge cases and errors
Test edge cases and errors
Don’t just test the happy path:
Keep tests isolated
Keep tests isolated
Each test should be independent:
Mock external dependencies
Mock external dependencies
Mock HTTP calls, file I/O, and external services:
Cleanup
Remove test artifacts and cache files:__pycache__directories.pytest_cache.mypy_cache.ruff_cachehtmlcov/(coverage reports).coveragefiles*.pycfiles
Next Steps
Development Setup
Set up your development environment
Contributing
Learn how to contribute to Esprit