# Write API Tests for a Django REST Framework API

An agent adds 20 APIClient tests to the untested endpoints of a Django REST Framework app, taking pytest from 40 to 60 passed against a real Postgres.

[Run this on Rush](https://cloud.getrush.ai/run/write-api-tests/django-rest-framework/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `pytest.ini`, `config/django/test.py`, `styleguide_example/api/urls.py` and each app's `urls.py` and `apis.py`, then the existing tests. The `authentication` tests already covered session and JWT login, so it left them alone. The users endpoints have no authentication; the files endpoints use `ApiAuthMixin`, which returns 403 to an anonymous caller.

It writes `users/tests/apis/test_user_apis.py`, `files/tests/apis/test_file_apis.py` and `errors/tests/test_error_apis.py`. Six of the new tests failed first. Rather than change the app, it read the failures: the repository's exception handler wraps validation errors as `{"detail": {...}}`, and `/api/errors/trigger/unique/` answers 400 with code `unique` instead of raising. It fixed the assertions to match.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Map this Django REST Framework project's API URLs and existing tests, then add APIClient tests for every endpoint that has none: real login and logout, permission denials, serializer validation failures, missing records and the error shape the project's exception handler returns. Read each view's permission classes; do not invent authentication for public endpoints. Use a disposable local Postgres and the existing test settings, no mocks of the project's own modules, and keep uploads in a temporary directory. Run pytest before and after, plus ruff and mypy with the repository's configs, and open a pull request with the endpoint list and results.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on HackSoftware/Django-Styleguide-Example at a70ef43: 6 files changed, 251 insertions(+), all new test files and no change to existing code. `pytest` went from `40 passed, 20 warnings in 1.68s` to `60 passed, 40 warnings in 2.36s`. Coverage of the API modules rose from 61% to 81%, with `users/apis.py` and `files/apis.py` at 100%. `ruff check .` and `mypy` passed on the changed tree.

For the reviewer: the tests assert the `{"detail": {...}}` error shape, so switching to `hacksoft_proposed_exception_handler` would break them. The Google OAuth `apis.py` files stay at 36%, untested.

## Run it on every merged pull request

An automation with a `github` trigger on `pull_request.closed.merged` and `branch_patterns` `master` runs this prompt after each merge. A merge that adds a view to an `apis.py` gets its tests in a follow-up pull request.

## Why run it on Rush?

Half the work was learning the app's real contract. The users endpoints are public, so denials were only tested where `ApiAuthMixin` applies, and the six first failures exposed the wrapped error shape and a uniqueness error returned as 400, not a test bug to delete.

## Frequently asked questions

### How was coverage measured?

The repository does not configure coverage, so the agent ran `coverage run -m pytest` in its own environment over `*/apis.py` and `api/*`. Missed statements fell from 148 of 376 to 71.

### Did the tests need anything outside the repository?

A Postgres 15 on port 5433 through `DATABASE_URL`, installed with apt because `psycopg2` would not build without libpq. File uploads went to a temporary directory, and `git status` showed only the new tests.

## More Django REST Framework jobs

- [Add API Key Authentication to a Django REST Framework Project](https://getrush.ai/run/add-api-key-auth/django-rest-framework/)
- [Add API Versioning to a Django REST Framework Project from a Linear Issue](https://getrush.ai/run/add-api-versioning/django-rest-framework/)
- [Add an Audit Log to a Django REST Framework API from a Linear Issue](https://getrush.ai/run/add-audit-log/django-rest-framework/)

## The same job on other stacks

- [Write API Tests for a Flask-HTTPAuth Token API](https://getrush.ai/run/write-api-tests/flask-httpauth/)
- [Write API Integration Tests for a Hono App](https://getrush.ai/run/write-api-tests/hono/)
- [Write API Integration Tests for a NestJS App](https://getrush.ai/run/write-api-tests/nestjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
