feat: restructure test infrastructure with unit/integration/system separation

Test Organization:
- Create tests/unit/, tests/integration/, tests/system/ directories
- Move existing tests into appropriate categories
- Add pytest markers (@pytest.mark.unit, @pytest.mark.integration)

Shared Fixtures:
- Create conftest.py with SQLite engine (for unit tests)
- Add PostgreSQL session fixture with transaction rollback
- Add TestClient fixture for API tests

Configuration:
- Update pyproject.toml with asyncio_mode=auto
- Add test markers and default addopts
- Add aiosqlite dependency for SQLite support

E2E Testing:
- Initialize Playwright in e2e/ directory
- Add playwright.config.ts
- Create login flow E2E test

Build:
- Add test-unit, test-integration, test-system, test-e2e to Makefile
- Update test target to run all categories
- Add testing documentation to README

Note: Some tests have import issues due to missing python-jose
package in dev environment. This needs to be addressed separately.
This commit is contained in:
Fusion
2026-05-18 15:00:33 +02:00
parent a441ea2fac
commit 3ccd94f661
26 changed files with 627 additions and 11 deletions
@@ -0,0 +1,41 @@
## Why
The current test infrastructure has several issues that slow development and reduce confidence:
1. **Mixed test types in single directory**: Unit, integration, and system tests are all mixed together in `apps/api/tests/`, making it hard to run fast unit tests independently.
2. **Duplicate fixture code**: Database setup/teardown is duplicated across multiple test files instead of using a shared `conftest.py`.
3. **Inconsistent test patterns**: 4 different patterns exist for database setup (sync helpers, async fixtures, module reloads, autouse env vars).
4. **No test database isolation**: Tests truncate tables manually instead of using transaction rollback, leading to potential data leakage.
5. **Backend tests require live PostgreSQL**: Unit tests that test pure logic still connect to a real database.
6. **No E2E/system tests**: There's no automated way to test the full stack (frontend + API + database) together.
7. **Import errors in local environment**: Tests depend on packages not in `dev` dependencies (e.g., `python-jose`).
## What Changes
- **Restructure backend tests** into `unit/`, `integration/`, and `system/` directories.
- **Create shared fixtures** in `conftest.py` for database sessions, test client, and authentication.
- **Add SQLite in-memory support** for fast unit tests that don't need PostgreSQL.
- **Implement transaction rollback isolation** using `begin_nested()` for integration tests.
- **Add missing test dependencies** to `pyproject.toml`.
- **Create E2E test setup** using Playwright for critical user journeys.
- **Add test markers** (`@pytest.mark.unit`, `@pytest.mark.integration`, `@pytest.mark.system`) to enable selective test runs.
- **Update Makefile** with `test-unit`, `test-integration`, `test-system`, and `test-e2e` targets.
## Capabilities
### New Capabilities
- `test-organization`: Structured test directories with clear separation of concerns.
- `test-isolation`: Transaction rollback and SQLite support for fast, isolated tests.
- `e2e-testing`: Playwright-based end-to-end tests for critical user journeys.
### Modified Capabilities
- `docker-infrastructure`: Add test database service and test runner configuration.
## Impact
- `apps/api/tests/`: Restructured into subdirectories.
- `apps/api/pyproject.toml`: New test dependencies added.
- `apps/api/conftest.py`: New shared fixtures file.
- `Makefile`: New test targets.
- New `e2e/` directory for Playwright tests.
- CI pipeline may need updates to run new test categories.