HOW-TO · DEV
How to create integration test suites for REST API endpoints using AI-assisted tools
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
Running API service, an AI assistant (e.g., via CLI), a REST API documented via OpenAPI/Swagger or manual endpoint list, and a Python testing framework such as pytest with the requests library.
What this does
This guide describes how to use an AI assistant to generate a complete integration test suite for REST API endpoints. The AI produces parameterized test functions covering happy paths, error responses, authentication scenarios, and input validation. The result is a reusable test file that can be run locally and in CI pipelines.
Steps
- List all endpoints to test in a structured format: HTTP method, path, request body schema, and expected status codes.
- Provide this endpoint inventory to the AI assistant and request a pytest test file with setup, teardown, and parameterized tests.
- Ask the AI to include fixtures for base URL configuration, auth token injection, and response assertion helpers.
- Ask the AI assistant to generate the full test file with appropriate setup and teardown functions.
- Execute the test suite and capture the output.
- Review test coverage and ask for additional edge case coverage if gaps appear.
- Refactor the generated tests to align with project conventions.
- Run the tests again to confirm all pass.
Verification
pytest tests/integration/test_api_users.py -v --tb=short
Expected output:
tests/integration/test_api_users.py::test_get_users_success PASSED
tests/integration/test_api_users.py::test_get_users_unauthorized PASSED
tests/integration/test_api_users.py::test_post_users_success PASSED
tests/integration/test_api_users.py::test_post_users_validation_error PASSED
Common failures
- Connection refused errors — The API service is not running at the expected base URL. Verify the service is up with
curl http://localhost:8000/healthbefore running the test suite. - Unexpected JSON structure in assertions — The API response schema does not match what the AI generated. Compare the actual JSON output with the test assertions and update the expected fields.
- Missing authentication headers — Tests receive 401 responses because auth tokens are not injected. Ensure the fixture that sets the Authorization header is applied to all requests.
- Flaky tests with timeouts — Slow API responses cause timeouts. Adjust the
requeststimeout parameter in the test file from the default to 10–15 seconds.
Related guides
- Setup property-based testing with AI edge cases (guide 26)
- Configure CI/CD with GitHub Actions for AI tests (guide 27)