218 lines
6.6 KiB
Markdown
218 lines
6.6 KiB
Markdown
# Your Specific Test Migration Plan
|
|
|
|
## New Directory Structure
|
|
|
|
```
|
|
tests/
|
|
├── conftest.py # Your current global fixtures (keep most of it)
|
|
├── pytest.ini # New - test configuration
|
|
├── fixtures/ # Extract fixtures from conftest.py
|
|
│ ├── __init__.py
|
|
│ ├── auth_fixtures.py # User, admin fixtures
|
|
│ ├── product_fixtures.py # Product-related fixtures
|
|
│ ├── shop_fixtures.py # Shop-related fixtures
|
|
│ └── database_fixtures.py # DB setup fixtures
|
|
├── unit/ # Fast, isolated tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py # Unit-specific fixtures
|
|
│ ├── models/
|
|
│ │ ├── __init__.py
|
|
│ │ └── test_database_models.py # From test_database.py
|
|
│ ├── utils/
|
|
│ │ ├── __init__.py
|
|
│ │ └── test_data_processing.py # From test_utils.py
|
|
│ └── services/
|
|
│ ├── __init__.py
|
|
│ └── test_admin_service.py # Keep as-is, move here
|
|
├── integration/ # Multi-component tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py
|
|
│ ├── api/
|
|
│ │ ├── __init__.py
|
|
│ │ ├── conftest.py # API client fixtures
|
|
│ │ └── v1/
|
|
│ │ ├── __init__.py
|
|
│ │ └── test_admin_endpoints.py # From test_admin.py
|
|
│ └── security/
|
|
│ ├── __init__.py
|
|
│ ├── test_authentication.py # Split from test_security.py
|
|
│ ├── test_authorization.py # Split from test_security.py
|
|
│ └── test_input_validation.py # Split from test_security.py
|
|
├── system/ # Full system tests
|
|
│ ├── __init__.py
|
|
│ ├── conftest.py
|
|
│ └── test_error_handling.py # From test_error_handling.py
|
|
└── test_data/ # Test data files
|
|
└── csv/
|
|
└── sample_products.csv
|
|
```
|
|
|
|
## Specific File Mappings
|
|
|
|
### 1. Global Fixtures (Keep in main conftest.py)
|
|
**tests/conftest.py** - Keep these fixtures:
|
|
- `engine`, `testing_session_local`, `db`
|
|
- `client`
|
|
- `cleanup`
|
|
|
|
### 2. Extract to Fixture Files
|
|
|
|
**tests/fixtures/auth_fixtures.py:**
|
|
```python
|
|
# Move these from conftest.py:
|
|
- auth_manager
|
|
- test_user
|
|
- test_admin
|
|
- auth_headers
|
|
- admin_headers
|
|
- other_user
|
|
```
|
|
|
|
**tests/fixtures/product_fixtures.py:**
|
|
```python
|
|
# Move these from conftest.py:
|
|
- test_product
|
|
- unique_product
|
|
- multiple_products
|
|
- product_factory
|
|
```
|
|
|
|
**tests/fixtures/shop_fixtures.py:**
|
|
```python
|
|
# Move these from conftest.py:
|
|
- test_shop
|
|
- unique_shop
|
|
- inactive_shop
|
|
- verified_shop
|
|
- shop_product
|
|
- shop_factory
|
|
```
|
|
|
|
### 3. Unit Tests
|
|
|
|
**tests/unit/models/test_database_models.py:**
|
|
Move content from `test_database.py`:
|
|
- `TestDatabaseModels.test_user_model`
|
|
- `TestDatabaseModels.test_product_model`
|
|
- `TestDatabaseModels.test_stock_model`
|
|
- `TestDatabaseModels.test_shop_model_with_owner`
|
|
- `TestDatabaseModels.test_database_constraints`
|
|
|
|
**tests/unit/utils/test_data_processing.py:**
|
|
Move content from `test_utils.py`:
|
|
- `TestGTINProcessor` (entire class)
|
|
- `TestPriceProcessor` (entire class)
|
|
|
|
**tests/unit/services/test_admin_service.py:**
|
|
Keep `test_admin_service.py` exactly as-is, just move to this location.
|
|
|
|
### 4. Integration Tests
|
|
|
|
**tests/integration/api/v1/test_admin_endpoints.py:**
|
|
Move content from `test_admin.py`:
|
|
- `TestAdminAPI` (entire class) - all your admin endpoint tests
|
|
|
|
**tests/integration/security/test_authentication.py:**
|
|
Move from `test_security.py`:
|
|
```python
|
|
class TestAuthentication:
|
|
def test_protected_endpoint_without_auth(self, client):
|
|
# From test_security.py
|
|
|
|
def test_protected_endpoint_with_invalid_token(self, client):
|
|
# From test_security.py
|
|
```
|
|
|
|
**tests/integration/security/test_authorization.py:**
|
|
Move from `test_security.py`:
|
|
```python
|
|
class TestAuthorization:
|
|
def test_admin_endpoint_requires_admin_role(self, client, auth_headers):
|
|
# From test_security.py
|
|
```
|
|
|
|
**tests/integration/security/test_input_validation.py:**
|
|
Move from `test_security.py`:
|
|
```python
|
|
class TestInputValidation:
|
|
def test_sql_injection_prevention(self, client, auth_headers):
|
|
# From test_security.py
|
|
|
|
# def test_input_validation(self, client, auth_headers):
|
|
# # Your commented XSS test
|
|
```
|
|
|
|
### 5. System Tests
|
|
|
|
**tests/system/test_error_handling.py:**
|
|
Move `test_error_handling.py` as-is to this location.
|
|
|
|
## Migration Steps
|
|
|
|
### Step 1: Create Structure
|
|
```bash
|
|
mkdir -p tests/{fixtures,unit/{models,utils,services},integration/{api/v1,security},system,test_data}
|
|
touch tests/{fixtures,unit,integration,system}/__init__.py
|
|
touch tests/unit/{models,utils,services}/__init__.py
|
|
touch tests/integration/{api,security}/__init__.py
|
|
touch tests/integration/api/v1/__init__.py
|
|
```
|
|
|
|
### Step 2: Create pytest.ini
|
|
```ini
|
|
[tool:pytest]
|
|
testpaths = tests
|
|
python_files = test_*.py
|
|
addopts = -v --tb=short
|
|
markers =
|
|
unit: Unit tests - fast, isolated
|
|
integration: Integration tests - multiple components
|
|
system: System tests - full application
|
|
slow: Slow running tests
|
|
```
|
|
|
|
### Step 3: Extract Fixtures
|
|
Create fixture files and move relevant fixtures from your current `conftest.py`.
|
|
|
|
### Step 4: Move Test Files
|
|
Move each test file to its new location and update imports.
|
|
|
|
### Step 5: Update Imports
|
|
After moving files, update imports like:
|
|
```python
|
|
# Old import in test files
|
|
# No explicit imports needed since fixtures were in conftest.py
|
|
|
|
# New imports in test files
|
|
from tests.fixtures.auth_fixtures import test_user, auth_headers
|
|
from tests.fixtures.product_fixtures import test_product
|
|
```
|
|
|
|
## Running Tests by Category
|
|
|
|
```bash
|
|
# Fast unit tests during development
|
|
pytest tests/unit -m unit
|
|
|
|
# Integration tests before commit
|
|
pytest tests/integration -m integration
|
|
|
|
# Full test suite
|
|
pytest
|
|
|
|
# Specific domain tests
|
|
pytest tests/unit/services/ tests/integration/api/v1/test_admin_endpoints.py
|
|
|
|
# Your current debug tests (move to integration/security)
|
|
pytest tests/integration/security/ -v -s
|
|
```
|
|
|
|
## Benefits for Your Specific Tests
|
|
|
|
1. **Your admin tests** get separated into service logic (unit) vs API endpoints (integration)
|
|
2. **Your security tests** get properly categorized by concern
|
|
3. **Your database tests** become proper model unit tests
|
|
4. **Your utility tests** become isolated unit tests
|
|
5. **Your error handling** becomes system-level testing
|
|
|
|
This structure will make your test suite much more maintainable and allow for faster development cycles! |