Documentation
¶
Overview ¶
Package ut (for UtilTest) contains testing utilities to shorten unit tests.
Index ¶
- func AssertEqual(t testing.TB, expected, actual interface{})
- func AssertEqualIndex(t testing.TB, index int, expected, actual interface{})
- func AssertEqualf(t testing.TB, expected, actual interface{}, format string, ...)
- func Decorate(s string) string
- func ExpectEqual(t testing.TB, expected, actual interface{})
- func ExpectEqualIndex(t testing.TB, index int, expected, actual interface{})
- func ExpectEqualf(t testing.TB, expected, actual interface{}, format string, ...)
- func NewWriter(t testing.TB) io.WriteCloser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEqual ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.