Files
2026-06-15 14:22:07 +08:00

119 lines
2.2 KiB
Markdown

# Testing Guide
## Running Tests
```bash
cd backend
# Run all tests
pytest
# Run with coverage
pytest --cov=app --cov-report=term-missing
# Run specific test file
pytest tests/api/test_health.py -v
# Run specific test
pytest tests/api/test_health.py::test_health_check -v
# Run only unit tests
pytest tests/unit/
# Run only integration tests
pytest tests/integration/
# Run with verbose output
pytest -v
# Stop on first failure
pytest -x
```
## Test Structure
```
tests/
├── conftest.py # Shared fixtures
├── api/ # API endpoint tests
│ ├── test_health.py
│ └── test_auth.py
├── unit/ # Unit tests (services, utils)
│ └── test_services.py
└── integration/ # Integration tests
└── test_db.py
```
## Key Fixtures (`conftest.py`)
```python
# Database session for tests
@pytest.fixture
async def db_session():
async with async_session() as session:
yield session
await session.rollback()
# Test client
@pytest.fixture
def client():
return TestClient(app)
# Authenticated client
@pytest.fixture
async def auth_client(client, test_user):
token = create_access_token(test_user.id)
client.headers["Authorization"] = f"Bearer {token}"
return client
```
## Writing Tests
### API Endpoint Test
```python
def test_health_check(client):
response = client.get("/api/v1/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
```
### Service Test
```python
async def test_create_item(db_session):
service = ItemService(db_session)
item = await service.create(ItemCreate(name="Test"))
assert item.name == "Test"
```
### Test with Authentication
```python
def test_protected_endpoint(auth_client):
response = auth_client.get("/api/v1/users/me")
assert response.status_code == 200
```
## Frontend Tests
```bash
cd frontend
# Run unit tests
bun test
# Run with watch mode
bun test --watch
# Run E2E tests
bun test:e2e
# Run E2E in headed mode (see browser)
bun test:e2e --headed
```
## Test Database
Tests use a separate test database or SQLite in-memory:
- Configuration in `tests/conftest.py`
- Database is reset between tests
- Use fixtures for test data