Getting Started

Overview

apitest is a simple and extensible testing library for the Go programming language.. You can use apitest to simplify testing of REST services, HTTP handlers and HTTP clients.

Features

Mock external http calls

Render sequence diagrams on test completion

Extensible - supports various injection points

GraphQL support

Custom assert functions and mock matchers

JSON path assertions, css selector assertions and more

Installation

go get -u github.com/steinfletcher/apitest

or

import "github.com/steinfletcher/apitest"

apitest follows semantic versioning and releases are managed using Github releases.

Anatomy of a test

A test consists of three main parts

Configuration
defines the http.handler that will be tested and any specific test configurations, such as mocks, debug mode and reporting
Request
defines the test input. This is typically a http request
Expectations
defines how the application under test should respond. This is typically a http response
package main

import (
	"net/http"
	"testing"

	"github.com/steinfletcher/apitest"
)

func TestGetMessage(t *testing.T) {
	handler := func(w http.ResponseWriter, r *http.Request) {
		msg := `{"message": "hello"}`
		_, _ = w.Write([]byte(msg))
		w.WriteHeader(http.StatusOK)
	}

	apitest.New(). // configuration
			HandlerFunc(handler).
			Get("/message"). // request
			Expect(t).       // expectations
			Body(`{"message": "hello"}`).
			Status(http.StatusOK).
			End()
}