generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from vladimirvivien/table-driven-tests
Support for table-driven test representation
- Loading branch information
Showing
5 changed files
with
164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/coverage.html | ||
/coverage.out | ||
.DS_Store | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Table-Driven Tests | ||
This directory contains examples that show how the test framework can be used to define table-driven tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package table | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"sigs.k8s.io/e2e-framework/pkg/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/features" | ||
) | ||
|
||
var test = env.New() | ||
func TestMain(m *testing.M){ | ||
// Setup the rand number source and a limit | ||
test.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) { | ||
rnd := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
return context.WithValue(context.WithValue(ctx, "limit", rand.Int31n(255)), "randsrc", rnd), nil | ||
}) | ||
|
||
// Don't forget to launch the package test | ||
os.Exit(test.Run(m)) | ||
} | ||
|
||
func TestTableDriven(t *testing.T) { | ||
// feature 1 | ||
table0 := features.Table{ | ||
{ | ||
Name: "less than equal 64", | ||
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type | ||
lim := ctx.Value("limit").(int32) // check type assertion | ||
if rnd.Int31n(lim) > 64 { | ||
t.Log("limit should be less than 64") | ||
} | ||
return ctx | ||
}, | ||
}, | ||
{ | ||
Name: "more than than equal 128", | ||
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type | ||
lim := ctx.Value("limit").(int32) // check type assertion | ||
if rnd.Int31n(lim) > 128 { | ||
t.Log("limit should be less than 128") | ||
} | ||
return ctx | ||
}, | ||
}, | ||
{ | ||
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type | ||
lim := ctx.Value("limit").(int32) // check type assertion | ||
if rnd.Int31n(lim) > 256 { | ||
t.Log("limit should be less than 256") | ||
} | ||
return ctx | ||
}, | ||
}, | ||
}.Build("Random numbers").Feature() | ||
|
||
// feature 2 | ||
table1 := features.Table{ | ||
{ | ||
Name: "A simple feature", | ||
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
rnd := ctx.Value("randsrc").(*rand.Rand) | ||
if rnd.Int() > 100 { | ||
t.Log("this is a great number") | ||
} | ||
return ctx | ||
}, | ||
}, | ||
} | ||
|
||
test.Test(t, table0, table1.Build().Feature() ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package features | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Table provides a structure for table-driven tests. | ||
// Each entry in the table represents an executable assessment. | ||
type Table []struct { | ||
Name string | ||
Assessment Func | ||
} | ||
|
||
// Build converts the defined test steps in the table | ||
// into a FeatureBuilder which can be used to add additional attributes | ||
// to the feature before it's exercised. Build takes an optional feature name | ||
// if omitted will be generated. | ||
func (table Table) Build(featureName ...string) *FeatureBuilder { | ||
var name string | ||
if len(featureName) > 0 { | ||
name = featureName[0] | ||
} | ||
f := New(name) | ||
for i, test := range table { | ||
if test.Name == "" { | ||
test.Name = fmt.Sprintf("Assessment-%d", i) | ||
} | ||
if test.Assessment != nil { | ||
f.Assess(test.Name, test.Assessment) | ||
} | ||
} | ||
return f | ||
} |