Skip to content

Commit 027a72b

Browse files
authored
Add grouped tests under a new package called 'testy' (#161)
* add grouped tests * do not run tests when tags are specified, add skip reason msg * rename functions
1 parent bab6ede commit 027a72b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

testy/tagged.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package testy
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
const (
10+
TagUnit = "unit"
11+
TagIntegration = "integration"
12+
TagPostgres = "postgres"
13+
TagRabbit = "rabbit"
14+
)
15+
16+
// TaggedTestsEnvVar defines the name of the environment variable for the tagged tests.
17+
// Example:
18+
// TaggedTestsEnvVar="TEST_TAGS"
19+
// env TEST_TAGS="unit" go test ./...
20+
var TaggedTestsEnvVar = "TEST_TAGS"
21+
22+
// RequireTestTag runs the test if the provided tag matches at least one runtime tag.
23+
// Example:
24+
// func TestSomething(t *testing.T) {
25+
// RequireTestTag(t, "unit")
26+
// ...
27+
// }
28+
// Run with:
29+
// env TEST_TAGS="unit,integration" go test ./...
30+
func RequireTestTag(t *testing.T, testTag string) {
31+
if !getRuntimeTags().contains(testTag) {
32+
t.Skipf("skipping test '%s', requires '%s' tag", t.Name(), testTag)
33+
}
34+
}
35+
36+
// RequireOneOfTestTags runs the test if any of the provided test tags matches one of the runtime tags.
37+
func RequireOneOfTestTags(t *testing.T, testTags ...string) {
38+
if !getRuntimeTags().containsAny(testTags...) {
39+
t.Skipf("skipping test '%s', requires at least one of the following tags: '%s'",
40+
t.Name(), strings.Join(testTags, ", "))
41+
}
42+
}
43+
44+
// RequireAllTestTags runs the test if all the provided test tags appear in runtime tags.
45+
func RequireAllTestTags(t *testing.T, testTags ...string) {
46+
if !getRuntimeTags().containsAll(testTags...) {
47+
t.Skipf("skipping test '%s', requires all of the following tags: '%s'",
48+
t.Name(), strings.Join(testTags, ", "))
49+
}
50+
}
51+
52+
type runtimeTags []string
53+
54+
func getRuntimeTags() runtimeTags {
55+
return parseTags(os.Getenv(TaggedTestsEnvVar))
56+
}
57+
58+
func parseTags(rawTags string) runtimeTags {
59+
rawTags = strings.ReplaceAll(rawTags, " ", "")
60+
if rawTags == "" {
61+
return nil
62+
}
63+
return strings.Split(rawTags, ",")
64+
}
65+
66+
func (rt runtimeTags) contains(targetTag string) bool {
67+
for _, tag := range rt {
68+
if tag == targetTag {
69+
return true
70+
}
71+
}
72+
return false
73+
}
74+
75+
func (rt runtimeTags) containsAny(targetTags ...string) bool {
76+
for _, targetTag := range targetTags {
77+
if rt.contains(targetTag) {
78+
return true
79+
}
80+
}
81+
return false
82+
}
83+
84+
func (rt runtimeTags) containsAll(targetTags ...string) bool {
85+
for _, targetTag := range targetTags {
86+
if !rt.contains(targetTag) {
87+
return false
88+
}
89+
}
90+
return true
91+
}

testy/tagged_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package testy
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestContainsMethods(t *testing.T) {
10+
rt := parseTags("unit,integration")
11+
12+
assert.True(t, rt.contains("unit"))
13+
assert.True(t, rt.contains("integration"))
14+
assert.False(t, rt.contains(""))
15+
assert.False(t, rt.contains("UNIT"))
16+
17+
assert.True(t, rt.containsAll("unit"))
18+
assert.True(t, rt.containsAll("unit", "integration"))
19+
assert.False(t, rt.containsAll("unit", "integration", "something-else"))
20+
assert.False(t, rt.containsAll("unit", "integration", ""))
21+
22+
assert.True(t, rt.containsAny("unit", "something-else"))
23+
assert.True(t, rt.containsAny("whatever", "unit", "something-else"))
24+
assert.True(t, rt.containsAny("whatever", "unit", "something-else", "integration"))
25+
assert.False(t, rt.containsAny("whatever", "", "something-else"))
26+
}

0 commit comments

Comments
 (0)