run.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package check
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. // -----------------------------------------------------------------------
  11. // Test suite registry.
  12. var allSuites []interface{}
  13. // Suite registers the given value as a test suite to be run. Any methods
  14. // starting with the Test prefix in the given value will be considered as
  15. // a test method.
  16. func Suite(suite interface{}) interface{} {
  17. allSuites = append(allSuites, suite)
  18. return suite
  19. }
  20. // -----------------------------------------------------------------------
  21. // Public running interface.
  22. var (
  23. oldFilterFlag = flag.String("gocheck.f", "", "Regular expression selecting which tests and/or suites to run")
  24. oldVerboseFlag = flag.Bool("gocheck.v", false, "Verbose mode")
  25. oldStreamFlag = flag.Bool("gocheck.vv", false, "Super verbose mode (disables output caching)")
  26. oldBenchFlag = flag.Bool("gocheck.b", false, "Run benchmarks")
  27. oldBenchTime = flag.Duration("gocheck.btime", 1*time.Second, "approximate run time for each benchmark")
  28. oldListFlag = flag.Bool("gocheck.list", false, "List the names of all tests that will be run")
  29. oldWorkFlag = flag.Bool("gocheck.work", false, "Display and do not remove the test working directory")
  30. newFilterFlag = flag.String("check.f", "", "Regular expression selecting which tests and/or suites to run")
  31. newVerboseFlag = flag.Bool("check.v", false, "Verbose mode")
  32. newStreamFlag = flag.Bool("check.vv", false, "Super verbose mode (disables output caching)")
  33. newBenchFlag = flag.Bool("check.b", false, "Run benchmarks")
  34. newBenchTime = flag.Duration("check.btime", 1*time.Second, "approximate run time for each benchmark")
  35. newBenchMem = flag.Bool("check.bmem", false, "Report memory benchmarks")
  36. newListFlag = flag.Bool("check.list", false, "List the names of all tests that will be run")
  37. newWorkFlag = flag.Bool("check.work", false, "Display and do not remove the test working directory")
  38. )
  39. // TestingT runs all test suites registered with the Suite function,
  40. // printing results to stdout, and reporting any failures back to
  41. // the "testing" package.
  42. func TestingT(testingT *testing.T) {
  43. benchTime := *newBenchTime
  44. if benchTime == 1*time.Second {
  45. benchTime = *oldBenchTime
  46. }
  47. conf := &RunConf{
  48. Filter: *oldFilterFlag + *newFilterFlag,
  49. Verbose: *oldVerboseFlag || *newVerboseFlag,
  50. Stream: *oldStreamFlag || *newStreamFlag,
  51. Benchmark: *oldBenchFlag || *newBenchFlag,
  52. BenchmarkTime: benchTime,
  53. BenchmarkMem: *newBenchMem,
  54. KeepWorkDir: *oldWorkFlag || *newWorkFlag,
  55. }
  56. if *oldListFlag || *newListFlag {
  57. w := bufio.NewWriter(os.Stdout)
  58. for _, name := range ListAll(conf) {
  59. fmt.Fprintln(w, name)
  60. }
  61. w.Flush()
  62. return
  63. }
  64. result := RunAll(conf)
  65. println(result.String())
  66. if !result.Passed() {
  67. testingT.Fail()
  68. }
  69. }
  70. // RunAll runs all test suites registered with the Suite function, using the
  71. // provided run configuration.
  72. func RunAll(runConf *RunConf) *Result {
  73. result := Result{}
  74. for _, suite := range allSuites {
  75. result.Add(Run(suite, runConf))
  76. }
  77. return &result
  78. }
  79. // Run runs the provided test suite using the provided run configuration.
  80. func Run(suite interface{}, runConf *RunConf) *Result {
  81. runner := newSuiteRunner(suite, runConf)
  82. return runner.run()
  83. }
  84. // ListAll returns the names of all the test functions registered with the
  85. // Suite function that will be run with the provided run configuration.
  86. func ListAll(runConf *RunConf) []string {
  87. var names []string
  88. for _, suite := range allSuites {
  89. names = append(names, List(suite, runConf)...)
  90. }
  91. return names
  92. }
  93. // List returns the names of the test functions in the given
  94. // suite that will be run with the provided run configuration.
  95. func List(suite interface{}, runConf *RunConf) []string {
  96. var names []string
  97. runner := newSuiteRunner(suite, runConf)
  98. for _, t := range runner.tests {
  99. names = append(names, t.String())
  100. }
  101. return names
  102. }
  103. // -----------------------------------------------------------------------
  104. // Result methods.
  105. func (r *Result) Add(other *Result) {
  106. r.Succeeded += other.Succeeded
  107. r.Skipped += other.Skipped
  108. r.Failed += other.Failed
  109. r.Panicked += other.Panicked
  110. r.FixturePanicked += other.FixturePanicked
  111. r.ExpectedFailures += other.ExpectedFailures
  112. r.Missed += other.Missed
  113. if r.WorkDir != "" && other.WorkDir != "" {
  114. r.WorkDir += ":" + other.WorkDir
  115. } else if other.WorkDir != "" {
  116. r.WorkDir = other.WorkDir
  117. }
  118. }
  119. func (r *Result) Passed() bool {
  120. return (r.Failed == 0 && r.Panicked == 0 &&
  121. r.FixturePanicked == 0 && r.Missed == 0 &&
  122. r.RunError == nil)
  123. }
  124. func (r *Result) String() string {
  125. if r.RunError != nil {
  126. return "ERROR: " + r.RunError.Error()
  127. }
  128. var value string
  129. if r.Failed == 0 && r.Panicked == 0 && r.FixturePanicked == 0 &&
  130. r.Missed == 0 {
  131. value = "OK: "
  132. } else {
  133. value = "OOPS: "
  134. }
  135. value += fmt.Sprintf("%d passed", r.Succeeded)
  136. if r.Skipped != 0 {
  137. value += fmt.Sprintf(", %d skipped", r.Skipped)
  138. }
  139. if r.ExpectedFailures != 0 {
  140. value += fmt.Sprintf(", %d expected failures", r.ExpectedFailures)
  141. }
  142. if r.Failed != 0 {
  143. value += fmt.Sprintf(", %d FAILED", r.Failed)
  144. }
  145. if r.Panicked != 0 {
  146. value += fmt.Sprintf(", %d PANICKED", r.Panicked)
  147. }
  148. if r.FixturePanicked != 0 {
  149. value += fmt.Sprintf(", %d FIXTURE-PANICKED", r.FixturePanicked)
  150. }
  151. if r.Missed != 0 {
  152. value += fmt.Sprintf(", %d MISSED", r.Missed)
  153. }
  154. if r.WorkDir != "" {
  155. value += "\nWORK=" + r.WorkDir
  156. }
  157. return value
  158. }