testutils

package module
v0.0.0-...-bd7bcab Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 11 Imported by: 0

README

testutils

Golang testutils contains useful utilities for writing golang tests

get it

go get github.com/hlindberg/testutils

use it

Simple case:

Using CheckEqual which treats numerical values as equal irrespective of type if they have the same numerical value. (There are many other "Check" methods available).

import "github.com/hlindberg/testutils"

func TestSomething(t *testing.T) {
    testutils.CheckEqual(1, 1, t)
}

Case with iteration:

import "github.com/hlindberg/testutils"

func TestSomething(t *testing.T) {
    tester := testutils.NewTester(t)
    for i := 0; i <10; i++ {
        tester.At(i).CheckEqual(i, i)
}

Documentation

Overview

Package testutils contains convenient testing checkers that compare a produced value against an expected value (or condition). There are value checks like `CheckEqual(expected, produced, t)“, and checks that should run deferred like `defer ShouldPanic(t)`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsFloat

func AsFloat(v interface{}) (rv float64, ok bool)

AsFloat returns the argument as a 64 bit float and true if the argument is a float that fits into that type. Otherwise it returns 0, false

func AsInteger

func AsInteger(v interface{}) (int64, bool)

AsInteger returns the argument as a signed 64 bit integer and true if the argument is an integer that fits into that type. Otherwise it returns 0, false

func AsInterface

func AsInterface(v interface{}) (rv interface{}, ok bool)

AsInterface returns the argument as an interface{}. This is useful when a value may be a reflect.Value and an operation does not work on such, but on the underlying real value albeit behind an interface{}.

func CheckContainsElements

func CheckContainsElements(expected interface{}, got interface{}, t *testing.T)

CheckContainsElements checks if one slice contains all elements of another slice irrespective of order and uniqueness.

func CheckEqual

func CheckEqual(expected interface{}, got interface{}, t *testing.T)

CheckEqual checks if two values are deeply equal and calls t.Fatalf if not

func CheckEqualAndNoError

func CheckEqualAndNoError(expected interface{}, got interface{}, gotError error, t *testing.T)

CheckEqualAndNoError checks there is no error, and that two values are deeply equal and calls t.Fatalf if not

func CheckEqualElements

func CheckEqualElements(expected interface{}, got interface{}, t *testing.T)

CheckEqualElements checks if two slices contains the exact same set of elements irrespective of order and uniqueness.

func CheckError

func CheckError(got interface{}, t *testing.T)

CheckError checks if there is an error

func CheckFalse

func CheckFalse(got bool, t *testing.T)

CheckFalse checks if value is false

func CheckFileExists

func CheckFileExists(filename string, t *testing.T)

CheckFileExists checks that given file name is for an existing regular file

func CheckFilesEqual

func CheckFilesEqual(file1, file2 string, t *testing.T)

CheckFilesEqual equals checks if the two files have the exact same contents.

func CheckMatches

func CheckMatches(expected interface{}, got string, t *testing.T)

CheckMatches checks expected regular expression is matched by the given string and calls t.Fatalf if not

The expected regular expression can be either a *regexp.Regexp or a string that represents a valid regexp

func CheckNil

func CheckNil(got interface{}, t *testing.T)

CheckNil checks if value is nil

func CheckNotEqual

func CheckNotEqual(expected interface{}, got interface{}, t *testing.T)

CheckNotEqual checks if two values are deeply equal and calls t.Fatalf if not

func CheckNotError

func CheckNotError(got interface{}, t *testing.T)

CheckNotError checks if value is not an error

func CheckNotNil

func CheckNotNil(got interface{}, t *testing.T)

CheckNotNil checks if value is not nil

func CheckNumericGreater

func CheckNumericGreater(expected interface{}, got interface{}, t *testing.T)

CheckNumericGreater checks if second value is greater than first. Comparisons are made regardless of bit size and an integer is equal to a float if casting it to a float makes it equal.

func CheckNumericLess

func CheckNumericLess(expected interface{}, got interface{}, t *testing.T)

CheckNumericLess checks if second value is less than first. Comparisons are made regardless of bit size and an integer is equal to a float if casting it to a float makes it equal.

func CheckTrue

func CheckTrue(got bool, t *testing.T)

CheckTrue checks if value is true

func ShouldNotPanic

func ShouldNotPanic(t *testing.T)

ShouldNotPanic is used to assert that a function does not panic

func ShouldPanic

func ShouldPanic(t *testing.T)

ShouldPanic is used to assert that a function does panic

Types

type Tester

type Tester interface {
	// At sets index and returns this for convenient chaining as tt.At(i).CheckXXX()
	At(index int) Tester
	CheckEqual(expected interface{}, got interface{})
	CheckNotEqual(expected interface{}, got interface{})
	CheckNumericGreater(expected interface{}, got interface{})
	CheckNumericLess(expected interface{}, got interface{})
	CheckEqualAndNoError(expected interface{}, got interface{}, gotError error)
	CheckNil(got interface{})
	CheckNotNil(got interface{})
	CheckError(got error)
	CheckNotError(got error)
	CheckTrue(got bool)
	CheckFalse(got bool)
	CheckAfter(expected, got time.Time, add ...time.Duration)
	CheckAfterOrEqual(expected, got time.Time, add ...time.Duration)
	CheckBefore(expected, got time.Time, add ...time.Duration)
	CheckBeforeOrEqual(expected, got time.Time, add ...time.Duration)
	CheckMatches(expected interface{}, got string)
	Fatalf(fmt string, args ...interface{})
	CheckTruef(predicate bool, fmt string, args ...interface{})
	CheckStringSlicesEqual(expected, got []string)
	CheckTextEqual(expected, got string)
}

Tester describes a testing context which can be modified to output an index for iterative testing

func NewTester

func NewTester(t *testing.T) Tester

NewTester returns a new tester that supports setting the Index

Jump to

Keyboard shortcuts

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