forked from trustwallet/go-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add grouped tests under a new package called 'testy' (trustwallet#161)
* add grouped tests * do not run tests when tags are specified, add skip reason msg * rename functions
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package testy | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const ( | ||
TagUnit = "unit" | ||
TagIntegration = "integration" | ||
TagPostgres = "postgres" | ||
TagRabbit = "rabbit" | ||
) | ||
|
||
// TaggedTestsEnvVar defines the name of the environment variable for the tagged tests. | ||
// Example: | ||
// TaggedTestsEnvVar="TEST_TAGS" | ||
// env TEST_TAGS="unit" go test ./... | ||
var TaggedTestsEnvVar = "TEST_TAGS" | ||
|
||
// RequireTestTag runs the test if the provided tag matches at least one runtime tag. | ||
// Example: | ||
// func TestSomething(t *testing.T) { | ||
// RequireTestTag(t, "unit") | ||
// ... | ||
// } | ||
// Run with: | ||
// env TEST_TAGS="unit,integration" go test ./... | ||
func RequireTestTag(t *testing.T, testTag string) { | ||
if !getRuntimeTags().contains(testTag) { | ||
t.Skipf("skipping test '%s', requires '%s' tag", t.Name(), testTag) | ||
} | ||
} | ||
|
||
// RequireOneOfTestTags runs the test if any of the provided test tags matches one of the runtime tags. | ||
func RequireOneOfTestTags(t *testing.T, testTags ...string) { | ||
if !getRuntimeTags().containsAny(testTags...) { | ||
t.Skipf("skipping test '%s', requires at least one of the following tags: '%s'", | ||
t.Name(), strings.Join(testTags, ", ")) | ||
} | ||
} | ||
|
||
// RequireAllTestTags runs the test if all the provided test tags appear in runtime tags. | ||
func RequireAllTestTags(t *testing.T, testTags ...string) { | ||
if !getRuntimeTags().containsAll(testTags...) { | ||
t.Skipf("skipping test '%s', requires all of the following tags: '%s'", | ||
t.Name(), strings.Join(testTags, ", ")) | ||
} | ||
} | ||
|
||
type runtimeTags []string | ||
|
||
func getRuntimeTags() runtimeTags { | ||
return parseTags(os.Getenv(TaggedTestsEnvVar)) | ||
} | ||
|
||
func parseTags(rawTags string) runtimeTags { | ||
rawTags = strings.ReplaceAll(rawTags, " ", "") | ||
if rawTags == "" { | ||
return nil | ||
} | ||
return strings.Split(rawTags, ",") | ||
} | ||
|
||
func (rt runtimeTags) contains(targetTag string) bool { | ||
for _, tag := range rt { | ||
if tag == targetTag { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (rt runtimeTags) containsAny(targetTags ...string) bool { | ||
for _, targetTag := range targetTags { | ||
if rt.contains(targetTag) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (rt runtimeTags) containsAll(targetTags ...string) bool { | ||
for _, targetTag := range targetTags { | ||
if !rt.contains(targetTag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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,26 @@ | ||
package testy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsMethods(t *testing.T) { | ||
rt := parseTags("unit,integration") | ||
|
||
assert.True(t, rt.contains("unit")) | ||
assert.True(t, rt.contains("integration")) | ||
assert.False(t, rt.contains("")) | ||
assert.False(t, rt.contains("UNIT")) | ||
|
||
assert.True(t, rt.containsAll("unit")) | ||
assert.True(t, rt.containsAll("unit", "integration")) | ||
assert.False(t, rt.containsAll("unit", "integration", "something-else")) | ||
assert.False(t, rt.containsAll("unit", "integration", "")) | ||
|
||
assert.True(t, rt.containsAny("unit", "something-else")) | ||
assert.True(t, rt.containsAny("whatever", "unit", "something-else")) | ||
assert.True(t, rt.containsAny("whatever", "unit", "something-else", "integration")) | ||
assert.False(t, rt.containsAny("whatever", "", "something-else")) | ||
} |