This guide covers how to test the Python API backend using pytest.
Backend tests use pytest to test the FastAPI application, services, and models.
cd webapp/packages/api/user-service
# Run all tests
python -m pytest
# Run unit tests only
python -m pytest tests/unit/
# Run integration tests only
python -m pytest tests/integration/
# Run with coverage
python -m pytest --cov=. --cov-report=htmltests/
conftest.py # Shared fixtures
factories/ # Test data factories
agent_factory.py
user_factory.py
unit/ # Unit tests
test_user_service.py
test_user_model.py
integration/ # Integration tests
test_health_endpoint.py
import pytest
from services.user_service import UserService
def test_create_user():
service = UserService()
user = service.create_user(name="Test User")
assert user.name == "Test User"from tests.factories.user_factory import UserFactory
def test_user_with_factory():
user = UserFactory.create()
assert user.id is not None- pytest configuration:
pytest.ini - Coverage configuration:
.coveragerc