apitest
A simple and extensible behavioural testing library for Go HTTP services. Tests are written the way a client uses the
API: build a request, send it to the handler, and assert on the response. External HTTP calls can be mocked, and every
test can render a sequence diagram of what happened.
In behavioural tests the internal structure of the app is not known by the tests. Data is input to the system and the
outputs are expected to meet certain conditions.
- Fluent builders for requests and expectations
- Works with anything that implements
http.Handler, or against a running server
- Declarative mocks for outbound HTTP calls, with matchers for every part of the request
- Sequence diagrams of each test, including mocked calls and database queries
- No third party dependencies; integrates with
testing, Ginkgo and custom assertion libraries
The API is stable. The library is maintained and issues are addressed; feature requests are considered.
Join the conversation at #apitest on https://gophers.slack.com.
Logo by @egonelbre
Documentation
This README covers the whole library. The same material with more narrative is at
https://apitest.dev, and the API reference is on
pkg.go.dev.
AI coding agents
This repository includes an Agent Skill that teaches coding agents how to write apitest tests,
including the mistakes that are easy to make with mocks and JSONPath. In Claude Code, add the repository as a plugin
marketplace and install the plugin:
claude plugin marketplace add steinfletcher/apitest
claude plugin install apitest@apitest
Inside a session, /plugin marketplace add steinfletcher/apitest followed by /plugin install apitest@apitest does the
same. To use the skill without the plugin, or with another agent that supports Agent Skills, copy
skills/apitest into the agent's skills directory, such as .claude/skills/apitest in your project.
The documentation is also available as Markdown for agents, at apitest.dev/llms-full.txt.
Installation
go get github.com/steinfletcher/apitest
Quick start
func TestGetUser(t *testing.T) {
apitest.New().
Handler(handler).
Get("/user/1234").
Expect(t).
Status(http.StatusOK).
Body(`{"id": "1234", "name": "Tate"}`).
End()
}
Handler takes any http.Handler, so a http.ServeMux, a gin engine, an echo instance and so on all work. Everything
before Expect(t) describes the request; everything after it describes the expected response. End() runs the test.
When the expected body is valid JSON it is compared structurally, so key order and whitespace do not matter.
Demo

Building the request
Method and URL
Get, Post, Put, Patch and Delete set the method and URL. Each has an f variant that formats the URL, and
Method covers anything else.
apitest.Handler(handler).Getf("/user/%s", id)
apitest.Handler(handler).Method(http.MethodOptions).URL("/user")
A ready-made *http.Request can be used instead of the builder.
req := httptest.NewRequest(http.MethodGet, "/user/1234", nil)
apitest.Handler(handler).
HttpRequest(req).
Expect(t).
Status(http.StatusOK).
End()
Headers
apitest.Handler(handler).
Get("/hello").
Header("Authorization", "Bearer token").
Headers(map[string]string{"X-Request-Id": "12345"}).
ContentType("application/json").
Expect(t).
Status(http.StatusOK).
End()
Query parameters
Query, QueryParams and QueryCollection can be combined. QueryCollection sets repeated parameters, so
map[string][]string{"a": {"b", "c", "d"}} is encoded as a=b&a=c&a=d. A query string written in the URL, as in
Get("/hello?page=2"), is kept, and parameters from the builder methods are appended to it.