Categorygithub.com/nao1215/spectest
modulepackage
0.1.0
Repository: https://github.com/nao1215/spectest.git
Documentation: pkg.go.dev

# README

Go Reference LinuxUnitTest MacUnitTest WindowsUnitTest UnitTestExampleCodes reviewdog Gosec Coverage

What is spectest?

A simple and extensible behavioral testing library. Supports mocking external http calls and renders sequence diagrams on completion. In behavioral 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.

This project is forked from steinfletcher/apitest apitest was functionally complete. However, I wanted more features, so I decided to fork it to actively develop it further. I will mainly enhance document generation and integration with AWS. There are no plans for compatibility between apitest and spectest. Therefore, future development will include BREAKING CHANGES.

The spectest has its own unique use cases, Use Cases of spectest. Please refer to this document for more information.

Supported OS

  • Linux
  • Mac
  • Windows (Original apitest does not support Windows)

Installation

go get -u github.com/nao1215/spectest

Demo

animated gif

Examples

Framework and library integration examples

ExampleComment
ginpopular martini-like web framework
graphqlusing gqlgen.com to generate a graphql server
gorillathe gorilla web toolkit
iris (broken)iris web framework
echoHigh performance, extensible, minimalist Go web framework
fiberExpress inspired web framework written in Go
httprouterHigh performance HTTP request router that scales well
mocksexample mocking out external http calls
sequence diagramsgenerate sequence diagrams from tests
GinkgoGinkgo BDD test framework
plantumlwxample generating plantuml

Companion libraries (Side projects)

In the original apitest repository, side projects were managed in separate repositories. However, in spectest, these side projects are managed within the same repository. However, the difflib, which operates independently from spectest, and the malfunctioning aws package, are managed in separate repositories.

LibraryComment
JSON PathJSON Path assertion addons
JOSN SchemaJSON Schema assertion addons
CSS SelectorsCSS selector assertion addons
PlantUMLExport sequence diagrams as plantUML
DynamoDB (broken)Add DynamoDB interactions to sequence diagrams

Credits

This library was influenced by the following software packages:

  • YatSpec for creating sequence diagrams from tests
  • MockMVC and superagent for the concept and behavioral testing approach
  • Gock for the approach to mocking HTTP services in Go
  • Baloo for API design

Code snippets

JSON body matcher

func TestApi(t *testing.T) {
	spectest.New().
		Handler(handler).
		Get("/user/1234").
		Expect(t).
		Body(`{"id": "1234", "name": "Tate"}`).
		Status(http.StatusOK).
		End()
}

JSONPath

For asserting on parts of the response body JSONPath may be used. A separate module must be installed which provides these assertions - go get -u github.com/nao1215/spectest/jsonpath. This is packaged separately to keep this library dependency free.

Given the response is {"a": 12345, "b": [{"key": "c", "value": "result"}]}

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		Expect(t).
		Assert(jsonpath.Contains(`$.b[? @.key=="c"].value`, "result")).
		End()
}

and jsonpath.Equals checks for value equality

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		Expect(t).
		Assert(jsonpath.Equal(`$.a`, float64(12345))).
		End()
}

Custom assert functions

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		Expect(t).
		Assert(func(res *http.Response, req *http.Request) error {
			assert.Equal(t, http.StatusOK, res.StatusCode)
			return nil
		}).
		End()
}

Assert cookies

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Patch("/hello").
		Expect(t).
		Status(http.StatusOK).
		Cookies(spectest.Cookie("ABC").Value("12345")).
		CookiePresent("Session-Token").
		CookieNotPresent("XXX").
		Cookies(
			spectest.Cookie("ABC").Value("12345"),
			spectest.Cookie("DEF").Value("67890"),
		).
		End()
}

Assert headers

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		Headers(map[string]string{"ABC": "12345"}).
		End()
}

Mocking external http calls

var getUser = spectest.NewMock().
	Get("/user/12345").
	RespondWith().
	Body(`{"name": "jon", "id": "1234"}`).
	Status(http.StatusOK).
	End()

var getPreferences = spectest.NewMock().
	Get("/preferences/12345").
	RespondWith().
	Body(`{"is_contactable": true}`).
	Status(http.StatusOK).
	End()

func TestApi(t *testing.T) {
	spectest.New().
		Mocks(getUser, getPreferences).
		Handler(handler).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		Body(`{"name": "jon", "id": "1234"}`).
		End()
}

Generating sequence diagrams from tests


func TestApi(t *testing.T) {
	spectest.New().
		Report(spectest.SequenceDiagram()).
		Mocks(getUser, getPreferences).
		Handler(handler).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		Body(`{"name": "jon", "id": "1234"}`).
		End()
}

It is possible to override the default storage location by passing the formatter instance Report(spectest.NewSequenceDiagramFormatter(".sequence-diagrams")). If you want to change the report file name , you use CustomReportName("file name is here") . By default, the hash value becomes the report file name.

You can bring your own formatter too if you want to produce custom output. By default a sequence diagram is rendered on a html page.

The spectest checks the Content Type of the response. If it's an image-related MIME type, the image will be displayed in the report. In the apitest, binary data was being displayed.

DiagramSample

One feature that does not exist in the apitest fork is the ability to output reports in markdown format. The below code snippet is an example of how to output a markdown report.


func TestApi(t *testing.T) {
	spectest.New().
		CustomReportName("markdow_report").
		Report(spectest.SequenceReport(spectest.ReportFormatterConfig{
			Path: "doc",
			Kind: spectest.ReportKindMarkdown,
		})).
		Handler(handler).
		Get("/image").
		Expect(t).
		Body(string(body)).
		Header("Content-Type", "image/png").
		Header("Content-Length", fmt.Sprint(imageInfo.Size())).
		Status(http.StatusOK).
		End()
}

