This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt.go
196 lines (171 loc) · 4.57 KB
/
pt.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Package pt provides functions to run tests in parallel.
You don't have to call t.Parallel() anymore.
# Example
func TestMyFunc(t *testing.T) {
pt.PackageParallel(t,
pt.Test("should do something", func(t *testing.T) {
// test code
}),
pt.Test("should do something else", func(t *testing.T) {
// test code
}),
pt.Group("some condition",
pt.Test("should not do something", func(t *testing.T) {
// test code
}),
pt.Test("should not do something else", func(t *testing.T) {
// test code
}),
),
)
}
You can achieve the same behavior with bare testing package:
func TestMyFunc(t *testing.T) {
t.Parallel()
t.Run("should do something", func(t *testing.T) {
t.Parallel()
// test code
})
t.Run("should do something else", func(t *testing.T) {
t.Parallel()
// test code
})
t.Run("some condition", func(t *testing.T) {
t.Parallel()
t.Run("should not do something", func(t *testing.T) {
t.Parallel()
// test code
})
t.Run("should not do something else", func(t *testing.T) {
t.Parallel()
// test code
})
})
}
# Parallel vs PackageParallel
The difference can be demonstrated with the code below. Tests will be executed in the following sequence:
1. Tests 1-8 run in parallel
2. After that tests 9-12 run in parallel
3. After that tests 13-16 run in parallel
Code:
func TestA(t *testing.T) {
pt.PackageParallel(t, test1, test2)
pt.PackageParallel(t, test3, test4)
}
func TestB(t *testing.T) {
pt.PackageParallel(t, test5, test6)
pt.Parallel(t, test7, test8)
}
func TestC(t *testing.T) {
pt.Parallel(t, test9, test10)
pt.Parallel(t, test11, test12)
}
func TestD(t *testing.T) {
pt.Parallel(t, test13, test14)
pt.Parallel(t, test15, test16)
}
*/
package pt
import (
"reflect"
"testing"
)
/*
PackageParallel is non-blocking function that runs provided tests in parallel with other tests in package.
It can take [Group] and [Test] as arguments.
func TestA(t *testing.T) {
pt.PackageParallel(t, test1, test2)
}
is equivalent to
func TestA(t *testing.T) {
t.Parallel()
t.Run(test1.Name, func(t *testing.T) {
t.Parallel()
test1.F(t)
})
t.Run(test2.Name, func(t *testing.T) {
t.Parallel()
test2.F(t)
})
}
If you don't need to run TestA in parallel with other tests, use [Parallel].
*/
func PackageParallel(t *testing.T, tests ...testing.InternalTest) {
if t == nil {
panic("argument t *testing.T can not be nil")
}
if !alreadyParallel(t) { // avoid panic
t.Parallel()
}
Parallel(t, tests...)
}
/*
Parallel is non-blocking function that runs provided tests in parallel.
It can take [Group] and [Test] as arguments.
func TestA(t *testing.T) {
pt.Parallel(t, test1, test2)
}
is equivalent to
func TestA(t *testing.T) {
t.Run(test1.Name, func(t *testing.T) {
t.Parallel()
test1.F(t)
})
t.Run(test2.Name, func(t *testing.T) {
t.Parallel()
test2.F(t)
})
}
Note that Parallel will not execute different TestXXX and TestYYY in parallel.
For example, test3 and test4 will run in parallel and after that test5 and test6 will run in parallel.
func TestB(t *testing.T) {
pt.Parallel(t, test3, test4)
}
func TestC(t *testing.T) {
pt.Parallel(t, test5, test6)
}
If you need different behavior, use [PackageParallel].
*/
func Parallel(t *testing.T, tests ...testing.InternalTest) {
if t == nil {
panic("argument t *testing.T can not be nil")
}
for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
test.F(t)
})
}
}
// Group is a constructor of [testing.InternalTest].
// It wraps provided tests with a single [testing.InternalTest].
// Provided tests will run in parallel when the wrapper is executed.
// It is designed to be an argument of [Group], [Parallel] and [PackageParallel].
func Group(name string, tests ...testing.InternalTest) testing.InternalTest {
return testing.InternalTest{
Name: name,
F: func(t *testing.T) {
Parallel(t, tests...)
},
}
}
// Test is a simple constructor of [testing.InternalTest].
// It is designed to be an argument of [Group], [Parallel] and [PackageParallel].
func Test(name string, test func(t *testing.T)) testing.InternalTest {
if test == nil {
panic("argument test func(t *testing.T) can not be nil")
}
return testing.InternalTest{
Name: name,
F: test,
}
}
// alreadyParallel returns value of private field isParallel for provided t [*testing.T].
func alreadyParallel(t *testing.T) bool {
testObject := reflect.ValueOf(t).Elem()
isParallelField := testObject.FieldByName("isParallel")
isParallel := isParallelField.Bool()
return isParallel
}