HOW-TO · DEV

How to create integration test suites for REST API endpoints using AI-assisted tools

intermediate20 minBy Fredoline Eruo
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

  1. List all endpoints to test in a structured format: HTTP method, path, request body schema, and expected status codes.
  2. Provide this endpoint inventory to the AI assistant and request a pytest test file with setup, teardown, and parameterized tests.
  3. Ask the AI to include fixtures for base URL configuration, auth token injection, and response assertion helpers.
  4. Ask the AI assistant to generate the full test file with appropriate setup and teardown functions.
  5. Execute the test suite and capture the output.
  6. Review test coverage and ask for additional edge case coverage if gaps appear.
  7. Refactor the generated tests to align with project conventions.
  8. 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

  1. Connection refused errors — The API service is not running at the expected base URL. Verify the service is up with curl http://localhost:8000/health before running the test suite.
  2. 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.
  3. 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.
  4. Flaky tests with timeouts — Slow API responses cause timeouts. Adjust the requests timeout 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)