# Add Missing Tests to Every Pull Request in a Flask App

An agent reads each pull request to a Flask app's API, writes pytest tests for the requests it leaves untested, runs them, and posts a patch.

[Run this on Rush](https://cloud.getrush.ai/run/test-every-pull-request/flask/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent checks out the pull request and maps it onto the API. In CTFd the challenges API is one Flask-RESTX namespace in `CTFd/api/v1/challenges.py`, a resource class per route: `ChallengeList`, `Challenge`, `ChallengeAttempt` and smaller ones for solves, files, tags, hints and flags. For each method the pull request changes, it names the request and user state that reach the new code and checks whether a test sends it.

New tests go in `tests/api/v1/test_challenges.py` in its own shape: plain functions that build an app with `create_ctfd()`, tear it down with `destroy_ctfd(app)`, and set up state with `gen_challenge`, `gen_flag`, `register_user` and `login_as_user` from `tests/helpers.py` plus `set_config` from `CTFd.utils`, each with a docstring like `test_api_challenges_get_visibility_public`'s.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Check out this pull request and diff it against its base branch. List the new or changed behavior in CTFd/api/v1/challenges.py, and in any plugin or helper it calls, that no existing test covers, with file and line. Write pytest tests for each item in tests/api/v1/test_challenges.py, following the existing style: create_ctfd and destroy_ctfd per test, the gen_* helpers, register_user and login_as_user from tests/helpers.py, set_config from CTFd.utils for settings and freezegun for time, with a docstring stating what each test checks. Change no application code. Run the new tests with uv run pytest -k, then the whole file. Do not push commits or open a pull request. Post one review on the pull request listing each untested behavior with file and line, the tests you wrote, the pytest summary, any test that fails against the branch's code with the request it sent and its failing assertion, and the tests as a patch. If this run names no pull request, in resources or after this prompt, stop and say so.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app/pull/123"
    }
  ]
}
```

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

One review lands on the pull request: each untested request path with its file and line, the test function written for it, the `pytest` summary for the new tests and the whole challenges file, and a patch that changes `tests/api/v1/test_challenges.py` only. Nothing is pushed.

The reviewer decides whether to apply the patch, and, where a test fails, whether the API really lets a hidden challenge's details through to a non-admin or the test set the wrong visibility.

## Run it on every pull request opened against master

An automation with this request as its prompt and a `github` trigger on `pull_request.opened` and `pull_request.synchronize`, `branch_patterns` `master`, CTFd's default branch, reviews each new pull request and each push to one. Only an owner, member or collaborator fires it, and a fork's pull request never does. The event text gives the URL, title, author, labels, base branch and body but no SHAs, so the agent checks out the head from the URL. A pull request that only edits a theme still fires, since no trigger filters on paths, and the review says the API did not change.

## Why run it on Rush?

CTFd's test config needs no services: `TestingConfig` in `CTFd/config.py` falls back to an in-memory `sqlite://` when `TESTING_DATABASE_URL` is unset, so each test builds a whole CTF in seconds. So the agent can cheaply send the same request as an admin, a player and an anonymous visitor, not only the one the author tried.

## Frequently asked questions

### Why are the tests posted as a patch rather than pushed?

The review recipe does not push. The author applies the patch from the review, and CTFd's workflows run it on the next push.

### Does it run the MySQL and Postgres workflows' tests?

No. It runs on SQLite, as the `sqlite.yml` workflow does. CTFd's `mysql.yml` and `postgres.yml` workflows still run the same tests against those databases on the author's push.

## More Flask jobs

- [Upgrade Flask Dependencies Every Week](https://getrush.ai/run/upgrade-dependencies-weekly/flask/)
- [Upgrade a Flask App to Python 3.14](https://getrush.ai/run/upgrade-python-version/flask/)
- [Write pytest Tests for a Flask App](https://getrush.ai/run/write-pytest-tests/flask/)

## The same job on other stacks

- [Add Missing Tests to Every Pull Request in a Go Service](https://getrush.ai/run/test-every-pull-request/go/)
- [Add Missing Tests to Every Pull Request in a Laravel App](https://getrush.ai/run/test-every-pull-request/laravel/)
- [Add Missing Tests to Every Pull Request in a NestJS API](https://getrush.ai/run/test-every-pull-request/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/)
