Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c834d8f

Browse files
authoredAug 24, 2021
Merge pull request #41 from vladimirvivien/environment-test-enhancements
Environment.Test enhancements
2 parents 4d050de + ac21784 commit c834d8f

File tree

8 files changed

+285
-52
lines changed

8 files changed

+285
-52
lines changed
 

‎examples/simple/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
# Simple examples (without test suites)
22

33
As the title implies, this package shows how the test framework can be used
4-
directly in test functions without setting up a test suite in a `TestMain` function.
4+
directly in test functions without setting up a test suite in a `TestMain` function.
5+
6+
```go
7+
func TestHello_WithSetup(t *testing.T) {
8+
e := env.NewWithConfig(envconf.New())
9+
var name string
10+
feat := features.New("Hello Feature").
11+
WithLabel("type", "simple").
12+
Setup(func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
13+
name = "foobar"
14+
return ctx
15+
}).
16+
Assess("test message", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
17+
result := Hello(name)
18+
if result != "Hello foobar" {
19+
t.Error("unexpected message")
20+
}
21+
return ctx
22+
}).Feature()
23+
24+
e.Test(t, feat)
25+
}
26+
```

‎examples/suites/featureset_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package suites
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"sigs.k8s.io/e2e-framework/pkg/envconf"
24+
"sigs.k8s.io/e2e-framework/pkg/features"
25+
)
26+
27+
// TestFeatureSet shows how Environment.Test can be used to
28+
// test a collections of features (feature set). The example
29+
// also shows the before/after feature actions which causes
30+
// callbacks functions to be executed during the feature tests.
31+
func TestFeatureSet(t *testing.T) {
32+
f1 := features.New("bazz test").
33+
Assess("Hello Bazz", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
34+
result := Hello("bazz")
35+
if result != "Hello bazz" {
36+
t.Error("unexpected message")
37+
}
38+
return ctx
39+
}).Feature()
40+
41+
f2 := features.New("batt test").
42+
Assess("Hello Batt", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
43+
result := Hello("batt")
44+
if result != "Hello batt" {
45+
t.Error("unexpected message")
46+
}
47+
return ctx
48+
}).Feature()
49+
50+
testenv.Test(t, f1, f2)
51+
}
File renamed without changes.

‎pkg/env/action.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525

2626
const (
2727
roleSetup = iota
28-
roleBefore
29-
roleAfter
28+
roleBeforeTest
29+
roleBeforeFeature
30+
roleAfterFeature
31+
roleAfterTest
3032
roleFinish
3133
)
3234

‎pkg/env/env.go

+88-25
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func (e *testEnv) WithContext(ctx context.Context) types.Environment {
8686
return env
8787
}
8888

89+
// Setup registers environment operations that are executed once
90+
// prior to the environment being ready and prior to any test.
8991
func (e *testEnv) Setup(funcs ...Func) types.Environment {
9092
if len(funcs) == 0 {
9193
return e
@@ -94,11 +96,43 @@ func (e *testEnv) Setup(funcs ...Func) types.Environment {
9496
return e
9597
}
9698

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 {
98102
if len(funcs) == 0 {
99103
return e
100104
}
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})
102136
return e
103137
}
104138

@@ -114,37 +148,58 @@ func (e *testEnv) BeforeTest(funcs ...Func) types.Environment {
114148
//
115149
// BeforeTest and AfterTest operations are executed before and after
116150
// 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) {
118152
if e.ctx == nil {
119153
panic("context not set") // something is terribly wrong.
120154
}
121155

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()
123163
var err error
124-
for _, action := range befores {
164+
for _, action := range beforeTestActions {
125165
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)
127167
}
128168
}
129169

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)
131183

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+
}
136189
}
137190
}
138-
}
139191

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+
}
143198
}
144-
e.actions = append(e.actions, action{role: roleAfter, funcs: funcs})
145-
return e
146199
}
147200

201+
// Finish registers funcs that are executed at the end of the
202+
// test suite.
148203
func (e *testEnv) Finish(funcs ...Func) types.Environment {
149204
if len(funcs) == 0 {
150205
return e
@@ -165,7 +220,7 @@ func (e *testEnv) Run(m *testing.M) int {
165220
panic("context not set") // something is terribly wrong.
166221
}
167222

168-
setups := e.GetSetupActions()
223+
setups := e.getSetupActions()
169224
// fail fast on setup, upon err exit
170225
var err error
171226
for _, setup := range setups {
@@ -177,7 +232,7 @@ func (e *testEnv) Run(m *testing.M) int {
177232

178233
exitCode := m.Run() // exec test suite
179234

180-
finishes := e.GetFinishActions()
235+
finishes := e.getFinishActions()
181236
// attempt to gracefully clean up.
182237
// Upon error, log and continue.
183238
for _, fin := range finishes {
@@ -205,19 +260,27 @@ func (e *testEnv) getActionsByRole(r actionRole) []action {
205260
return result
206261
}
207262

208-
func (e *testEnv) GetSetupActions() []action {
263+
func (e *testEnv) getSetupActions() []action {
209264
return e.getActionsByRole(roleSetup)
210265
}
211266

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)
214277
}
215278

216-
func (e *testEnv) GetAfterActions() []action {
217-
return e.getActionsByRole(roleAfter)
279+
func (e *testEnv) getAfterTestActions() []action {
280+
return e.getActionsByRole(roleAfterTest)
218281
}
219282

220-
func (e *testEnv) GetFinishActions() []action {
283+
func (e *testEnv) getFinishActions() []action {
221284
return e.getActionsByRole(roleFinish)
222285
}
223286

There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.