Skip to content

Repository files navigation

tt

Simple and colorful test tools

Build Status CircleCI Status codecov Go Report Card Go Reference Release Join the chat at https://gitter.im/go-ego/ego

Installation/Update

go get -u github.com/vcaesar/tt

Usage:

package tt

import (
	"fmt"
	"testing"

	"github.com/vcaesar/tt"
	"github.com/vcaesar/tt/example"
)

func TestAdd(t *testing.T) {
	fmt.Println(add.Add(1, 1))

	tt.Expect(t, "1", add.Add(1, 1))
	tt.Expect(t, "2", add.Add(1, 1))

	tt.Equal(t, 1, add.Add(1, 1))
	tt.Equal(t, 2, add.Add(1, 1))

	at := tt.New(t)
	at.Expect("2", add.Add(1, 1))
	at.Equal(2, add.Add(1, 1))
}

func Benchmark1(b *testing.B) {
	at := tt.New(b)
	fn := func() {
		at.Equal(2, add.Add(1, 1))
	}

	tt.BM(b, fn)
	// at.BM(b, fn)
}

func Benchmark2(b *testing.B) {
	at := tt.New(b)
	for i := 0; i < b.N; i++ {
		at.Equal(2, Add(1, 1))
	}
}

Mock

github.com/vcaesar/tt/mock is a small mock object system. Embed mock.Mock in a fake, register expectations with On(...).Return(...), and forward calls with Called(...):

package store

import (
	"errors"
	"testing"

	"github.com/vcaesar/tt"
	"github.com/vcaesar/tt/mock"
)

type Store struct{ mock.Mock }

func (s *Store) Get(id int) (string, error) {
	args := s.Called(id)
	return args.String(0), args.Error(1)
}

func TestStore(t *testing.T) {
	s := new(Store)
	s.On("Get", 1).Return("one", nil).Once()
	s.On("Get", mock.Anything).Return("", errors.New("not found"))

	v, err := s.Get(1)
	tt.Equal(t, "one", v)
	tt.Nil(t, err)

	s.AssertExpectations(t)
	s.AssertCalled(t, "Get", 1)
	s.AssertNumberOfCalls(t, "Get", 1)
}

Argument matchers: mock.Anything, mock.AnythingOfType("int"), mock.MatchedBy(func(v T) bool). Call options: Once(), Twice(), Times(n), Run(func(mock.Arguments)).

mock.T is a tt.TestingT that records failures instead of reporting them, for testing assertions that are expected to fail:

mt := new(mock.T)
tt.Equal(mt, 1, 2)
tt.True(t, mt.Failed())

HTTP

github.com/vcaesar/tt/http tests http.Handlers in-process with net/http/httptest. Build a request, Run() it, and assert on the response:

package api

import (
	"testing"

	"github.com/vcaesar/tt"
	tthttp "github.com/vcaesar/tt/http"
)

func TestUsers(t *testing.T) {
	r := tthttp.New(handler).Get("/users").Param("id", "1").Run()
	tthttp.Status(t, 200, r)
	tthttp.Header(t, "Content-Type", "application/json", r)
	tthttp.JSON(t, `{"id":1,"name":"alice"}`, r)

	tthttp.New(handler).Post("/users").JSON(map[string]string{"name": "bob"}).
		Set("Authorization", "Bearer x").Run().
		Expect(t).Status(201).Contains("bob").Cookie("session", "s1")

	var u struct{ ID int }
	tt.Nil(t, r.JSON(&u))
}

Request builders: Get/Post/Put/Patch/Delete/Head/Options/Do, Set/Headers, Param/Params, Cookie, Text/Bytes/Reader/JSON/Form. Assertions: Status, Success, Redirect, Error, Body, Contains, NotContains, Header, Cookie, JSON (structural), each also available on r.Expect(t) as a chain ending in OK(). Fetch(h, method, path, values) mirrors testify's helper signature.

Thanks

Testify, the code has some inspiration.

About

Simple and colorful test tools

Topics

Resources

Contributing

Stars

10 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages