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

An agent finds what a Go pull request leaves untested in the users and articles packages, writes Gin handler tests, runs go test, and reviews with a patch.

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

## What the agent does

In gothinkster/golang-gin-realworld-example-app, the agent reads the pull request's diff and lists each new branch in the handlers of `users/routers.go` and `articles/routers.go`, the rules in each package's `validators.go` and the queries in `models.go`, then searches that package's tests for one that reaches it.

New tests go where this repository keeps them: in the package's own `unit_test.go`, whose `TestMain` opens a SQLite database with `common.TestDBInit()` and frees it with `common.TestDBFree`. `articles/unit_test.go` builds its router with `setupRouter()`, creates rows with `createTestUser` and `createArticleWithUser`, and sends requests through `httptest` with `common.HeaderTokenMock`. Where a file already drives cases from a table, such as `articleRequestTests`, the agent adds rows there instead of a new function.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Review this pull request for untested behavior. Check it out and read its diff against the base branch. List each new or changed behavior in users/ and articles/ (routers.go, validators.go, serializers.go, models.go) that no existing test reaches. Write Go tests for each in that package's unit_test.go, in its style: TestMain with common.TestDBInit and common.TestDBFree, setupRouter, the createTestUser and createArticleWithUser helpers, httptest requests with common.HeaderTokenMock, testify assert, and new rows in a case table where the file has one. Run go test ./... and gofmt -l until the new tests pass and are formatted. Do not change application code: if a test fails because the code is wrong, keep it out of the patch and describe the failure. Do not push commits, commit to the branch or open a pull request. Post one review on the pull request that lists each untested behavior with file and line, each test you wrote, the go test ./... and gofmt -l results, and the new tests as one 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 on the pull request. It lists each untested behavior with its file and line, for example a new filter in `ArticleList` in `articles/routers.go`, the `TestXxx` functions or table rows that cover it, and the `go test ./...` and `gofmt -l` results. The patch touches only `unit_test.go` files and adds nothing to `go.mod`. Nothing is pushed.

Two decisions stay with a reviewer: whether to apply the patch with `git apply`, and whether a test the agent kept out because a handler failed it shows a real bug or a wrong expectation.

## Run it on every pull request opened against main

An automation with this request as its prompt and a `github` trigger on `pull_request.opened` and `pull_request.synchronize`, `branch_patterns` `main`, reviews each new pull request and each push to one. Only an owner, member or collaborator fires it; a fork's pull request never does. The event carries no SHAs, so the agent reads the head and base from the pull request URL.

## Why run it on Rush?

The users and articles packages each share one SQLite database for their whole `TestMain`, so a new test that leaves rows behind can break one that runs after it. The agent runs the full `go test ./...`, not only its new functions, before the patch goes into the review, and the reviewer sees that result next to each behavior it found.

## Frequently asked questions

### Why does it post a patch instead of pushing the tests?

This recipe reviews and never writes to the branch, so the author decides what lands. Applying the patch from the review adds the tests in one step, and the review already shows them passing on the pull request's head.

### Will it put the tests in a separate _test package?

No. Each package keeps its tests in `unit_test.go` inside the same package, so they can call unexported helpers like `resetDBWithMock`, and the agent writes there too.

## More Go jobs

- [Upgrade Go Dependencies Every Week](https://getrush.ai/run/upgrade-dependencies-weekly/go/)
- [Write Unit Tests for a Go Project](https://getrush.ai/run/write-unit-tests/go/)
- [Add Test Coverage Reporting to Go CI](https://getrush.ai/run/add-coverage-to-ci/go/)

## The same job on other stacks

- [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/)
- [Add Missing Tests to Every Pull Request in a Next.js App](https://getrush.ai/run/test-every-pull-request/nextjs/)

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