All jobs

Write Unit Tests for a Go Project

An agent adds four Go test files to a Gin RealWorld API, two of which drive its handlers through database failures, lifting coverage from 90.5% to 94.7%.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent ran go test ./... -coverprofile on the untouched clone first and ranked files with go tool cover -func. In gothinkster/golang-gin-realworld-example-app the gaps were not happy paths: the uncovered lines in articles/routers.go and users/routers.go were the branches taken when storage fails. It skipped common/utils.go, whose remaining gaps are unreachable.

So the new tests break the database on purpose. articles/routers_dberror_test.go renames a table so list endpoints fail, and adds SQLite triggers that block writes while reads still work, then checks the HTTP response and that nothing changed. users/routers_dberror_test.go does the same for registration and update, common/database_token_test.go covers Init, TestDBInit, TestDBFree and the GenToken claims, and hello_test.go runs Migrate twice.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Run the test suite with coverage, pick the five least-covered files that hold real logic, and write Go testing tests for them that exercise behaviour, not implementation details. Do not change production code unless a test reveals a bug; if it does, say so in the description. Open a pull request with the coverage before and after.",
  "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 gothinkster/golang-gin-realworld-example-app at a41289b: 4 files changed, 389 insertions(+), all new _test.go files and no production code. The baseline failed until the run installed gcc, because go-sqlite3 needs cgo; with CGO_ENABLED=1 the untouched clone passed. After the change CGO_ENABLED=1 go test ./... -coverprofile=cover.out exited 0 with ok github.com/gothinkster/golang-gin-realworld-example-app/articles 5.063s coverage: 98.5% of statements, total coverage went from 90.5% to 94.7%, and go vet ./... exited 0. No pull request was opened.

For the reviewer: the run found that GetArticleFeed in articles/models.go ignores the errors from its Find and Count calls, so the feed answers 200 with an empty list when the database fails. It left that unfixed. go test -count=2 ./articles fails in TestTagModel with or without the new files, and whether CI has gcc the run could not check.

Run it on every night

An automation with a schedule trigger, cron 0 2 * * * and your timezone, runs this prompt in the Project's repository each night. For this repository the sandbox needs gcc and CGO_ENABLED=1, or every night ends on the same sqlite panic before any new test runs.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

At 90.5% the suite looked finished, but the missing lines were the paths where the database says no. Forcing those failures with renamed tables and SQLite triggers turned up a feed handler that reports success on a failed query, and the run reported it as a possible bug and left it unfixed, though the request allows a fix when a test reveals a bug.

Frequently asked questions

Why did the first test run fail?

The sandbox had no C compiler, and github.com/mattn/go-sqlite3 needs cgo. With CGO_ENABLED=0 the articles and users packages panicked with "go-sqlite3 requires cgo to work". The run installed gcc and reran with CGO_ENABLED=1; the repository was not at fault.

Why does hello.go stay at 18.8%?

The untested part is main(), which starts a server; covering it would need a production refactor, so the run tested Migrate instead.

More Go jobs

The same job on other stacks

Keep reading