Skip to content
A simple assertion library for go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assert
example
th
.gitignore
LICENSE
Makefile
README.md

README.md

Trial GoDoc GoReport

A simple assertion library for go. Also see schema for easier JSON Schema testing.

Motivation

go-test is fine. We do not need big testing frameworks for most projects. One thing its lacking though is simple assertions and ability to implement simple helpers as it does not allow us to skip test logs.

Trial gives us th.Error(t testingT, skip int, msgs ...interface{}) allowing to skip callers to implement helpers with nice logging.

For most uses, the trial/assert package is enough, giving us the most basic assertions needed with nice error messages.

trial/assert Usage

Simple equals

import "github.com/jgroeneveld/trial/assert"

assert.Equal(t, 1, 2)

Output:
unit_test.go:42: Not equal:
		Expected: 1
		  Actual: 2

Additional arguments to overwrite the message

assert.Equal(t, 1, 2, "numbers dont match for %q", "my param")

Output:
unit_test.go:42: numbers dont match for "my param":
		Expected: 1
		  Actual: 2

Type problems are made clear

assert.Equal(t, 1, int64(1))

unit_test.go:42: Not equal:
		Expected: 1
		  Actual: 1
		   Types: Expected:int, Actual:int64

See example/example_test.go for more.

asserter

If you dont want to pass t into every call, you can bind t with Asserter.

	asserter := assert.Asserter(t)

	asserter.Equal(1, 2, "numbers dont match")
	asserter.NotEqual(1, 1, "numbers match")

trial/th Usage

th can be used to write simple own assertions. This for example gives you a wrapper for schema.MatchJSON to have simple JSON schema assertions in your tests:

func AssertJSONSchema(t *testing.T, matcher schema.Matcher, r io.Reader) {
	err := schema.MatchJSON(matcher, r)
	if err != nil {
		th.Error(t, 1, err.Error())
	}
}

func MustMatchJSONSchema(t *testing.T, matcher schema.Matcher, r io.Reader) {
	err := schema.MatchJSON(matcher, r)
	if err != nil {
		th.Error(t, 1, err.Error())
		t.FailNow()
	}
}
You can’t perform that action at this time.