Categorygithub.com/go-quicktest/qt
modulepackage
1.101.0
Repository: https://github.com/go-quicktest/qt.git
Documentation: pkg.go.dev

# README

qt: quicker Go tests

go get github.com/go-quicktest/qt

Package qt provides a collection of Go helpers for writing tests. It uses generics, so requires Go 1.18 at least.

For a complete API reference, see the package documentation.

Quicktest helpers can be easily integrated inside regular Go tests, for instance:

    import "github.com/go-quicktest/qt"

    func TestFoo(t *testing.T) {
        t.Run("numbers", func(t *testing.T) {
            numbers, err := somepackage.Numbers()
            qt.Assert(t, qt.DeepEquals(numbers, []int{42, 47})
            qt.Assert(t, qt.ErrorMatches(err, "bad wolf"))
        })
        t.Run("nil", func(t *testing.T) {
            got := somepackage.MaybeNil()
            qt.Assert(t, qt.IsNil(got), qt.Commentf("value: %v", somepackage.Value))
        })
    }

Assertions

An assertion looks like this, where qt.Equals could be replaced by any available checker. If the assertion fails, the underlying t.Fatal method is called to describe the error and abort the test.

qt.Assert(t, qt.Equals(someValue, wantValue))

If you don’t want to abort on failure, use Check instead, which calls Error instead of Fatal:

qt.Check(t, qt.Equals(someValue, wantValue))

The library provides some base checkers like Equals, DeepEquals, Matches, ErrorMatches, IsNil and others. More can be added by implementing the Checker interface.

Other helpers

The Patch helper makes it a little more convenient to change a global or other variable for the duration of a test.

# Packages

Package qtsuite allows quicktest to run test suites.

# Functions

Assert checks that the provided argument passes the given check and calls tb.Fatal otherwise, including any Comment arguments in the failure.
BadCheckf returns an error used to report a problem with the checker invocation or testing execution itself (like wrong number or type of arguments) rather than a real Check or Assert failure.
Check checks that the provided argument passes the given check and calls tb.Error otherwise, including any Comment arguments in the failure.
CmpEquals is like DeepEquals but allows custom compare options to be passed too, to allow unexported fields to be compared.
CodecEquals returns a Checker that checks for codec value equivalence.
Commentf returns a test comment whose output is formatted according to the given format specifier and args.
ContentEquals is like DeepEquals but any slices in the compared values will be sorted before being compared.
DeepEquals returns a Checker checking equality of two values using cmp.DeepEqual.
Equals returns a Checker checking equality of two comparable values.
ErrorAs retruns a Checker checking that the error is or wraps a specific error type.
ErrorIs returns a Checker that checks that the error is or wraps a specific error value.
ErrorMatches returns a Checker checking that the provided value is an error whose message matches the provided regular expression pattern.
F2 factors a 2-argument checker function into a single argument function suitable for passing to an *Any or *All checker.
Format formats the given value as a string.
HasLen returns a Checker checking that the provided value has the given length.
Implements returns a Checker checking that the provided value implements the interface specified by the type parameter.
IsBadCheck reports whether the given error has been created by BadCheckf.
IsFalse returns a Checker checking that the provided value is false.
IsNil returns a Checker checking that the provided value is equal to nil.
IsNotNil returns a Checker checking that the provided value is not nil.
IsTrue returns a Checker checking that the provided value is true.
JSONEquals returns a Checker that checks whether a string or byte slice is JSON-equivalent to a Go value.
MapAll returns a Checker that uses checkers returned by f to check values of a map.
MapAny returns a Checker that uses checkers returned by f to check values of a map.
MapContains returns a Checker that succeeds if the given value is contained in the values of the given map, by comparing for equality.
Matches returns a Checker checking that the provided string matches the provided regular expression pattern.
Not returns a Checker negating the given Checker.
PanicMatches returns a Checker checking that the provided function panics with a message matching the provided regular expression pattern.
Patch sets a variable to a temporary value for the duration of the test.
Satisfies returns a Checker checking that the provided value, when used as argument of the provided predicate function, causes the function to return true.
SliceAll returns a Checker that uses checkers returned by f to check elements of a slice.
SliceAny returns a Checker that uses the given checker to check elements of a slice.
SliceContains returns a Checker that succeeds if the given slice contains the given element, by comparing for equality.
StringContains returns a Checker checking that the given string contains the given substring.

# Variables

ErrSilent is the error used when there is no need to include in the failure output the "error" and "check" keys and all the keys automatically added for args.

# Structs

Arg holds a single argument to a checker.
Comment represents additional information on a check or an assertion which is displayed when the check or assertion fails.
SuppressedIfLong indicates that the value must be suppressed if verbose testing is off and the pretty printed version of the value is long.

# Interfaces

Checker is implemented by types used as part of Check/Assert invocations.

# Type aliases

Unquoted indicates that the string must not be pretty printed in the failure output.