-
Notifications
You must be signed in to change notification settings - Fork 4
/
gosuite.go
61 lines (50 loc) · 1.78 KB
/
gosuite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Package gosuite provides a simple and tiny tool that brings the support of test suites to Go 1.7 Subtests addition to "testing".
A test suite is an abstraction that allows you to group certain test cases together as well as allowing you to perform setup/teardown
logic for each of test cases as well as the setup/teardown stuff for the suite itself.
This is useful, for instance, in cases where you need to set up database schema before your suite as well as truncate the database
tables before each test case so each of them is run against an empty database.
*/
package gosuite
import (
"reflect"
"strings"
"testing"
)
const suiteTestMethodPrefix = "Test"
// TestSuite is an interface where you define suite and test case preparation and tear down logic.
type TestSuite interface {
// SetUpSuite is called once before the very first test in suite runs
SetUpSuite()
// TearDownSuite is called once after thevery last test in suite runs
TearDownSuite()
// SetUp is called before each test method
SetUp()
// TearDown is called after each test method
TearDown()
}
/*
Run sets up the suite, runs its test cases and tears it down:
1. Calls `suite.SetUpSuite`
2. Seeks for any methods that have `Test` prefix, for each of them it:
a. Calls `SetUp`
b. Calls the test method itself
c. Calls `TearDown`
3. Calls `suite.TearDownSuite`
*/
func Run(t *testing.T, suite TestSuite) {
suite.SetUpSuite()
defer suite.TearDownSuite()
suiteType := reflect.TypeOf(suite)
for i := 0; i < suiteType.NumMethod(); i++ {
m := suiteType.Method(i)
if strings.HasPrefix(m.Name, suiteTestMethodPrefix) {
t.Run(m.Name, func(t *testing.T) {
suite.SetUp()
defer suite.TearDown()
in := []reflect.Value{reflect.ValueOf(suite), reflect.ValueOf(t)}
m.Func.Call(in)
})
}
}
}