Categorygithub.com/hlindberg/testutils
modulepackage
0.0.0-20230831202759-bd7bcabafe55
Repository: https://github.com/hlindberg/testutils.git
Documentation: pkg.go.dev

# 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)
}

# Functions

AsFloat returns the argument as a 64 bit float and true if the argument is a float that fits into that type.
AsInteger returns the argument as a signed 64 bit integer and true if the argument is an integer that fits into that type.
AsInterface returns the argument as an interface{}.
CheckContainsElements checks if one slice contains all elements of another slice irrespective of order and uniqueness.
CheckEqual checks if two values are deeply equal and calls t.Fatalf if not.
CheckEqualAndNoError checks there is no error, and that two values are deeply equal and calls t.Fatalf if not.
CheckEqualElements checks if two slices contains the exact same set of elements irrespective of order and uniqueness.
CheckError checks if there is an error.
CheckFalse checks if value is false.
CheckFileExists checks that given file name is for an existing regular file.
CheckFilesEqual equals checks if the two files have the exact same contents.
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.
CheckNil checks if value is nil.
CheckNotEqual checks if two values are deeply equal and calls t.Fatalf if not.
CheckNotError checks if value is not an error.
CheckNotNil checks if value is not nil.
CheckNumericGreater checks if second value is greater than first.
CheckNumericLess checks if second value is less than first.
CheckTrue checks if value is true.
NewTester returns a new tester that supports setting the Index.
ShouldNotPanic is used to assert that a function does not panic.
ShouldPanic is used to assert that a function does panic.

# Interfaces

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