github.com/frankban/quicktest (go): Package quicktest provides a collection of Go helpers for writing tests.  Quicktest helpers can be easily integrated inside regular Go tests, for instance:  An assertion looks like this, where qt.Equals could be replaced by any available checker. If the assertion fails, the underlying Fatal method is called to describe the error and abort the test.  If you don’t want to abort on failure, use Check instead, which calls Error instead of Fatal:  For really short tests, the extra line for instantiating *qt.C can be avoided:  The library provides some base checkers like Equals, DeepEquals, Matches, ErrorMatches, IsNil and others. More can be added by implementing the Checker interface. Below, we list the checkers implemented by the package in alphabetical order.  All returns a Checker that uses the given checker to check elements of slice or array or the values of a map. It succeeds if all elements pass the check. On failure it prints the error from the first index that failed.  For example:  See also Any and Contains.  Any returns a Checker that uses the given checker to check elements of a slice or array or the values from a map. It succeeds if any element passes the check.  For example:  See also All and Contains.  CmpEquals checks equality of two arbitrary values according to the provided compare options. DeepEquals is more commonly used when no compare options are required.  Example calls:  CodecEquals returns a checker that checks for codec value equivalence.  It expects two arguments: a byte slice or a string containing some codec-marshaled data, and a Go value.  It uses unmarshal to unmarshal the data into an interface{} value. It marshals the Go value using marshal, then unmarshals the result into an interface{} value.  It then checks that the two interface{} values are deep-equal to one another, using CmpEquals(opts) to perform the check.  See JSONEquals for an example of this in use.  Contains checks that a map, slice, array or string contains a value. It's the same as using Any(Equals), except that it has a special case for strings - if the first argument is a string, the second argument must also be a string and strings.Contains will be used.  For example:  ContentEquals is is like DeepEquals but any slices in the compared values will be sorted before being compared.  For example:  DeepEquals checks that two arbitrary values are deeply equal. The comparison is done using the github.com/google/go-cmp/cmp package. When comparing structs, by default no exported fields are allowed. If a more sophisticated comparison is required, use CmpEquals (see below).  Example call:  Equals checks that two values are equal, as compared with Go's == operator.  For instance:  Note that the following will fail:  Use the IsNil checker below for this kind of nil check.  ErrorAs checks that the error is or wraps a specific error type. If so, it assigns it to the provided pointer. This is analogous to calling errors.As.  For instance:  ErrorIs checks that the error is or wraps a specific error value. This is analogous to calling errors.Is.  For instance:  ErrorMatches checks that the provided value is an error whose message matches the provided regular expression.  For instance:  HasLen checks that the provided value has the given length.  For instance:  Implements checks that the provided value implements an interface. The interface is specified with a pointer to an interface variable.  For instance:  IsFalse checks that the provided value is false. The value must have a boolean underlying type.  For instance:  IsNil checks that the provided value is nil.  For instance:  As a special case, if the value is nil but implements the error interface, it is still considered to be non-nil. This means that IsNil will fail on an error value that happens to have an underlying nil value, because that's invariably a mistake. See https://golang.org/doc/faq#nil_error.  So it's just fine to check an error like this:  IsNotNil is a Checker checking that the provided value is not nil. IsNotNil is the equivalent of qt.Not(qt.IsNil)  For instance:  IsTrue checks that the provided value is true. The value must have a boolean underlying type.  For instance:  JSONEquals checks whether a byte slice or string is JSON-equivalent to a Go value. See CodecEquals for more information.  It uses DeepEquals to do the comparison. If a more sophisticated comparison is required, use CodecEquals directly.  For instance:  Matches checks that a string or result of calling the String method (if the value implements fmt.Stringer) matches the provided regular expression.  For instance:  Not returns a Checker negating the given Checker.  For instance:  PanicMatches checks that the provided function panics with a message matching the provided regular expression.  For instance:  Satisfies checks that the provided value, when used as argument of the provided predicate function, causes the function to return true. The function must be of type func(T) bool, having got assignable to T.  For instance:  The testing.TB.Cleanup helper provides the ability to defer the execution of functions that will be run when the test completes. This is often useful for creating OS-level resources such as temporary directories (see c.Mkdir).  When targeting Go versions that don't have Cleanup (< 1.14), the same can be achieved using c.Defer. In this case, to trigger the deferred behavior, calling c.Done is required. For instance, if you create a *C instance at the top level, you’ll have to add a defer to trigger the cleanups at the end of the test:  However, if you use quicktest to create a subtest, Done will be called automatically at the end of that subtest. For example:  The c.Patch, c.Setenv, c.Unsetenv and c.Mkdir helpers use t.Cleanup for cleaning up resources when available, and fall back to Defer otherwise.
Keywords: assertions|go|golang|library|testing