-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
reporter.go
74 lines (60 loc) · 2.14 KB
/
reporter.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
74
package httpexpect
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// AssertReporter implements Reporter interface using `testify/assert'
// package. Failures are non-fatal with this reporter.
type AssertReporter struct {
backend *assert.Assertions
}
// NewAssertReporter returns a new AssertReporter object.
func NewAssertReporter(t assert.TestingT) *AssertReporter {
return &AssertReporter{assert.New(t)}
}
// Errorf implements Reporter.Errorf.
func (r *AssertReporter) Errorf(message string, args ...interface{}) {
r.backend.Fail(fmt.Sprintf(message, args...))
}
// RequireReporter implements Reporter interface using `testify/require'
// package. Failures are fatal with this reporter.
type RequireReporter struct {
backend *require.Assertions
}
// NewRequireReporter returns a new RequireReporter object.
func NewRequireReporter(t require.TestingT) *RequireReporter {
return &RequireReporter{require.New(t)}
}
// Errorf implements Reporter.Errorf.
func (r *RequireReporter) Errorf(message string, args ...interface{}) {
r.backend.FailNow(fmt.Sprintf(message, args...))
}
// FatalReporter is a struct that implements the Reporter interface
// and calls t.Fatalf() when a test fails.
type FatalReporter struct {
backend testing.TB
}
// NewFatalReporter returns a new FatalReporter object.
func NewFatalReporter(t testing.TB) *FatalReporter {
return &FatalReporter{t}
}
// Errorf implements Reporter.Errorf.
func (r *FatalReporter) Errorf(message string, args ...interface{}) {
r.backend.Fatalf(fmt.Sprintf(message, args...))
}
// PanicReporter is a struct that implements the Reporter interface
// and panics when a test fails.
// Useful for multithreaded tests when you want to report fatal
// failures from goroutines other than the main goroutine, because
// the main goroutine is forbidden to call t.Fatal.
type PanicReporter struct{}
// NewPanicReporter returns a new PanicReporter object.
func NewPanicReporter() *PanicReporter {
return &PanicReporter{}
}
// Errorf implements Reporter.Errorf
func (r *PanicReporter) Errorf(message string, args ...interface{}) {
panic(fmt.Sprintf(message, args...))
}