| 1 |
[](https://pkg.go.dev/github.com/frankban/quicktest#section-documentation) |
| 2 |
[](https://github.com/frankban/quicktest/actions/workflows/ci.yaml) |
| 3 |
|
| 4 |
[//]: # (Generated with: godocdown -template=.godocdown.template -o README.md) |
| 5 |
|
| 6 |
### quicktest |
| 7 |
|
| 8 |
`go get github.com/frankban/quicktest@latest` |
| 9 |
|
| 10 |
Package quicktest provides a collection of Go helpers for writing tests. |
| 11 |
|
| 12 |
Quicktest helpers can be easily integrated inside regular Go tests, for |
| 13 |
instance: |
| 14 |
|
| 15 |
import qt "github.com/frankban/quicktest" |
| 16 |
|
| 17 |
func TestFoo(t *testing.T) { |
| 18 |
t.Run("numbers", func(t *testing.T) { |
| 19 |
c := qt.New(t) |
| 20 |
numbers, err := somepackage.Numbers() |
| 21 |
c.Assert(err, qt.IsNil) |
| 22 |
c.Assert(numbers, qt.DeepEquals, []int{42, 47}) |
| 23 |
}) |
| 24 |
t.Run("bad wolf error", func(t *testing.T) { |
| 25 |
c := qt.New(t) |
| 26 |
numbers, err := somepackage.Numbers() |
| 27 |
c.Assert(err, qt.ErrorMatches, "bad wolf") |
| 28 |
}) |
| 29 |
t.Run("nil", func(t *testing.T) { |
| 30 |
c := qt.New(t) |
| 31 |
got := somepackage.MaybeNil() |
| 32 |
c.Assert(got, qt.IsNil, qt.Commentf("value: %v", somepackage.Value)) |
| 33 |
}) |
| 34 |
} |
| 35 |
|
| 36 |
### Assertions |
| 37 |
|
| 38 |
An assertion looks like this, where qt.Equals could be replaced by any available |
| 39 |
checker. If the assertion fails, the underlying Fatal method is called to |
| 40 |
describe the error and abort the test. |
| 41 |
|
| 42 |
c := qt.New(t) |
| 43 |
c.Assert(someValue, qt.Equals, wantValue) |
| 44 |
|
| 45 |
If you don’t want to abort on failure, use Check instead, which calls Error |
| 46 |
instead of Fatal: |
| 47 |
|
| 48 |
c.Check(someValue, qt.Equals, wantValue) |
| 49 |
|
| 50 |
For really short tests, the extra line for instantiating *qt.C can be avoided: |
| 51 |
|
| 52 |
qt.Assert(t, someValue, qt.Equals, wantValue) |
| 53 |
qt.Check(t, someValue, qt.Equals, wantValue) |
| 54 |
|
| 55 |
The library provides some base checkers like Equals, DeepEquals, Matches, |
| 56 |
ErrorMatches, IsNil and others. More can be added by implementing the Checker |
| 57 |
interface. Below, we list the checkers implemented by the package in |
| 58 |
alphabetical order. |
| 59 |
|
| 60 |
### All |
| 61 |
|
| 62 |
All returns a Checker that uses the given checker to check elements of slice or |
| 63 |
array or the values of a map. It succeeds if all elements pass the check. On |
| 64 |
failure it prints the error from the first index that failed. |
| 65 |
|
| 66 |
For example: |
| 67 |
|
| 68 |
c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0) |
| 69 |
c.Assert([][]string{{"a", "b"}, {"a", "b"}}, qt.All(qt.DeepEquals), []string{"c", "d"}) |
| 70 |
|
| 71 |
See also Any and Contains. |
| 72 |
|
| 73 |
### Any |
| 74 |
|
| 75 |
Any returns a Checker that uses the given checker to check elements of a slice |
| 76 |
or array or the values from a map. It succeeds if any element passes the check. |
| 77 |
|
| 78 |
For example: |
| 79 |
|
| 80 |
c.Assert([]int{3,5,7,99}, qt.Any(qt.Equals), 7) |
| 81 |
c.Assert([][]string{{"a", "b"}, {"c", "d"}}, qt.Any(qt.DeepEquals), []string{"c", "d"}) |
| 82 |
|
| 83 |
See also All and Contains. |
| 84 |
|
| 85 |
### CmpEquals |
| 86 |
|
| 87 |
CmpEquals checks equality of two arbitrary values according to the provided |
| 88 |
compare options. DeepEquals is more commonly used when no compare options are |
| 89 |
required. |
| 90 |
|
| 91 |
Example calls: |
| 92 |
|
| 93 |
c.Assert(list, qt.CmpEquals(cmpopts.SortSlices), []int{42, 47}) |
| 94 |
c.Assert(got, qt.CmpEquals(), []int{42, 47}) // Same as qt.DeepEquals. |
| 95 |
|
| 96 |
### CodecEquals |
| 97 |
|
| 98 |
CodecEquals returns a checker that checks for codec value equivalence. |
| 99 |
|
| 100 |
func CodecEquals( |
| 101 |
marshal func(interface{}) ([]byte, error), |
| 102 |
unmarshal func([]byte, interface{}) error, |
| 103 |
opts ...cmp.Option, |
| 104 |
) Checker |
| 105 |
|
| 106 |
It expects two arguments: a byte slice or a string containing some |
| 107 |
codec-marshaled data, and a Go value. |
| 108 |
|
| 109 |
It uses unmarshal to unmarshal the data into an interface{} value. It marshals |
| 110 |
the Go value using marshal, then unmarshals the result into an interface{} |
| 111 |
value. |
| 112 |
|
| 113 |
It then checks that the two interface{} values are deep-equal to one another, |
| 114 |
using CmpEquals(opts) to perform the check. |
| 115 |
|
| 116 |
See JSONEquals for an example of this in use. |
| 117 |
|
| 118 |
### Contains |
| 119 |
|
| 120 |
Contains checks that a map, slice, array or string contains a value. It's the |
| 121 |
same as using Any(Equals), except that it has a special case for strings - if |
| 122 |
the first argument is a string, the second argument must also be a string and |
| 123 |
strings.Contains will be used. |
| 124 |
|
| 125 |
For example: |
| 126 |
|
| 127 |
c.Assert("hello world", qt.Contains, "world") |
| 128 |
c.Assert([]int{3,5,7,99}, qt.Contains, 7) |
| 129 |
|
| 130 |
### ContentEquals |
| 131 |
|
| 132 |
ContentEquals is is like DeepEquals but any slices in the compared values will |
| 133 |
be sorted before being compared. |
| 134 |
|
| 135 |
For example: |
| 136 |
|
| 137 |
c.Assert([]string{"c", "a", "b"}, qt.ContentEquals, []string{"a", "b", "c"}) |
| 138 |
|
| 139 |
### DeepEquals |
| 140 |
|
| 141 |
DeepEquals checks that two arbitrary values are deeply equal. The comparison is |
| 142 |
done using the github.com/google/go-cmp/cmp package. When comparing structs, by |
| 143 |
default no exported fields are allowed. If a more sophisticated comparison is |
| 144 |
required, use CmpEquals (see below). |
| 145 |
|
| 146 |
Example call: |
| 147 |
|
| 148 |
c.Assert(got, qt.DeepEquals, []int{42, 47}) |
| 149 |
|
| 150 |
### Equals |
| 151 |
|
| 152 |
Equals checks that two values are equal, as compared with Go's == operator. |
| 153 |
|
| 154 |
For instance: |
| 155 |
|
| 156 |
c.Assert(answer, qt.Equals, 42) |
| 157 |
|
| 158 |
Note that the following will fail: |
| 159 |
|
| 160 |
c.Assert((*sometype)(nil), qt.Equals, nil) |
| 161 |
|
| 162 |
Use the IsNil checker below for this kind of nil check. |
| 163 |
|
| 164 |
### ErrorAs |
| 165 |
|
| 166 |
ErrorAs checks that the error is or wraps a specific error type. If so, it |
| 167 |
assigns it to the provided pointer. This is analogous to calling errors.As. |
| 168 |
|
| 169 |
For instance: |
| 170 |
|
| 171 |
// Checking for a specific error type |
| 172 |
c.Assert(err, qt.ErrorAs, new(*os.PathError)) |
| 173 |
|
| 174 |
// Checking fields on a specific error type |
| 175 |
var pathError *os.PathError |
| 176 |
if c.Check(err, qt.ErrorAs, &pathError) { |
| 177 |
c.Assert(pathError.Path, Equals, "some_path") |
| 178 |
} |
| 179 |
|
| 180 |
### ErrorIs |
| 181 |
|
| 182 |
ErrorIs checks that the error is or wraps a specific error value. This is |
| 183 |
analogous to calling errors.Is. |
| 184 |
|
| 185 |
For instance: |
| 186 |
|
| 187 |
c.Assert(err, qt.ErrorIs, os.ErrNotExist) |
| 188 |
|
| 189 |
### ErrorMatches |
| 190 |
|
| 191 |
ErrorMatches checks that the provided value is an error whose message matches |
| 192 |
the provided regular expression. |
| 193 |
|
| 194 |
For instance: |
| 195 |
|
| 196 |
c.Assert(err, qt.ErrorMatches, `bad wolf .*`) |
| 197 |
|
| 198 |
### HasLen |
| 199 |
|
| 200 |
HasLen checks that the provided value has the given length. |
| 201 |
|
| 202 |
For instance: |
| 203 |
|
| 204 |
c.Assert([]int{42, 47}, qt.HasLen, 2) |
| 205 |
c.Assert(myMap, qt.HasLen, 42) |
| 206 |
|
| 207 |
### Implements |
| 208 |
|
| 209 |
Implements checks that the provided value implements an interface. The interface |
| 210 |
is specified with a pointer to an interface variable. |
| 211 |
|
| 212 |
For instance: |
| 213 |
|
| 214 |
var rc io.ReadCloser |
| 215 |
c.Assert(myReader, qt.Implements, &rc) |
| 216 |
|
| 217 |
### IsFalse |
| 218 |
|
| 219 |
IsFalse checks that the provided value is false. The value must have a boolean |
| 220 |
underlying type. |
| 221 |
|
| 222 |
For instance: |
| 223 |
|
| 224 |
c.Assert(false, qt.IsFalse) |
| 225 |
c.Assert(IsValid(), qt.IsFalse) |
| 226 |
|
| 227 |
### IsNil |
| 228 |
|
| 229 |
IsNil checks that the provided value is nil. |
| 230 |
|
| 231 |
For instance: |
| 232 |
|
| 233 |
c.Assert(got, qt.IsNil) |
| 234 |
|
| 235 |
As a special case, if the value is nil but implements the error interface, it is |
| 236 |
still considered to be non-nil. This means that IsNil will fail on an error |
| 237 |
value that happens to have an underlying nil value, because that's invariably a |
| 238 |
mistake. See https://golang.org/doc/faq#nil_error. |
| 239 |
|
| 240 |
So it's just fine to check an error like this: |
| 241 |
|
| 242 |
c.Assert(err, qt.IsNil) |
| 243 |
|
| 244 |
### IsNotNil |
| 245 |
|
| 246 |
IsNotNil is a Checker checking that the provided value is not nil. IsNotNil is |
| 247 |
the equivalent of qt.Not(qt.IsNil) |
| 248 |
|
| 249 |
For instance: |
| 250 |
|
| 251 |
c.Assert(got, qt.IsNotNil) |
| 252 |
|
| 253 |
### IsTrue |
| 254 |
|
| 255 |
IsTrue checks that the provided value is true. The value must have a boolean |
| 256 |
underlying type. |
| 257 |
|
| 258 |
For instance: |
| 259 |
|
| 260 |
c.Assert(true, qt.IsTrue) |
| 261 |
c.Assert(myBoolean(false), qt.IsTrue) |
| 262 |
|
| 263 |
### JSONEquals |
| 264 |
|
| 265 |
JSONEquals checks whether a byte slice or string is JSON-equivalent to a Go |
| 266 |
value. See CodecEquals for more information. |
| 267 |
|
| 268 |
It uses DeepEquals to do the comparison. If a more sophisticated comparison is |
| 269 |
required, use CodecEquals directly. |
| 270 |
|
| 271 |
For instance: |
| 272 |
|
| 273 |
c.Assert(`{"First": 47.11}`, qt.JSONEquals, &MyStruct{First: 47.11}) |
| 274 |
|
| 275 |
### Matches |
| 276 |
|
| 277 |
Matches checks that a string or result of calling the String method (if the |
| 278 |
value implements fmt.Stringer) matches the provided regular expression. |
| 279 |
|
| 280 |
For instance: |
| 281 |
|
| 282 |
c.Assert("these are the voyages", qt.Matches, `these are .*`) |
| 283 |
c.Assert(net.ParseIP("1.2.3.4"), qt.Matches, `1.*`) |
| 284 |
|
| 285 |
### Not |
| 286 |
|
| 287 |
Not returns a Checker negating the given Checker. |
| 288 |
|
| 289 |
For instance: |
| 290 |
|
| 291 |
c.Assert(got, qt.Not(qt.IsNil)) |
| 292 |
c.Assert(answer, qt.Not(qt.Equals), 42) |
| 293 |
|
| 294 |
### PanicMatches |
| 295 |
|
| 296 |
PanicMatches checks that the provided function panics with a message matching |
| 297 |
the provided regular expression. |
| 298 |
|
| 299 |
For instance: |
| 300 |
|
| 301 |
c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, `bad wolf .*`) |
| 302 |
|
| 303 |
### Satisfies |
| 304 |
|
| 305 |
Satisfies checks that the provided value, when used as argument of the provided |
| 306 |
predicate function, causes the function to return true. The function must be of |
| 307 |
type func(T) bool, having got assignable to T. |
| 308 |
|
| 309 |
For instance: |
| 310 |
|
| 311 |
// Check that an error from os.Open satisfies os.IsNotExist. |
| 312 |
c.Assert(err, qt.Satisfies, os.IsNotExist) |
| 313 |
|
| 314 |
// Check that a floating point number is a not-a-number. |
| 315 |
c.Assert(f, qt.Satisfies, math.IsNaN) |
| 316 |
|
| 317 |
### Deferred Execution |
| 318 |
|
| 319 |
The testing.TB.Cleanup helper provides the ability to defer the execution of |
| 320 |
functions that will be run when the test completes. This is often useful for |
| 321 |
creating OS-level resources such as temporary directories (see c.Mkdir). |
| 322 |
|
| 323 |
When targeting Go versions that don't have Cleanup (< 1.14), the same can be |
| 324 |
achieved using c.Defer. In this case, to trigger the deferred behavior, calling |
| 325 |
c.Done is required. For instance, if you create a *C instance at the top level, |
| 326 |
you’ll have to add a defer to trigger the cleanups at the end of the test: |
| 327 |
|
| 328 |
defer c.Done() |
| 329 |
|
| 330 |
However, if you use quicktest to create a subtest, Done will be called |
| 331 |
automatically at the end of that subtest. For example: |
| 332 |
|
| 333 |
func TestFoo(t *testing.T) { |
| 334 |
c := qt.New(t) |
| 335 |
c.Run("subtest", func(c *qt.C) { |
| 336 |
c.Setenv("HOME", c.Mkdir()) |
| 337 |
// Here $HOME is set the path to a newly created directory. |
| 338 |
// At the end of the test the directory will be removed |
| 339 |
// and HOME set back to its original value. |
| 340 |
}) |
| 341 |
} |
| 342 |
|
| 343 |
The c.Patch, c.Setenv, c.Unsetenv and c.Mkdir helpers use t.Cleanup for cleaning |
| 344 |
up resources when available, and fall back to Defer otherwise. |
| 345 |
|
| 346 |
For a complete API reference, see the |
| 347 |
[package documentation](https://pkg.go.dev/github.com/frankban/quicktest#section-documentation). |