MarkdownReportSample

Debugging http requests and responses generated by api test and any mocks

func TestApi(t *testing.T) {
	spectest.New().
		Debug().
		Handler(handler).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide basic auth in the request

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		BasicAuth("username", "password").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Pass a custom context to the request

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		WithContext(context.TODO()).
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide cookies in the request

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		Cookies(spectest.Cookie("ABC").Value("12345")).
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide headers in the request

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Delete("/hello").
		Headers(map[string]string{"My-Header": "12345"}).
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide query parameters in the request

Query, QueryParams and QueryCollection can all be used in combination

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		QueryParams(map[string]string{"a": "1", "b": "2"}).
		Query("c", "d").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Providing {"a": {"b", "c", "d"} results in parameters encoded as a=b&a=c&a=d. QueryCollection can be used in combination with Query

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Get("/hello").
		QueryCollection(map[string][]string{"a": {"b", "c", "d"}}).
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide a url encoded form body in the request

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Post("/hello").
		FormData("a", "1").
		FormData("b", "2").
		FormData("b", "3").
		FormData("c", "4", "5", "6").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Provide a multipart/form-data

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Post("/hello").
		MultipartFormData("a", "1", "2").
		MultipartFile("file", "path/to/some.file1", "path/to/some.file2").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Capture the request and response data

func TestApi(t *testing.T) {
	spectest.New().
		Observe(func(res *http.Response, req *http.Request, specTest *spectest.SpecTest) {
			// do something with res and req
		}).
		Handler(handler).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Intercept the request

This is useful for mutating the request before it is sent to the system under test.

func TestApi(t *testing.T) {
	spectest.Handler(handler).
		Intercept(func(req *http.Request) {
			req.URL.RawQuery = "a[]=xxx&a[]=yyy"
		}).
		Get("/hello").
		Expect(t).
		Status(http.StatusOK).
		End()
}

Contributing

View the contributing guide.

# Packages

No description provided by the author
Package selector provides a set of functions for css selector based assertions.
No description provided by the author
Package image provides assertions for image comparison.
Package jsonpath provides assertions for jsonpath expressions.
Package jsonschema provides a spectest.Assert function to validate the http response body against the provided json schema.
No description provided by the author
Package plantuml write plantuml markup to a writer.
Package version manage spectest command version.
No description provided by the author

# Functions

FromHTTPCookie transforms an http cookie into a Cookie.
Handler is a convenience method for creating a new spectest with a handler.
HandlerFunc is a convenience method for creating a new spectest with a handler func.
New creates a new api test.
NewCookie creates a new Cookie with the provided name.
NewHTTPRequestLogEntry creates a new LogEntry from a http.Request.
NewHTTPResponseLogEntry creates a new LogEntry from a http.Response.
NewInterval creates a new interval.
NewMock create a new mock, ready for configuration using the builder pattern.
NewStandaloneMocks create a series of StandaloneMocks.
NewTestRecorder creates a new TestRecorder.
SequenceDiagram produce a sequence diagram at the given path or .sequence by default.
SequenceReport produce a sequence diagram at the given path or .sequence by default.

# Constants

ConsumerDefaultName default consumer name.
ReportKindHTML is the HTML report kind.
ReportKindMarkdown is the Markdown report kind.
SystemUnderTestDefaultName default name for system under test.

# Variables

ErrTimeout is an error that indicates a timeout.
IsClientError is a convenience function to assert on a range of client error status codes.
IsServerError is a convenience function to assert on a range of server error status codes.
IsSuccess is a convenience function to assert on a range of happy path status codes.

# Structs

Cookie used to represent an http cookie.
DefaultVerifier is a verifier that uses some code from https://github.com/stretchr/testify to perform assertions.
GraphQLRequestBody represents the POST request body as per the GraphQL spec.
No description provided by the author
No description provided by the author
Interval represents a time interval.
LogEntry represents a single log entry that is used to generate the web sequence diagram.
MarkdownFormatter implementation of a ReportFormatter.
No description provided by the author
No description provided by the author
Meta represents the meta data for the report.
Mock represents the entire interaction for a mock to be used for testing.
MockRequest represents the http request side of a mock interaction.
MockResponse represents the http response side of a mock interaction.
NoopVerifier is a verifier that does not perform verification.
No description provided by the author
ReportFormatterConfig is the configuration for a ReportFormatter.
Request is the user defined request that will be invoked on the handler under test.
Response is the user defined expected response from the application under test.
Result provides the final result.
No description provided by the author
SpecTest is the top level struct holding the test spec.
StandaloneMocks for using mocks outside of API tests context.
Transport wraps components used to observe and manipulate the real request and response objects.
UnmatchedMock exposes some information about mocks that failed to match a request.

# Interfaces

No description provided by the author
No description provided by the author
TestingT is an interface to wrap the native *testing.T interface, this allows integration with GinkgoT() interface GinkgoT interface defined in https://github.com/onsi/ginkgo/blob/55c858784e51c26077949c81b6defb6b97b76944/ginkgo_dsl.go#L91.
Verifier is the assertion interface allowing consumers to inject a custom assertion implementation.

# Type aliases

Assert is a user defined custom assertion function.
Intercept will be called before the request is made.
Matcher type accepts the actual request and a mock request to match against.
Mocks is a slice of Mock.
Observe will be called by with the request and response on completion.
ReportKind is the kind of the report.