forked from onsi/ginkgo
/
suite_test.go
201 lines (171 loc) · 6.16 KB
/
suite_test.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
package ginkgo
import (
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
. "github.com/onsi/gomega"
"math/rand"
"time"
)
func init() {
Describe("Suite", func() {
var (
specSuite *suite
fakeT *fakeTestingT
fakeR *fakeReporter
)
BeforeEach(func() {
fakeT = &fakeTestingT{}
fakeR = &fakeReporter{}
specSuite = newSuite()
})
Describe("running a suite", func() {
var (
runOrder []string
randomizeAllSpecs bool
randomSeed int64
focusString string
parallelNode int
parallelTotal int
)
var f = func(runText string) func() {
return func() {
runOrder = append(runOrder, runText)
}
}
BeforeEach(func() {
randomizeAllSpecs = false
randomSeed = 11
parallelNode = 1
parallelTotal = 1
focusString = ""
runOrder = make([]string, 0)
specSuite.pushBeforeEachNode(f("top BE"), types.GenerateCodeLocation(0), 0)
specSuite.pushJustBeforeEachNode(f("top JBE"), types.GenerateCodeLocation(0), 0)
specSuite.pushAfterEachNode(f("top AE"), types.GenerateCodeLocation(0), 0)
specSuite.pushContainerNode("container", func() {
specSuite.pushBeforeEachNode(f("BE"), types.GenerateCodeLocation(0), 0)
specSuite.pushJustBeforeEachNode(f("JBE"), types.GenerateCodeLocation(0), 0)
specSuite.pushAfterEachNode(f("AE"), types.GenerateCodeLocation(0), 0)
specSuite.pushItNode("it", f("IT"), flagTypeNone, types.GenerateCodeLocation(0), 0)
specSuite.pushContainerNode("inner container", func() {
specSuite.pushItNode("inner it", f("inner IT"), flagTypeNone, types.GenerateCodeLocation(0), 0)
}, flagTypeNone, types.GenerateCodeLocation(0))
}, flagTypeNone, types.GenerateCodeLocation(0))
specSuite.pushContainerNode("container 2", func() {
specSuite.pushBeforeEachNode(f("BE 2"), types.GenerateCodeLocation(0), 0)
specSuite.pushItNode("it 2", f("IT 2"), flagTypeNone, types.GenerateCodeLocation(0), 0)
}, flagTypeNone, types.GenerateCodeLocation(0))
specSuite.pushItNode("top level it", f("top IT"), flagTypeNone, types.GenerateCodeLocation(0), 0)
})
JustBeforeEach(func() {
specSuite.run(fakeT, "suite description", []Reporter{fakeR}, config.GinkgoConfigType{
RandomSeed: randomSeed,
RandomizeAllSpecs: randomizeAllSpecs,
FocusString: focusString,
ParallelNode: parallelNode,
ParallelTotal: parallelTotal,
})
})
It("provides the config and suite description to the reporter", func() {
Ω(fakeR.config.RandomSeed).Should(Equal(int64(randomSeed)))
Ω(fakeR.config.RandomizeAllSpecs).Should(Equal(randomizeAllSpecs))
Ω(fakeR.beginSummary.SuiteDescription).Should(Equal("suite description"))
})
Measure("should run measurements", func(b Benchmarker) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
runtime := b.Time("sleeping", func() {
sleepTime := time.Duration(r.Float64() * 0.01 * float64(time.Second))
time.Sleep(sleepTime)
})
Ω(runtime.Seconds()).Should(BeNumerically("<=", 0.011))
Ω(runtime.Seconds()).Should(BeNumerically(">=", 0))
randomValue := r.Float64() * 10.0
b.RecordValue("random value", randomValue)
Ω(randomValue).Should(BeNumerically("<=", 10.0))
Ω(randomValue).Should(BeNumerically(">=", 0.0))
}, 10)
It("creates a node hierarchy, converts it to an example collection, and runs it", func() {
Ω(runOrder).Should(Equal([]string{
"top BE", "BE", "top JBE", "JBE", "IT", "AE", "top AE",
"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
"top BE", "BE 2", "top JBE", "IT 2", "top AE",
"top BE", "top JBE", "top IT", "top AE",
}))
})
Context("when told to randomize all examples", func() {
BeforeEach(func() {
randomizeAllSpecs = true
})
It("does", func() {
Ω(runOrder).Should(Equal([]string{
"top BE", "top JBE", "top IT", "top AE",
"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
"top BE", "BE", "top JBE", "JBE", "IT", "AE", "top AE",
"top BE", "BE 2", "top JBE", "IT 2", "top AE",
}))
})
})
Describe("with ginkgo.parallel.total > 1", func() {
BeforeEach(func() {
parallelTotal = 2
randomizeAllSpecs = true
})
Context("for one worker", func() {
BeforeEach(func() {
parallelNode = 1
})
It("should run a subset of tests", func() {
Ω(runOrder).Should(Equal([]string{
"top BE", "top JBE", "top IT", "top AE",
"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
}))
})
})
Context("for another worker", func() {
BeforeEach(func() {
parallelNode = 2
})
It("should run a (different) subset of tests", func() {
Ω(runOrder).Should(Equal([]string{
"top BE", "BE", "top JBE", "JBE", "IT", "AE", "top AE",
"top BE", "BE 2", "top JBE", "IT 2", "top AE",
}))
})
})
})
Context("when provided with a filter", func() {
BeforeEach(func() {
focusString = `inner|\d`
})
It("converts the filter to a regular expression and uses it to filter the running examples", func() {
Ω(runOrder).Should(Equal([]string{
"top BE", "BE", "top JBE", "JBE", "inner IT", "AE", "top AE",
"top BE", "BE 2", "top JBE", "IT 2", "top AE",
}))
})
})
Context("when the specs pass", func() {
It("doesn't report a failure", func() {
Ω(fakeT.didFail).Should(BeFalse())
})
})
Context("when a spec fails", func() {
var location types.CodeLocation
BeforeEach(func() {
specSuite.pushItNode("top level it", func() {
location = types.GenerateCodeLocation(0)
func() { specSuite.fail("oops!", 0) }()
}, flagTypeNone, types.GenerateCodeLocation(0), 0)
})
It("reports a failure", func() {
Ω(fakeT.didFail).Should(BeTrue())
})
It("generates the correct failure data", func() {
Ω(fakeR.exampleSummaries[0].Failure.Message).Should(Equal("oops!"))
Ω(fakeR.exampleSummaries[0].Failure.Location.FileName).Should(Equal(location.FileName))
Ω(fakeR.exampleSummaries[0].Failure.Location.LineNumber).Should(Equal(location.LineNumber + 1))
})
})
})
})
}