-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgo_test.cli.txt
150 lines (115 loc) · 7.99 KB
/
go_test.cli.txt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
┏━━━━━━━━━━━━━┓
┃ GO_TEST ┃
┗━━━━━━━━━━━━━┛
VERSION ==> #See Go language
RELATED DOCUMENTATION ==> #See Go language
SNAPSHOT TESTING ==> #See go_examples documentation
DATA-DRIVEN TESTING ==> #See testing_quick documentation
TEST COVERAGE ==> #See go_test_cover documentation
BENCHMARKING ==> #See go_test_bench documentation
┌───────────┐
│ TESTS │
└───────────┘
TEST FILES ==> #Must export Test*|Example*|Benchmark*()
#Each FUNC is run in its own goroutine
TestNAME(*testing.T) #Test function
import "testing" #
testing.T #Test function utilities
T.Name()->"TestNAME" #
T.Helper() #Make function as test helper, i.e. file|line information not printed
┌────────────────┐
│ ASSERTIONS │
└────────────────┘
T.Fail() #Mark test as failed. Keep current FUNC|goroutine going.
T.Failed()->BOOL #
T.FailNow() #Run T.Fail(), then exit current FUNC|goroutine (not others)
┌───────────────────┐
│ ORCHESTRATION │
└───────────────────┘
T.Cleanup(FUNC()) #Run FUNC() when test exits.
#Last added is called first.
TestMain(*testing.M) #When defined:
# - must explicitly call M.Run() to run other Test*(), i.e. allow beforeAll logic
# - must explicitly call os.Exit(EXIT_CODE_INT), i.e. allow afterAll logic
M.Run()->EXIT_CODE_INT #
T.Run("NAME2", FUNC(*T))->BOOL #Runs a sub-test, for nesting|grouping tests.
#Done in a goroutine.
#Blocks until complete (unless parallel)
#BOOL is whether failed
# - if parallel, until T.Parallel() was called
#With -run|bench, sub-test name is PARENT_NAME/NAME2
# - running only a specific sub-test also runs its parent once
┌───────────────┐
│ SELECTION │
└───────────────┘
go test [IMPORT_PATHS] #If IMPORT_PATHS is omitted:
# - current PACKAGE
# - no caching
# - imply -v
-run REGEXP #Only run tests whose "TestNAME" partially matches REGEXP
#"" means ".*"
testdata/** #Always ignored, i.e. good for fixtures
T.SkipNow() #Mark test as skipped, then exit current FUNC|goroutine (not others)
T.Skipped()->BOOL #
go test -short #
testing.Short()->BOOL #Returns whether -short CLI flag is set
#To use in long tests, to T.Skip() them when that flag is set
┌───────────────┐
│ EXECUTION │
└───────────────┘
go test [IMPORT_PATHS] [ARG...] #Build TEST_BINARY using go_build *.go *_test.go
# - i.e. TEST_BINARY includes the source BINARY
#Then executes TEST_BINARY ARG...
# - cached:
# - if files or CLI flags changed, not cached
# - convention to disable caching: using -count=1
-test.FLAG #Under the hood, CLI flags are passed to TEST_BINARY as -test.*
#They can be specified to go test either as -* or as -test.*
#Any -FLAG can be set after IMPORT_PATHS too, so should not be any conflicts
# - to distinguish, can prepend -args to ARG...
-c #Do not execute TEST_BINARY
-o PATH #TEST_BINARY's location.
#Def: BINARY.test
-ANY_GO_RUN_FLAG #Note: -i implied -c
-list REGEXP #Prints all exported functions whose name partially match REGEXP
go test -failfast #Stops on first test failure
┌───────────────┐
│ REPORTING │
└───────────────┘
DEFAULT REPORTING ==> #Prints test summary to stdout (redirects any stderr to stdout instead)
#If any test assertion fails, exit code 1
T.Log[f](...) #Similar to fmt.Println|Printf(...) except more test-friendly:
# - only printed on test failure or if -v CLI flag
T.Error[f](...) #Run T.Log[f](...) then t.Fail()
T.Fatal[f](...) #Run T.Log[f](...) then t.FailNow()
T.Skip[f](...) #Run T.Log[f](...) then t.SkipNow()
go test -v #Verbose. Force printing logs from all tests
T.Verbose()->BOOL #
go test -json #Prints test results as JSON
go test -OPT... | #Make go test output JSON stream instead.
go tool test2json #Includes verbose information:
# - test start|pass|fail
# - test pause|continue
# - test skip
# - test output|benchmark
go tool test2json TEST_BINARY
-test.OPT... #Same
-t #Adds OBJ.Time 'TIMESTAMP' to each OBJ
-p PACKAGE #Adds OBJ.Package 'PACKAGE' to each OBJ
┌───────────┐
│ SPEED │
└───────────┘
go test -timeout DURATION #Def: "10m"
┌─────────────────┐
│ PARALLELISM │
└─────────────────┘
go test -cpu NUM #Set GOMAXPROCS
go test -parallel NUM #Maximum of parallel tests at once (def: GOMAXPROCS)
T.Parallel() #Mark as parallel test, i.e. run in parallel with other parallel tests
#Noop unless -parallel CLI flag
┌─────────────┐
│ LINTING │
└─────────────┘
go test -vet=RULE,... #Run go vet RULEs
#Can be "off"
#Def: only bools|stringintconv|nilfunc|ifaceassert|atomic|printf|buildtag