@@ -16,6 +16,8 @@ import (
1616// Test is a minimal interface for abstracting test methods that are needed to
1717// setup an isolated test environment for GoMock and Testify.
1818type Test interface { //nolint:interfacebloat // Minimal interface.
19+ // Embeds the basic test reporter interface.
20+ Reporter
1921 // Name provides the test name.
2022 Name () string
2123 // Helper declares a test helper function.
@@ -38,33 +40,13 @@ type Test interface { //nolint:interfacebloat // Minimal interface.
3840 SkipNow ()
3941 // Skipped reports whether the test has been skipped.
4042 Skipped () bool
41- // Log provides a logging function for the test.
42- Log (args ... any )
43- // Logf provides a logging function for the test.
44- Logf (format string , args ... any )
45- // Error handles a failure messages when a test is supposed to continue.
46- Error (args ... any )
47- // Errorf handles a failure messages when a test is supposed to continue.
48- Errorf (format string , args ... any )
49- // Fatal handles a fatal failure message that immediate aborts of the test
50- // execution.
51- Fatal (args ... any )
52- // Fatalf handles a fatal failure message that immediate aborts of the test
53- // execution.
54- Fatalf (format string , args ... any )
55- // Fail handles a failure message that immediate aborts of the test
56- // execution.
57- Fail ()
58- // FailNow handles fatal failure notifications without log output that
59- // aborts test execution immediately.
60- FailNow ()
6143 // Failed reports whether the test has failed.
6244 Failed () bool
6345 // Cleanup is a function called to setup test cleanup after execution.
6446 Cleanup (cleanup func ())
6547}
6648
67- // Cleanuper defines an interface to add a custom mehtod that is called after
49+ // Cleanuper defines an interface to add a custom method that is called after
6850// the test execution to cleanup the test environment.
6951type Cleanuper interface {
7052 Cleanup (cleanup func ())
@@ -80,7 +62,7 @@ func Run(expect Expect, test Func) func(*testing.T) {
8062 return func (t * testing.T ) {
8163 t .Helper ()
8264
83- New (t , expect , Parallel ).Run (test )
65+ New (t , Parallel ). Expect ( expect ).Run (test )
8466 }
8567}
8668
@@ -91,7 +73,7 @@ func RunSeq(expect Expect, test Func) func(*testing.T) {
9173 return func (t * testing.T ) {
9274 t .Helper ()
9375
94- New (t , expect , ! Parallel ).Run (test )
76+ New (t , ! Parallel ). Expect ( expect ).Run (test )
9577 }
9678}
9779
@@ -102,7 +84,7 @@ func InRun(expect Expect, test Func) Func {
10284 return func (t Test ) {
10385 t .Helper ()
10486
105- New (t , expect , ! Parallel ).Run (test )
87+ New (t , ! Parallel ). Expect ( expect ).Run (test )
10688 }
10789}
10890
@@ -123,14 +105,14 @@ type Context struct {
123105}
124106
125107// New creates a new minimal isolated test context based on the given test
126- // context with the given expectation . The parent test context is used to
127- // delegate methods calls to the parent context to propagate test results.
128- func New (t Test , expect Expect , parallel bool ) * Context {
108+ // context with. The parent test context is used to delegate methods calls
109+ // to the parent context to propagate test results.
110+ func New (t Test , parallel bool ) * Context {
129111 if tx , ok := t .(* Context ); ok {
130112 return & Context {
131113 t : tx , wg : tx .wg ,
132114 deadline : tx .deadline ,
133- expect : expect ,
115+ expect : true ,
134116 parallel : parallel ,
135117 }
136118 }
@@ -142,11 +124,23 @@ func New(t Test, expect Expect, parallel bool) *Context {
142124 deadline , _ := t .Deadline ()
143125 return deadline
144126 }(t ),
145- expect : expect ,
127+ expect : true ,
146128 parallel : parallel ,
147129 }
148130}
149131
132+ // Expect sets up a new test outcome.
133+ func (t * Context ) Expect (expect Expect ) * Context {
134+ t .t .Helper ()
135+
136+ t .mu .Lock ()
137+ defer t .mu .Unlock ()
138+
139+ t .expect = expect
140+
141+ return t
142+ }
143+
150144// Timeout sets up an individual timeout for the test. This does not affect the
151145// global test timeout or a pending parent timeout that may abort the test, if
152146// the given duration is exceeding the timeout. A negative or zero duration is
@@ -325,6 +319,9 @@ func (t *Context) Log(args ...any) {
325319 t .t .Helper ()
326320
327321 t .t .Log (args ... )
322+ if t .reporter != nil {
323+ t .reporter .Log (args ... )
324+ }
328325}
329326
330327// Logf delegates request to the parent context. It provides a logging function
@@ -333,6 +330,9 @@ func (t *Context) Logf(format string, args ...any) {
333330 t .t .Helper ()
334331
335332 t .t .Logf (format , args ... )
333+ if t .reporter != nil {
334+ t .reporter .Logf (format , args ... )
335+ }
336336}
337337
338338// Error handles failure messages where the test is supposed to continue. On
@@ -466,7 +466,9 @@ func (t *Context) Panic(arg any) {
466466 stack := regexPanic .Split (string (debug .Stack ()), - 1 )
467467 t .t .Fatalf ("panic: %v\n %s\n %s" , arg , stack [0 ], stack [1 ])
468468 } else if t .reporter != nil {
469- t .reporter .Panic (arg )
469+ if reporter , ok := t .reporter .(Panicer ); ok {
470+ reporter .Panic (arg )
471+ }
470472 }
471473 runtime .Goexit ()
472474}
0 commit comments