-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
67 lines (59 loc) · 2.07 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Package testhelper contains helper functions for doing things you might want
to do in lots of your tests. For instance checking that an error value or a
panic status was as expected.
The functions where a 't *testing.T' is passed will mark themselves as
helpers by calling t.Helper(). These will also report the error themselves.
The testID parameter that many of the helpers take should be a string which
can identify the instance of the test that is being run. For instance, if you
have a slice of test cases you might want this string to include the index of
the test and possibly some descriptive name. It might be created and used as
follows:
testcases := []struct{
name string
...
}{
{
name: "whatever",
},
}
...
for i, tc := range testcases {
tcID := fmt.Sprintf("test %d: %s", i, tc.name)
...
testhelper.SomeFunc(t, tcID, ...)
...
}
Alternatively you can construct the testcase struct with an embedded
testhelper.ID member. Then the testcase ID can be initialised with the MkID
func. The ID string can then be created with the IDStr() func defined on the
testhelper.ID as follows:
testcases := []struct{
testhelper.ID
...
}{
{
ID: testhelper.MkID("whatever"),
},
}
...
for _, tc := range testcases {
tcID := tc.IDStr()
...
testhelper.SomeFunc(t, tcID, ...)
...
}
This way of constructing a test case struct has the advantage that the
constructed ID string gives more information about where the test was
constructed and several of the testhelper functions take a testhelper.ID (or
an interface which it satisfies).
The problem which the testhelper.ID solves is that reporting just the index
number of a test (as in the previous example) imposes work on the
tester such as counting tests to reach the failing test case. Using
testhelper.ID gives you the filename and line number where the ID was
instantiated.
There are additional mixin structs beside ID which allow you to record such
things as whether an error is expected and if so what the error string should
contain.
*/
package testhelper