reporter.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package httpexpect
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // AssertReporter implements Reporter interface using `testify/assert'
  9. // package. Failures are non-fatal with this reporter.
  10. type AssertReporter struct {
  11. backend *assert.Assertions
  12. }
  13. // NewAssertReporter returns a new AssertReporter object.
  14. func NewAssertReporter(t assert.TestingT) *AssertReporter {
  15. return &AssertReporter{assert.New(t)}
  16. }
  17. // Errorf implements Reporter.Errorf.
  18. func (r *AssertReporter) Errorf(message string, args ...interface{}) {
  19. r.backend.Fail(fmt.Sprintf(message, args...))
  20. }
  21. // RequireReporter implements Reporter interface using `testify/require'
  22. // package. Failures are fatal with this reporter.
  23. type RequireReporter struct {
  24. backend *require.Assertions
  25. }
  26. // NewRequireReporter returns a new RequireReporter object.
  27. func NewRequireReporter(t require.TestingT) *RequireReporter {
  28. return &RequireReporter{require.New(t)}
  29. }
  30. // Errorf implements Reporter.Errorf.
  31. func (r *RequireReporter) Errorf(message string, args ...interface{}) {
  32. r.backend.FailNow(fmt.Sprintf(message, args...))
  33. }
  34. // FatalReporter is a struct that implements the Reporter interface
  35. // and calls t.Fatalf() when a test fails.
  36. type FatalReporter struct {
  37. backend testing.TB
  38. }
  39. // NewFatalReporter returns a new FatalReporter object.
  40. func NewFatalReporter(t testing.TB) *FatalReporter {
  41. return &FatalReporter{t}
  42. }
  43. // Errorf implements Reporter.Errorf.
  44. func (r *FatalReporter) Errorf(message string, args ...interface{}) {
  45. r.backend.Fatalf(fmt.Sprintf(message, args...))
  46. }
  47. // PanicReporter is a struct that implements the Reporter interface
  48. // and panics when a test fails.
  49. // Useful for multithreaded tests when you want to report fatal
  50. // failures from goroutines other than the main goroutine, because
  51. // the main goroutine is forbidden to call t.Fatal.
  52. type PanicReporter struct{}
  53. // NewPanicReporter returns a new PanicReporter object.
  54. func NewPanicReporter() *PanicReporter {
  55. return &PanicReporter{}
  56. }
  57. // Errorf implements Reporter.Errorf
  58. func (r *PanicReporter) Errorf(message string, args ...interface{}) {
  59. panic(fmt.Sprintf(message, args...))
  60. }