262 lines
9.9 KiB
Markdown
262 lines
9.9 KiB
Markdown
# Test Structure Migration Guide
|
|
|
|
This document outlines the complete restructuring of the FastAPI application test suite from a single-folder approach to a comprehensive, scalable test organization.
|
|
|
|
## Overview
|
|
|
|
The test suite has been reorganized from:
|
|
```
|
|
tests/
|
|
├── conftest.py
|
|
├── test_admin.py
|
|
├── test_admin_service.py
|
|
├── test_database.py
|
|
├── test_error_handling.py
|
|
├── test_pagination.py
|
|
├── test_performance.py
|
|
├── test_security.py
|
|
├── test_utils.py
|
|
└── pytest.ini
|
|
```
|
|
|
|
To a structured, domain-organized approach that scales with application growth and provides better development workflow support.
|
|
|
|
## New Structure
|
|
|
|
```
|
|
tests/
|
|
├── conftest.py # Core test configuration and database fixtures
|
|
├── pytest.ini # Enhanced pytest configuration
|
|
├── fixtures/ # Shared test fixtures organized by domain
|
|
│ ├── __init__.py
|
|
│ ├── auth_fixtures.py # Authentication: users, tokens, headers
|
|
│ ├── product_fixtures.py # Products: test products, factories
|
|
│ ├── shop_fixtures.py # Shops: shops, stock, shop-products
|
|
│ └── marketplace_fixtures.py # Marketplace: import jobs
|
|
├── unit/ # Fast, isolated component tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py # Unit test specific fixtures
|
|
│ ├── models/
|
|
│ │ ├── __init__.py
|
|
│ │ └── test_database_models.py # Database model tests
|
|
│ ├── utils/
|
|
│ │ ├── __init__.py
|
|
│ │ └── test_data_processing.py # Utility function tests
|
|
│ └── services/
|
|
│ ├── __init__.py
|
|
│ └── test_admin_service.py # Business logic tests
|
|
├── integration/ # Multi-component interaction tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py # Integration test fixtures
|
|
│ ├── api/
|
|
│ │ ├── __init__.py
|
|
│ │ ├── conftest.py # API client fixtures
|
|
│ │ └── v1/
|
|
│ │ ├── __init__.py
|
|
│ │ ├── test_admin_endpoints.py # Admin API endpoint tests
|
|
│ │ └── test_pagination.py # Pagination functionality tests
|
|
│ └── security/
|
|
│ ├── __init__.py
|
|
│ ├── test_authentication.py # Authentication mechanism tests
|
|
│ ├── test_authorization.py # Permission and role tests
|
|
│ └── test_input_validation.py # Input security and validation tests
|
|
├── performance/ # Performance and load tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py # Performance test fixtures
|
|
│ └── test_api_performance.py # API performance benchmarks
|
|
├── system/ # End-to-end system behavior tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py # System test fixtures
|
|
│ └── test_error_handling.py # Application error handling tests
|
|
└── test_data/ # Test data files
|
|
└── csv/
|
|
└── sample_products.csv # Sample CSV data for testing
|
|
```
|
|
|
|
## Migration Changes Made
|
|
|
|
### 1. Fixture Organization
|
|
**Problem Solved:** The original `conftest.py` contained 370+ lines mixing different concerns.
|
|
|
|
**Solution:** Extracted domain-specific fixtures into separate modules:
|
|
- **auth_fixtures.py**: User authentication, tokens, and headers
|
|
- **product_fixtures.py**: Product creation, factories, and bulk data
|
|
- **shop_fixtures.py**: Shop management, stock, and shop-product relationships
|
|
- **marketplace_fixtures.py**: Import job fixtures and helpers
|
|
|
|
### 2. Test Categorization
|
|
**Problem Solved:** All tests ran together, making development cycles slow.
|
|
|
|
**Solution:** Organized tests by execution speed and scope:
|
|
- **Unit Tests**: Fast (< 100ms), isolated, no external dependencies
|
|
- **Integration Tests**: Medium speed, multiple components, database required
|
|
- **Performance Tests**: Slow, large datasets, timing-sensitive
|
|
- **System Tests**: Full application behavior, error scenarios
|
|
|
|
### 3. Enhanced pytest Configuration
|
|
**Previous:** Basic configuration with limited markers.
|
|
|
|
**Current:** Comprehensive configuration including:
|
|
```ini
|
|
[tool:pytest]
|
|
addopts =
|
|
-v --tb=short --strict-markers --strict-config
|
|
--color=yes --durations=10 --showlocals -ra
|
|
--cov=app --cov=models --cov=utils --cov=middleware
|
|
--cov-report=term-missing --cov-report=html:htmlcov
|
|
--cov-fail-under=80
|
|
|
|
markers =
|
|
unit: Unit tests - fast, isolated components
|
|
integration: Integration tests - multiple components
|
|
system: System tests - full application behavior
|
|
performance: Performance and load tests
|
|
slow: Slow running tests
|
|
# Domain-specific markers
|
|
auth: Authentication and authorization tests
|
|
products: Product management functionality
|
|
admin: Admin functionality and permissions
|
|
# ... additional markers
|
|
```
|
|
|
|
## Usage Examples
|
|
|
|
### Development Workflow
|
|
```bash
|
|
# Fast feedback during development (unit tests only)
|
|
pytest -m unit
|
|
|
|
# Before committing (unit + integration)
|
|
pytest -m "unit or integration"
|
|
|
|
# Full test suite (CI/CD)
|
|
pytest
|
|
|
|
# Skip slow tests during development
|
|
pytest -m "not slow"
|
|
```
|
|
|
|
### Domain-Specific Testing
|
|
```bash
|
|
# Test specific functionality
|
|
pytest -m auth # Authentication tests
|
|
pytest -m products # Product-related tests
|
|
pytest -m admin # Admin functionality
|
|
|
|
# Test specific layers
|
|
pytest tests/unit/ # All unit tests
|
|
pytest tests/integration/api/ # API integration tests
|
|
pytest tests/performance/ # Performance tests only
|
|
```
|
|
|
|
### Coverage and Reporting
|
|
```bash
|
|
# Generate coverage report
|
|
pytest --cov-report=html
|
|
|
|
# Find slowest tests
|
|
pytest --durations=0
|
|
|
|
# Detailed failure information
|
|
pytest -vvv --tb=long
|
|
```
|
|
|
|
## Key Improvements
|
|
|
|
### 1. Scalability
|
|
- **Modular fixture organization** allows teams to work on different domains without conflicts
|
|
- **Clear separation of concerns** makes it easy to add new test categories
|
|
- **Factory patterns** provide flexible test data generation
|
|
|
|
### 2. Development Speed
|
|
- **Fast unit tests** provide immediate feedback (typically runs in < 10 seconds)
|
|
- **Selective test execution** allows developers to run only relevant tests
|
|
- **Parallel execution support** ready (uncomment `addopts = -n auto` in pytest.ini)
|
|
|
|
### 3. Maintainability
|
|
- **Domain organization** makes it easy to locate and update tests
|
|
- **Consistent fixture patterns** reduce duplication
|
|
- **Clear naming conventions** improve code readability
|
|
|
|
### 4. CI/CD Pipeline Optimization
|
|
- **Staged test execution**: Run fast tests first, fail fast on basic issues
|
|
- **Performance monitoring**: Track test execution times and performance regressions
|
|
- **Coverage tracking**: Maintain code coverage standards
|
|
|
|
## Migration Process
|
|
|
|
### Automated Setup
|
|
1. Run the migration script:
|
|
```bash
|
|
bash migrate_tests.sh
|
|
```
|
|
|
|
### Manual Steps
|
|
1. **Copy fixture files** to `tests/fixtures/`
|
|
2. **Update main conftest.py** with the new version
|
|
3. **Move test files** to their new locations:
|
|
- `test_database.py` → `tests/unit/models/test_database_models.py`
|
|
- `test_utils.py` → `tests/unit/utils/test_data_processing.py`
|
|
- `test_admin_service.py` → `tests/unit/services/`
|
|
- `test_admin.py` → `tests/integration/api/v1/test_admin_endpoints.py`
|
|
- Split `test_security.py` into `tests/integration/security/` files
|
|
- `test_pagination.py` → `tests/integration/api/v1/`
|
|
- `test_performance.py` → `tests/performance/test_api_performance.py`
|
|
- `test_error_handling.py` → `tests/system/`
|
|
|
|
4. **Update imports** in moved files
|
|
5. **Add pytest markers** to test classes
|
|
6. **Test incrementally**:
|
|
```bash
|
|
pytest tests/unit -v
|
|
pytest tests/integration -v
|
|
pytest -m unit
|
|
```
|
|
|
|
## Benefits Realized
|
|
|
|
### For Developers
|
|
- **Faster feedback loops**: Unit tests complete in seconds
|
|
- **Focused testing**: Run only tests relevant to current work
|
|
- **Better debugging**: Clear test categorization helps identify issues quickly
|
|
|
|
### For Teams
|
|
- **Reduced merge conflicts**: Domain separation allows parallel development
|
|
- **Clear ownership**: Teams can own test domains matching their code ownership
|
|
- **Consistent patterns**: Standardized fixture and test organization
|
|
|
|
### For CI/CD
|
|
- **Optimized pipeline**: Fast tests run first, expensive tests only when needed
|
|
- **Performance monitoring**: Track performance regressions over time
|
|
- **Coverage enforcement**: Maintain quality standards automatically
|
|
|
|
## Troubleshooting
|
|
|
|
### Import Issues
|
|
If you encounter import errors after migration:
|
|
1. Ensure all `__init__.py` files are created
|
|
2. Check that `pytest_plugins` in main `conftest.py` points to correct fixture modules
|
|
3. Verify fixture dependencies (e.g., `test_shop` depends on `test_user`)
|
|
|
|
### Fixture Not Found
|
|
If pytest can't find fixtures:
|
|
1. Check that fixture modules are listed in `pytest_plugins`
|
|
2. Ensure fixture dependencies are imported properly
|
|
3. Verify fixture scope (session vs function) matches usage
|
|
|
|
### Performance Issues
|
|
If tests are running slowly:
|
|
1. Check that unit tests don't have database dependencies
|
|
2. Consider using `pytest-xdist` for parallel execution
|
|
3. Review slow tests with `pytest --durations=10`
|
|
|
|
## Future Enhancements
|
|
|
|
This structure supports future additions:
|
|
- **E2E tests**: Add `tests/e2e/` for complete user journey testing
|
|
- **Contract tests**: Add API contract testing with tools like Pact
|
|
- **Load tests**: Expand performance testing with tools like Locust
|
|
- **Visual tests**: Add screenshot testing for frontend components
|
|
- **Mutation tests**: Add mutation testing to verify test quality
|
|
|
|
The restructured test suite provides a solid foundation for maintaining high code quality as the application scales. |