Aller au contenu

Tests

Guide des tests pour JARVIS.

Structure

tests/
├── conftest.py              # Fixtures
├── test_auth_endpoints.py
├── test_chat_endpoints.py
├── test_documents.py
└── integration/
    └── test_full_flow.py

Exécution

# Tous les tests
pytest tests/ -v

# Avec coverage
pytest tests/ --cov=app --cov-report=html

# Tests spécifiques
pytest tests/test_auth_endpoints.py -v

# Mode watch
ptw tests/ -- -v -x

Pattern AAA

@pytest.mark.asyncio
async def test_create_conversation(client, auth_headers):
    # Arrange
    payload = {"title": "Test"}

    # Act
    response = await client.post(
        "/api/v1/chat/conversations",
        json=payload,
        headers=auth_headers
    )

    # Assert
    assert response.status_code == 201
    assert response.json()["title"] == "Test"

Fixtures

@pytest.fixture
async def test_user(db_session) -> User:
    user = User(email="test@example.com", ...)
    db_session.add(user)
    await db_session.commit()
    return user

@pytest.fixture
async def auth_headers(test_user) -> dict:
    token = create_access_token({"sub": str(test_user.id)})
    return {"Authorization": f"Bearer {token}"}

Coverage

  • Minimum: 75%
  • Objectif: 80%
  • Actuel: ~78%