Backend & API Testing
How to instruct the agent to test endpoints, database logic, and external integrations.
Backend tests in XAIO typically run as pytest in the workspace container. The agent sets up a tests/ structure if not already present, and can write unit, integration, and smoke tests.
Endpoint tests
Test all endpoints at once:
> Test all FastAPI endpoints for me. For each: 200, 400, 401, 404 where applicable. Write it in tests/test_endpoints.py and run it.
What happens: The agent reads your routes from main.py, generates test cases per route with valid JWT, invalid payload, missing auth header — and runs them via pytest -v.
Per-endpoint detail:
> Write a test for POST /api/projects — it must verify that:
> 1. A logged-in user can create a project
> 2. Plan-limit violation returns 402
> 3. Invalid pipeline_type returns 400
Database tests
Migration testing:
> Test migration V075. Apply it, create some beta applications, verify all fields persist correctly. Then roll it back and verify the table is gone.
Constraint tests:
> Write a test that verifies status can only be 'pending', 'approved', 'rejected', 'waitlist', or 'contacted'. Other values must throw an IntegrityError.
Mock vs. real DB
By default, the agent uses a real local Postgres for backend tests (see memory entry *integration tests must hit a real database, not mocks*).
If you explicitly want mocks:
> Mock the DB calls for this test — I want to test the service logic in isolation, not persistence.
External APIs (Stripe, Brevo, GitHub)
These are always mocked, never called for real during tests:
> Test the Stripe webhook handler. Mock a payment_intent.succeeded event, verify that the subscription is set to active, idempotency test with a duplicate event ID.
Property-based testing
For complex invariants, the agent can use hypothesis:
> Use hypothesis to verify that the slug generator is ALWAYS URL-safe, no matter what input. Try Unicode, emojis, very long strings.
Integration tests with containers
For tests that need a real Postgres + Redis:
> Write an integration test that creates a user, creates a project, publishes it, and verifies all 6 cleanup steps work when deleting it.
Coverage & CI
> Run pytest --cov=services --cov-report=html and tell me where we're under 80% coverage.
The agent can also generate a GitHub Actions workflow file:
> Write a .github/workflows/test.yml that runs the backend tests on every PR — Postgres as a service container.

