@@ -18,9 +18,11 @@ type test struct {
18
18
type Organizer struct {
19
19
t * testing.T
20
20
21
- mocks []* mock.Mock
22
- setup []func ()
23
- tests []test
21
+ only * test
22
+ mocks []* mock.Mock
23
+ setup []func ()
24
+ cleanup []func ()
25
+ tests []test
24
26
}
25
27
26
28
// Create a new Organizer against testing's T interface
@@ -38,11 +40,20 @@ func (o *Organizer) Setup(f func()) {
38
40
o .setup = append (o .setup , f )
39
41
}
40
42
43
+ // Add some work to be done after each test
44
+ func (o * Organizer ) Cleanup (f func ()) {
45
+ o .cleanup = append (o .cleanup , f )
46
+ }
47
+
41
48
// Add a test.
42
49
func (o * Organizer ) It (name string , f func ()) {
43
50
o .tests = append (o .tests , test {name , f })
44
51
}
45
52
53
+ func (o * Organizer ) Only (name string , f func ()) {
54
+ o .only = & test {name , f }
55
+ }
56
+
46
57
// Useful by allowing the developer to simply add 'N' before
47
58
// It to disable a block.
48
59
func (o * Organizer ) NIt (name string , f func ()) {
@@ -51,28 +62,45 @@ func (o *Organizer) NIt(name string, f func()) {
51
62
52
63
// Coordinate running the tests with the setups and mocks
53
64
func (o * Organizer ) Run () {
65
+ if o .only != nil {
66
+ o .runTest (o .only )
67
+ return
68
+ }
69
+
54
70
for _ , test := range o .tests {
55
- if test .Func == nil {
56
- o .t .Logf ("==== DISABLED: %s ====" , test .Name )
57
- continue
58
- }
71
+ o .runTest (& test )
72
+ }
73
+ }
59
74
60
- o .t .Logf ("==== %s ====" , test .Name )
75
+ func (o * Organizer ) runTest (test * test ) {
76
+ if test .Func == nil {
77
+ o .t .Logf ("==== DISABLED: %s ====" , test .Name )
78
+ return
79
+ }
80
+
81
+ o .t .Logf ("==== %s ====" , test .Name )
82
+
83
+ for _ , mock := range o .mocks {
84
+ mock .ExpectedCalls = nil
85
+ mock .Calls = nil
86
+ }
61
87
62
- for _ , mock := range o .mocks {
63
- mock .ExpectedCalls = nil
64
- mock .Calls = nil
65
- }
88
+ for _ , setup := range o .setup {
89
+ setup ()
90
+ }
91
+
92
+ defer o .runCleanup ()
66
93
67
- for _ , setup := range o .setup {
68
- setup ()
69
- }
94
+ test .Func ()
70
95
71
- test .Func ()
96
+ for _ , mock := range o .mocks {
97
+ mock .AssertExpectations (o .t )
98
+ }
99
+ }
72
100
73
- for _ , mock := range o . mocks {
74
- mock . AssertExpectations ( o . t )
75
- }
101
+ func ( o * Organizer ) runCleanup () {
102
+ for _ , cleanup := range o . cleanup {
103
+ cleanup ()
76
104
}
77
105
}
78
106
0 commit comments