You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Line comments on the line you call most `test` functions on will be shown in failure messages as additional context
48
+
### Add Additional Context
62
49
63
-
That means you can have additional context in the failure message, as well as helpful comments explaining the assertion to readers of your code
50
+
`test` provides a number of options to decorate your test log with useful context:
64
51
65
52
```go
66
-
funcTestSomething(t *testing.T) {
67
-
test.Equal(t, "apples", "oranges") // Fruits are not equal
53
+
funcTestDetail(t *testing.T) {
54
+
test.Equal(t, "apples", "oranges", test.Title("Fruit scramble!"), test.Context("Apples are not oranges!"))
68
55
}
69
56
```
70
57
71
-
Will get you a failure message like:
58
+
Will get you an error log in the test that looks like this...
72
59
73
-
```shell
74
-
--- FAIL: TestSomething (0.00s)
75
-
something_test.go:1:
76
-
Not Equal // Fruits are not equal
77
-
---------
60
+
```plaintext
61
+
--- FAIL: TestDemo (0.00s)
62
+
test_test.go:501:
63
+
Fruit scramble!
64
+
---------------
65
+
78
66
Got: apples
79
67
Wanted: oranges
68
+
69
+
(Apples are not oranges!)
70
+
71
+
FAIL
80
72
```
81
73
82
74
### Non Comparable Types
83
75
84
-
`test` uses Go 1.18+ generics under the hood for most of the comparison, which is great, but what if your types don't satisfy `comparable`. We also provide
76
+
`test` uses generics under the hood for most of the comparison, which is great, but what if your types don't satisfy `comparable`. We also provide
85
77
`test.EqualFunc` and `test.NotEqualFunc` for those exact situations!
86
78
87
79
These allow you to pass in a custom comparator function for your type, if your comparator function returns true, the types are considered equal.
test.EqualFunc(t, a, b, slices.Equal[string]) // Also passes :)
93
+
// Can also use any function here
94
+
test.EqualFunc(t, a, b, slices.Equal) // Also passes :)
103
95
104
-
test.EqualFunc(t, a, c, slices.Equal[string]) // Fails
96
+
test.EqualFunc(t, a, c, slices.Equal) // Fails
105
97
}
106
98
```
107
99
108
100
You can also use this same pattern for custom user defined types, structs etc.
109
101
110
-
### Rich Comparison
111
-
112
-
Large structs or long slices can often be difficult to compare using `reflect.DeepEqual`, you have to scan for the difference yourself. `test` provides a
113
-
`test.Diff` function that produces a rich text diff for you on failure:
114
-
115
-
```go
116
-
funcTestDiff(t *testing.T) {
117
-
// Pretend these are very long, or are large structs
118
-
a:= []string{"hello", "world"}
119
-
b:= []string{"hello", "there"}
120
-
121
-
test.Diff(t, a, b)
122
-
}
123
-
```
124
-
125
-
Will give you:
126
-
127
-
```plain
128
-
--- FAIL: TestDiff (0.00s)
129
-
main_test.go:14: Mismatch (-want, +got):
130
-
[]string{
131
-
"hello",
132
-
- "there",
133
-
+ "world",
134
-
}
135
-
```
136
-
137
102
### Table Driven Tests
138
103
139
104
Table driven tests are great! But when you test errors too it can get a bit awkward, you have to do the `if (err != nil) != tt.wantErr` thing and I personally
Under the hood `CaptureOutput` temporarily captures both streams, copies the data to a buffer and returns the output back to you, before cleaning everything back up again.
234
199
235
-
### Golden Files
236
-
237
-
`test` has great support for golden files:
238
-
239
-
```go
240
-
funcTestFile(t *testing.T) {
241
-
got:="some contents\n"
242
-
want:= filepath.Join(test.Data(t), "golden.txt")
243
-
244
-
test.File(t, got, want)
245
-
}
246
-
```
247
-
248
-
This will read the file, normalise line endings and then generate an output almost like a git diff:
249
-
250
-
```patch
251
-
--- want
252
-
+++ got
253
-
@@ -1 +1 @@
254
-
-some file contents
255
-
+some contents
256
-
```
257
-
258
200
### Credits
259
201
260
202
This package was created with [copier] and the [FollowTheProcess/go_copier] project template.
0 commit comments