-
Notifications
You must be signed in to change notification settings - Fork 7
/
harness.go
367 lines (313 loc) · 8.87 KB
/
harness.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package testdeck
import (
"fmt"
"testing"
"time"
"github.com/mercari/testdeck/constants"
"github.com/mercari/testdeck/deferrer"
"github.com/mercari/testdeck/runner"
)
/*
harness.go: A wrapper around Golang's testing.T because it has a private method that prevents us from implementing it directly
This file is separated into the following sections:
TESTDECK CODE
Test case + lifecycle
Statistics
CODE FROM GOLANG TESTING LIBRARY
*/
// -----
// TESTDECK CODE - Test case + lifecycle
// -----
// TestConfig is for passing special configurations for the test case
type TestConfig struct {
// tests run in parallel by default but you can force it to run in sequential by using ParallelOff = true
ParallelOff bool
}
// TD contains a testdeck test case + statistics to save to the DB later
// It allows us to capture functionality from testing.T
type TD struct {
T TestingT // wrapper on testing.T
fatal bool
currentLifecycle string
statuses []constants.Status // stack of statuses; statuses are emitted by Error/Fatal operation or when the lifecycle completes successfully
timings map[string]constants.Timing
actualName string // name of testdeck test case (to pass to testing.T)
}
// An interface for testdeck test cases; it is implemented by the TestCase struct below
type TestCaseDelegate interface {
ArrangeMethod(t *TD)
ActMethod(t *TD)
AssertMethod(t *TD)
AfterMethod(t *TD)
}
// A struct that represents a testdeck test case
type TestCase struct {
Arrange func(t *TD) // setup stage before the test
Act func(t *TD) // the code you actually want to test
Assert func(t *TD) // the outcomes you want to verify
After func(t *TD) // clean-up steps
deferrer.DefaultDeferrer // deferred steps that you want to run after clean-up
}
// Interface methods
func (tc *TestCase) ArrangeMethod(t *TD) {
timedRun(tc.Arrange, t, constants.LifecycleArrange)
}
func (tc *TestCase) ActMethod(t *TD) {
timedRun(tc.Act, t, constants.LifecycleAct)
}
func (tc *TestCase) AssertMethod(t *TD) {
timedRun(tc.Assert, t, constants.LifecycleAssert)
}
func (tc *TestCase) AfterMethod(t *TD) {
timedRun(tc.After, t, constants.LifecycleAfter)
}
// This method starts the test
// t is the interface for testing.T
// tc is the interface for testdeck test cases
// options is an optional parameter for passing in special test configurations
func Test(t TestingT, tc TestCaseDelegate, options ...TestConfig) *TD {
// FIXME: currently tests cannot be run by matching name
tagged, matched, actualName := runner.MatchTag(t.Name())
// start timer
start := time.Now()
if runner.Initialized() {
r := runner.Instance(nil)
r.LogEvent(fmt.Sprintf("Instantiating: %s", actualName))
}
// initiate testdeck test case
td := &TD{
T: t,
fatal: false,
currentLifecycle: constants.LifecycleTestSetup, // start in the test setup step
timings: make(map[string]constants.Timing),
}
// if test configurations struct was passed, config the settings
if len(options) > 0 {
if options[0].ParallelOff == false {
td.T.Parallel()
}
} else {
// if no configs were passed, turn on parallel by default
td.T.Parallel()
}
// FIXME: currently tests cannot be run by matching name
if tagged {
if !matched {
if runner.Initialized() {
r := runner.Instance(nil)
r.LogEvent("(match workaround) test not in tagged set; skipping")
}
return td
}
td.actualName = actualName
}
arrangeComplete := false
// runs at the end of the test
defer func() {
end := time.Now()
// clean up and set test to finished
if !td.Skipped() || arrangeComplete {
tc.AfterMethod(td)
}
td.currentLifecycle = constants.LifecycleTestFinished
// add the final status so it is clear the test finished
if len(td.statuses) == 0 {
// no failure statuses, set passed
td.setPassed()
} else {
// failure statuses, set failed
td.setFailed(td.fatal)
}
// run deferred functions
if d, ok := tc.(deferrer.Deferrer); ok {
d.RunDeferred()
}
// save statistics to DB
if runner.Initialized() {
r := runner.Instance(nil)
stats := td.makeStatistics(start, end)
r.AddStatistics(stats)
}
}()
tc.ArrangeMethod(td)
arrangeComplete = true
tc.ActMethod(td)
tc.AssertMethod(td)
return td
}
// -----
// Statistics
// -----
// Create a statistics struct for use in saving to DB later
func (c *TD) makeStatistics(start time.Time, end time.Time) *constants.Statistics {
return &constants.Statistics{
Name: c.Name(),
Failed: c.Failed(),
Fatal: c.fatal,
Statuses: c.statuses,
Timings: c.timings,
Start: start,
End: end,
Duration: end.Sub(start),
}
}
// Add result of PASSED lifecycle stage to stack
func (c *TD) setPassed() {
status := constants.Status{
Status: constants.StatusPass,
Lifecycle: c.currentLifecycle,
Fatal: false,
}
c.statuses = append(c.statuses, status)
}
// Add result of FAILED lifecycle stage to stack
func (c *TD) setFailed(fatal bool) {
status := constants.Status{
Status: constants.StatusFail,
Lifecycle: c.currentLifecycle,
Fatal: fatal,
}
c.statuses = append(c.statuses, status)
}
// Add result of SKIPPED lifecycle stage to stack
func (c *TD) setSkipped() {
status := constants.Status{
Status: constants.StatusSkip,
Lifecycle: c.currentLifecycle,
}
c.statuses = append(c.statuses, status)
}
// timedRun executes fn and saves the lifecycle timing to the test case
// fn is the function to run
// t is the current test case
// lifecycle is the current test case step to save timing for
func timedRun(fn func(t *TD), t *TD, lifecycle string) {
t.currentLifecycle = lifecycle
timing := constants.Timing{
Lifecycle: lifecycle,
}
timing.Start = time.Now()
if fn != nil {
timing.Started = true
fn(t) // FIXME what if fn has a goexit (following code needs to be in defer)
timing.Ended = true
}
timing.End = time.Now()
timing.Duration = timing.End.Sub(timing.Start)
t.timings[timing.Lifecycle] = timing
}
// -----
// CODE FROM THE GOLANG TESTING LIBRARY
// -----
// methods from testing.T
type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Name() string
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
Skipped() bool
Helper()
Parallel()
}
// Failed passes through to testing.T.Failed
func (c *TD) Failed() bool {
return c.T.Failed()
}
// Log passes through to testing.T.Log
func (c *TD) Log(args ...interface{}) {
c.T.Log(args...)
}
// Logf passes through to testing.T.Logf
func (c *TD) Logf(format string, args ...interface{}) {
c.T.Logf(format, args...)
}
// Name passes through to testing.T.Name
func (c *TD) Name() string {
// temporary workaround
if c.actualName != "" {
return c.actualName
}
return c.T.Name()
}
// Helper passes through to testing.T.Helper
func (c *TD) Helper() {
c.T.Helper()
}
// Skipped passes through to testing.T.Skipped
func (c *TD) Skipped() bool {
return c.T.Skipped()
}
// Fail passes through to testing.T.Fail
func (c *TD) Fail() {
c.setFailed(false)
c.T.Fail()
}
// Error passes through to testing.T.Error
func (c *TD) Error(args ...interface{}) {
c.T.Helper()
c.setFailed(false)
c.T.Error(args...)
}
// Errorf passes through to testing.T.Errorf
func (c *TD) Errorf(format string, args ...interface{}) {
c.T.Helper()
c.setFailed(false)
c.T.Errorf(format, args...)
}
// Fatal passes through to testing.T.Fatal
func (c *TD) Fatal(args ...interface{}) {
c.T.Helper()
c.setFailed(true)
c.fatal = true
c.T.Fatal(args...)
}
// Fatalf passes through to testing.T.Fatalf
func (c *TD) Fatalf(format string, args ...interface{}) {
c.T.Helper()
c.setFailed(true)
c.fatal = true
c.T.Fatalf(format, args...)
}
// Skip passes through to testing.T.Skip
func (c *TD) Skip(args ...interface{}) {
c.setSkipped()
c.T.Skip(args...)
}
// Skipf passes through to testing.T.Skipf
func (c *TD) Skipf(format string, args ...interface{}) {
c.setSkipped()
c.T.Skipf(format, args...)
}
// SkipNow passes through to testing.T.SkipNow
func (c *TD) SkipNow() {
c.setSkipped()
c.T.SkipNow()
}
// FailNow passes through to testing.T.FailNow
func (c *TD) FailNow() {
c.setFailed(true)
c.fatal = true
c.T.FailNow()
}
// Parallel passes through to testing.T.Parallel
func (c *TD) Parallel() {
c.T.Parallel()
}
// Run passes through to testing.T.Run
func (tc *TestCase) Run(t *testing.T, name string) {
// this method is just a wrapper, some tests might run Test() directly so you should not do anything else here!
// any extra actions you want to do should be added to Test() instead because that method is run every time
t.Run(name, func(t *testing.T) {
// Redirect to Test() to execute testdeck test case
Test(t, tc)
})
}