ut

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2019 License: Apache-2.0 Imports: 10 Imported by: 1

README

ut (utiltest)

Collection of small functions to shorten Go test cases.

Requires Go 1.2 due to the use of testing.TB. If needed, replace with *testing.T at the cost of not being usable in benchmarks.

GoDoc Build Status Coverage Status

Examples

package foo

import (
	"github.com/maruel/ut"
	"log"
	"strconv"
	"testing"
)

func TestItoa(t *testing.T) {
	ut.AssertEqual(t, "42", strconv.Itoa(42))
}

func TestItoaDataListDriven(t *testing.T) {
	data := []struct {
		in       int
		expected string
	}{
		{9, "9"},
		{11, "11"},
	}
	for i, item := range data {
		ut.AssertEqualIndex(t, i, item.expected, strconv.Itoa(item.in))
	}
}

func TestWithLog(t *testing.T) {
	out := ut.NewWriter(t)
	defer out.Close()

	logger := log.New(out, "Foo:", 0)

	// These will be included in the test output only if the test case fails.
	logger.Printf("Q: What is the answer to life the universe and everything?")
	logger.Printf("A: %d", 42)
}

Documentation

Overview

Package ut (for UtilTest) contains testing utilities to shorten unit tests.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEqual

func AssertEqual(t testing.TB, expected, actual interface{})

AssertEqual verifies that two objects are equals and calls FailNow() to immediately cancel the test case.

It must be called from the main goroutine. Other goroutines must call ExpectEqual* flavors.

Equality is determined via reflect.DeepEqual().

Example
// For a func TestXXX(t *testing.T)
t := &testing.T{}
AssertEqual(t, "10", strconv.Itoa(10))
Output:

func AssertEqualIndex

func AssertEqualIndex(t testing.TB, index int, expected, actual interface{})

AssertEqualIndex verifies that two objects are equals and calls FailNow() to immediately cancel the test case.

It must be called from the main goroutine. Other goroutines must call ExpectEqual* flavors.

It is meant to be used in loops where a list of intrant->expected is processed so the assert failure message contains the index of the failing expectation.

Equality is determined via reflect.DeepEqual().

Example
// For a func TestXXX(t *testing.T)
t := &testing.T{}

data := []struct {
	in       int
	expected string
}{
	{9, "9"},
	{11, "11"},
}
for i, item := range data {
	// Call a function to test.
	actual := strconv.Itoa(item.in)
	// Then do an assert as a one-liner.
	AssertEqualIndex(t, i, item.expected, actual)
}
Output:

func AssertEqualf

func AssertEqualf(t testing.TB, expected, actual interface{}, format string, items ...interface{})

AssertEqualf verifies that two objects are equals and calls FailNow() to immediately cancel the test case.

It must be called from the main goroutine. Other goroutines must call ExpectEqual* flavors.

This functions enables specifying an arbitrary string on failure.

Equality is determined via reflect.DeepEqual().

func Decorate

func Decorate(s string) string

Decorate adds a prefix 'file:line: ' to a string, containing the 3 recent callers in the stack.

It skips internal functions. It is mostly meant to be used internally.

It is inspired by testing's decorate().

func ExpectEqual

func ExpectEqual(t testing.TB, expected, actual interface{})

ExpectEqual verifies that two objects are equals and calls Fail() to mark the test case as failed but let it continue.

It is fine to call this function from another goroutine than the main test case goroutine.

Equality is determined via reflect.DeepEqual().

Example
// For a func TestXXX(t *testing.T)
t := &testing.T{}
var wg sync.WaitGroup
wg.Add(1)
go func() {
	defer wg.Done()
	// ExpectEqual* flavors are safe to call in other goroutines.
	ExpectEqual(t, "10", strconv.Itoa(10))
}()
wg.Wait()
Output:

func ExpectEqualIndex

func ExpectEqualIndex(t testing.TB, index int, expected, actual interface{})

ExpectEqualIndex verifies that two objects are equals and calls Fail() to mark the test case as failed but let it continue.

It is fine to call this function from another goroutine than the main test case goroutine.

It is meant to be used in loops where a list of intrant->expected is processed so the assert failure message contains the index of the failing expectation.

Equality is determined via reflect.DeepEqual().

func ExpectEqualf

func ExpectEqualf(t testing.TB, expected, actual interface{}, format string, items ...interface{})

ExpectEqualf verifies that two objects are equals and calls Fail() to mark the test case as failed but let it continue.

It is fine to call this function from another goroutine than the main test case goroutine.

This functions enables specifying an arbitrary string on failure.

Equality is determined via reflect.DeepEqual().

func NewWriter

func NewWriter(t testing.TB) io.WriteCloser

NewWriter adapts a testing.TB into a io.WriteCloser that can be used with to log.SetOutput().

Don't forget to defer foo.Close().

Example
// For a func TestXXX(t *testing.T)
t := &testing.T{}

out := NewWriter(t)
defer out.Close()

logger := log.New(out, "Foo:", 0)

// These will be included in the test output only if the test case fails.
logger.Printf("Q: What is the answer to life the universe and everything?")
logger.Printf("A: %d", 42)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL