@@ -86,6 +86,8 @@ func (e *testEnv) WithContext(ctx context.Context) types.Environment {
86
86
return env
87
87
}
88
88
89
+ // Setup registers environment operations that are executed once
90
+ // prior to the environment being ready and prior to any test.
89
91
func (e * testEnv ) Setup (funcs ... Func ) types.Environment {
90
92
if len (funcs ) == 0 {
91
93
return e
@@ -94,11 +96,43 @@ func (e *testEnv) Setup(funcs ...Func) types.Environment {
94
96
return e
95
97
}
96
98
97
- func (e * testEnv ) BeforeTest (funcs ... Func ) types.Environment {
99
+ // BeforeEachTest registers environment funcs that are executed
100
+ // before each Env.Test(...)
101
+ func (e * testEnv ) BeforeEachTest (funcs ... Func ) types.Environment {
98
102
if len (funcs ) == 0 {
99
103
return e
100
104
}
101
- e .actions = append (e .actions , action {role : roleBefore , funcs : funcs })
105
+ e .actions = append (e .actions , action {role : roleBeforeTest , funcs : funcs })
106
+ return e
107
+ }
108
+
109
+ // BeforeEachFeature registers step functions that are executed
110
+ // before each Feature is tested during env.Test call.
111
+ func (e * testEnv ) BeforeEachFeature (funcs ... Func ) types.Environment {
112
+ if len (funcs ) == 0 {
113
+ return e
114
+ }
115
+ e .actions = append (e .actions , action {role : roleBeforeFeature , funcs : funcs })
116
+ return e
117
+ }
118
+
119
+ // AfterEachFeature registers step functions that are executed
120
+ // after each feature is tested during an env.Test call.
121
+ func (e * testEnv ) AfterEachFeature (funcs ... Func ) types.Environment {
122
+ if len (funcs ) == 0 {
123
+ return e
124
+ }
125
+ e .actions = append (e .actions , action {role : roleAfterFeature , funcs : funcs })
126
+ return e
127
+ }
128
+
129
+ // AfterEachTest registers environment funcs that are executed
130
+ // after each Env.Test(...).
131
+ func (e * testEnv ) AfterEachTest (funcs ... Func ) types.Environment {
132
+ if len (funcs ) == 0 {
133
+ return e
134
+ }
135
+ e .actions = append (e .actions , action {role : roleAfterTest , funcs : funcs })
102
136
return e
103
137
}
104
138
@@ -114,37 +148,58 @@ func (e *testEnv) BeforeTest(funcs ...Func) types.Environment {
114
148
//
115
149
// BeforeTest and AfterTest operations are executed before and after
116
150
// the feature is tested respectively.
117
- func (e * testEnv ) Test (t * testing.T , feature types.Feature ) {
151
+ func (e * testEnv ) Test (t * testing.T , testFeatures ... types.Feature ) {
118
152
if e .ctx == nil {
119
153
panic ("context not set" ) // something is terribly wrong.
120
154
}
121
155
122
- befores := e .GetBeforeActions ()
156
+ if len (testFeatures ) == 0 {
157
+ t .Log ("No test testFeatures provided, skipping test" )
158
+ return
159
+ }
160
+
161
+ // execute the beforeTest functions
162
+ beforeTestActions := e .getBeforeTestActions ()
123
163
var err error
124
- for _ , action := range befores {
164
+ for _ , action := range beforeTestActions {
125
165
if e .ctx , err = action .run (e .ctx , e .cfg ); err != nil {
126
- t .Fatalf ("BeforeTest failure: %s: %v" , feature . Name () , err )
166
+ t .Fatalf ("BeforeEachTest failure: %s" , err )
127
167
}
128
168
}
129
169
130
- e .ctx = e .execFeature (e .ctx , t , feature )
170
+ // execute each feature
171
+ beforeFeatureActions := e .getBeforeFeatureActions ()
172
+ afterFeatureActions := e .getAfterFeatureActions ()
173
+ for _ , feature := range testFeatures {
174
+ // execute beforeFeature actions
175
+ for _ , action := range beforeFeatureActions {
176
+ if e .ctx , err = action .run (e .ctx , e .cfg ); err != nil {
177
+ t .Fatalf ("BeforeEachTest failure: %s" , err )
178
+ }
179
+ }
180
+
181
+ // execute feature test
182
+ e .ctx = e .execFeature (e .ctx , t , feature )
131
183
132
- afters := e .GetAfterActions ()
133
- for _ , action := range afters {
134
- if e .ctx , err = action .run (e .ctx , e .cfg ); err != nil {
135
- t .Fatalf ("AfterTest failure: %s: %v" , feature .Name (), err )
184
+ // execute beforeFeature actions
185
+ for _ , action := range afterFeatureActions {
186
+ if e .ctx , err = action .run (e .ctx , e .cfg ); err != nil {
187
+ t .Fatalf ("BeforeEachTest failure: %s" , err )
188
+ }
136
189
}
137
190
}
138
- }
139
191
140
- func (e * testEnv ) AfterTest (funcs ... Func ) types.Environment {
141
- if len (funcs ) == 0 {
142
- return e
192
+ // execute afterTest functions
193
+ afterTestActions := e .getAfterTestActions ()
194
+ for _ , action := range afterTestActions {
195
+ if e .ctx , err = action .run (e .ctx , e .cfg ); err != nil {
196
+ t .Fatalf ("AfterEachTest failure: %s" , err )
197
+ }
143
198
}
144
- e .actions = append (e .actions , action {role : roleAfter , funcs : funcs })
145
- return e
146
199
}
147
200
201
+ // Finish registers funcs that are executed at the end of the
202
+ // test suite.
148
203
func (e * testEnv ) Finish (funcs ... Func ) types.Environment {
149
204
if len (funcs ) == 0 {
150
205
return e
@@ -165,7 +220,7 @@ func (e *testEnv) Run(m *testing.M) int {
165
220
panic ("context not set" ) // something is terribly wrong.
166
221
}
167
222
168
- setups := e .GetSetupActions ()
223
+ setups := e .getSetupActions ()
169
224
// fail fast on setup, upon err exit
170
225
var err error
171
226
for _ , setup := range setups {
@@ -177,7 +232,7 @@ func (e *testEnv) Run(m *testing.M) int {
177
232
178
233
exitCode := m .Run () // exec test suite
179
234
180
- finishes := e .GetFinishActions ()
235
+ finishes := e .getFinishActions ()
181
236
// attempt to gracefully clean up.
182
237
// Upon error, log and continue.
183
238
for _ , fin := range finishes {
@@ -205,19 +260,27 @@ func (e *testEnv) getActionsByRole(r actionRole) []action {
205
260
return result
206
261
}
207
262
208
- func (e * testEnv ) GetSetupActions () []action {
263
+ func (e * testEnv ) getSetupActions () []action {
209
264
return e .getActionsByRole (roleSetup )
210
265
}
211
266
212
- func (e * testEnv ) GetBeforeActions () []action {
213
- return e .getActionsByRole (roleBefore )
267
+ func (e * testEnv ) getBeforeTestActions () []action {
268
+ return e .getActionsByRole (roleBeforeTest )
269
+ }
270
+
271
+ func (e * testEnv ) getBeforeFeatureActions () []action {
272
+ return e .getActionsByRole (roleBeforeFeature )
273
+ }
274
+
275
+ func (e * testEnv ) getAfterFeatureActions () []action {
276
+ return e .getActionsByRole (roleAfterFeature )
214
277
}
215
278
216
- func (e * testEnv ) GetAfterActions () []action {
217
- return e .getActionsByRole (roleAfter )
279
+ func (e * testEnv ) getAfterTestActions () []action {
280
+ return e .getActionsByRole (roleAfterTest )
218
281
}
219
282
220
- func (e * testEnv ) GetFinishActions () []action {
283
+ func (e * testEnv ) getFinishActions () []action {
221
284
return e .getActionsByRole (roleFinish )
222
285
}
223
286
0 commit comments