testing.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // +build !go1.9
  2. package testing
  3. import (
  4. "fmt"
  5. "log"
  6. )
  7. // T is the interface that mimics the standard library *testing.T.
  8. //
  9. // In unit tests you can just pass a *testing.T struct. At runtime, outside
  10. // of tests, you can pass in a RuntimeT struct from this package.
  11. type T interface {
  12. Error(args ...interface{})
  13. Errorf(format string, args ...interface{})
  14. Fail()
  15. FailNow()
  16. Failed() bool
  17. Fatal(args ...interface{})
  18. Fatalf(format string, args ...interface{})
  19. Log(args ...interface{})
  20. Logf(format string, args ...interface{})
  21. Name() string
  22. Skip(args ...interface{})
  23. SkipNow()
  24. Skipf(format string, args ...interface{})
  25. Skipped() bool
  26. }
  27. // RuntimeT implements T and can be instantiated and run at runtime to
  28. // mimic *testing.T behavior. Unlike *testing.T, this will simply panic
  29. // for calls to Fatal. For calls to Error, you'll have to check the errors
  30. // list to determine whether to exit yourself. Name and Skip methods are
  31. // unimplemented noops.
  32. type RuntimeT struct {
  33. failed bool
  34. }
  35. func (t *RuntimeT) Error(args ...interface{}) {
  36. log.Println(fmt.Sprintln(args...))
  37. t.Fail()
  38. }
  39. func (t *RuntimeT) Errorf(format string, args ...interface{}) {
  40. log.Println(fmt.Sprintf(format, args...))
  41. t.Fail()
  42. }
  43. func (t *RuntimeT) Fatal(args ...interface{}) {
  44. log.Println(fmt.Sprintln(args...))
  45. t.FailNow()
  46. }
  47. func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
  48. log.Println(fmt.Sprintf(format, args...))
  49. t.FailNow()
  50. }
  51. func (t *RuntimeT) Fail() {
  52. t.failed = true
  53. }
  54. func (t *RuntimeT) FailNow() {
  55. panic("testing.T failed, see logs for output (if any)")
  56. }
  57. func (t *RuntimeT) Failed() bool {
  58. return t.failed
  59. }
  60. func (t *RuntimeT) Log(args ...interface{}) {
  61. log.Println(fmt.Sprintln(args...))
  62. }
  63. func (t *RuntimeT) Logf(format string, args ...interface{}) {
  64. log.Println(fmt.Sprintf(format, args...))
  65. }
  66. func (t *RuntimeT) Name() string { return "" }
  67. func (t *RuntimeT) Skip(args ...interface{}) {}
  68. func (t *RuntimeT) SkipNow() {}
  69. func (t *RuntimeT) Skipf(format string, args ...interface{}) {}
  70. func (t *RuntimeT) Skipped() bool { return false }