/
test.go
73 lines (59 loc) · 1.44 KB
/
test.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
62
63
64
65
66
67
68
69
70
71
72
73
package lock
import (
"context"
"fmt"
"reflect"
"sync"
"github.com/stretchr/testify/suite"
"go.uber.org/atomic"
"github.com/wfusion/gofusion/common/utils"
"github.com/wfusion/gofusion/log"
"github.com/wfusion/gofusion/test"
)
var (
component = "lock"
)
type Test struct {
test.Suite
once sync.Once
exits []func()
testName string
testsLefts atomic.Int64
}
func (t *Test) SetupTest() {
t.Catch(func() {
log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component))
t.once.Do(func() {
t.exits = append(t.exits, t.Suite.Copy(t.ConfigFiles(), t.testName, 1))
})
t.exits = append(t.exits, t.Suite.Init(t.ConfigFiles(), t.testName, 1))
})
}
func (t *Test) TearDownTest() {
t.Catch(func() {
log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component))
if t.testsLefts.Add(-1) == 0 {
for i := len(t.exits) - 1; i >= 0; i-- {
t.exits[i]()
}
}
})
}
func (t *Test) AppName() string {
return fmt.Sprintf("%s.%s", component, t.testName)
}
func (t *Test) Init(testingSuite suite.TestingSuite) {
methodFinder := reflect.TypeOf(testingSuite)
numMethod := methodFinder.NumMethod()
numTestLeft := int64(0)
for i := 0; i < numMethod; i++ {
method := methodFinder.Method(i)
ok, _ := test.MethodFilter(method.Name)
if !ok {
continue
}
numTestLeft++
}
t.testName = utils.IndirectType(methodFinder).Name()
t.testsLefts.Add(numTestLeft)
}