# Add Missing Tests to Every Pull Request in a Rust Service

On each pull request an agent finds untested Rust handler code, writes cargo tests against a real Postgres, runs them and posts them as a patch.

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

## What the agent does

In an Axum and SQLx service like launchbadge/realworld-axum-sqlx, the agent reads the touched handlers in `src/http/articles/mod.rs`, `listing.rs` or `comments.rs`. That file holds the crate's one unit test, `test_slugify`, under a comment saying the rest is left to integration tests, which `.github/workflows/rust.yml` runs as the RealWorld Postman collection after `cargo test`.

Pure functions get `#[test]` cases beside `test_slugify`. Handler behavior gets `#[tokio::test]` cases in a `#[cfg(test)]` module that builds the private `api_router()` with the `ApiContext` extension layer `serve()` adds, and sends requests with `tower::ServiceExt::oneshot`. `sqlx::query!` checks queries against a live database at compile time and the repository keeps no offline data, so the agent starts Postgres, fills `DATABASE_URL` and `HMAC_KEY` from `.env.sample` and runs `sqlx db setup`, as the workflow does.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This pull request changes the article handlers in src/http/articles. Read its diff against the base branch, taking the head and base commits from the pull request itself, and list the new or changed behavior no test covers, with file and line. Write cargo tests for it: #[test] unit tests next to the existing test_slugify for pure functions, and #[tokio::test] tests in a #[cfg(test)] module that call api_router(), layered with an ApiContext AddExtensionLayer as serve() does, with tower::ServiceExt::oneshot for handler behavior, including the error responses. Start Postgres in the sandbox, set DATABASE_URL and HMAC_KEY as .env.sample describes and run sqlx db setup so sqlx::query! compiles. Run cargo test until the new tests pass, and confirm each one fails when the line it covers is broken. Change nothing but #[test] functions and a new #[cfg(test)] module, do not commit or push, and do not open a pull request. Post one review on the pull request that lists each untested behavior with file and line, the test that covers it, the cargo test result, and the new tests as one patch. A test that fails because the change is wrong goes in the review as a possible bug. 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

What comes back is one review on the pull request and no commit. It lists each uncovered behavior by file and line, the test written for it, and the `cargo test` summary line from a run against a migrated Postgres. The patch is in the review.

Left to the reviewer: whether to apply the patch, and whether a handler test the review reports as failing found a bug or encodes the wrong RealWorld spec behavior.

## Run it on every pull request opened against main

Save this request as an automation's prompt with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and `branch_patterns` `main`, the repository's default branch. Pull requests by an owner, member or collaborator fire it; a fork's never does. The event text has the pull request's URL, title, author, labels, base branch and body but no commit SHAs, which is why the prompt has the agent read the head and base from the URL.

## Why run it on Rush?

With `sqlx = 0.5` and no offline query data, a test in this crate does not even compile without a migrated database. The sandbox brings up Postgres and applies `migrations/1_setup.sql` through `4_article.sql` per pull request, so the review reports a test that compiled and ran.

## Frequently asked questions

### Why does it not use #[sqlx::test]?

That attribute arrived in SQLx 0.6 and the crate pins 0.5. Upgrading a dependency is not a test change, so the agent sets up each test's data itself instead.

### Why are the tests a patch instead of a commit?

The per-pull-request review never pushes, so the author decides what lands. Applying the patch from the review adds the tests to the branch, and `rust.yml` then runs them with the Postman collection.

## More Rust jobs

- [Update Pinned GitHub Actions in a Rust Repo](https://getrush.ai/run/update-github-actions/rust/)
- [Upgrade Rust Dependencies Every Week](https://getrush.ai/run/upgrade-dependencies-weekly/rust/)
- [Write Property-Based Tests for a Rust Crate](https://getrush.ai/run/write-property-tests/rust/)

## The same job on other stacks

- [Add Missing Tests to Every Pull Request in a Spring Boot App](https://getrush.ai/run/test-every-pull-request/spring-boot/)
- [Add Missing Tests to Every Pull Request in a SvelteKit App](https://getrush.ai/run/test-every-pull-request/sveltekit/)
- [Add Missing Tests to Every Pull Request in a Symfony App](https://getrush.ai/run/test-every-pull-request/symfony/)

## 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/)
