run.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. checkTimeout = flag.String("check.timeout", "", "Panic if test runs longer than specified duration")
  39. )
  40. // TestingT runs all test suites registered with the Suite function,
  41. // printing results to stdout, and reporting any failures back to
  42. // the "testing" package.
  43. func TestingT(testingT *testing.T) {
  44. benchTime := *newBenchTime
  45. if benchTime == 1*time.Second {
  46. benchTime = *oldBenchTime
  47. }
  48. conf := &RunConf{
  49. Filter: *oldFilterFlag + *newFilterFlag,
  50. Verbose: *oldVerboseFlag || *newVerboseFlag,
  51. Stream: *oldStreamFlag || *newStreamFlag,
  52. Benchmark: *oldBenchFlag || *newBenchFlag,
  53. BenchmarkTime: benchTime,
  54. BenchmarkMem: *newBenchMem,
  55. KeepWorkDir: *oldWorkFlag || *newWorkFlag,
  56. }
  57. if *checkTimeout != "" {
  58. timeout, err := time.ParseDuration(*checkTimeout)
  59. if err != nil {
  60. testingT.Fatalf("error parsing specified timeout flag: %v", err)
  61. }
  62. conf.CheckTimeout = timeout
  63. }
  64. if *oldListFlag || *newListFlag {
  65. w := bufio.NewWriter(os.Stdout)
  66. for _, name := range ListAll(conf) {
  67. fmt.Fprintln(w, name)
  68. }
  69. w.Flush()
  70. return
  71. }
  72. result := RunAll(conf)
  73. println(result.String())
  74. if !result.Passed() {
  75. testingT.Fail()
  76. }
  77. }
  78. // RunAll runs all test suites registered with the Suite function, using the
  79. // provided run configuration.
  80. func RunAll(runConf *RunConf) *Result {
  81. result := Result{}
  82. for _, suite := range allSuites {
  83. result.Add(Run(suite, runConf))
  84. }
  85. return &result
  86. }
  87. // Run runs the provided test suite using the provided run configuration.
  88. func Run(suite interface{}, runConf *RunConf) *Result {
  89. runner := newSuiteRunner(suite, runConf)
  90. return runner.run()
  91. }
  92. // ListAll returns the names of all the test functions registered with the
  93. // Suite function that will be run with the provided run configuration.
  94. func ListAll(runConf *RunConf) []string {
  95. var names []string
  96. for _, suite := range allSuites {
  97. names = append(names, List(suite, runConf)...)
  98. }
  99. return names
  100. }
  101. // List returns the names of the test functions in the given
  102. // suite that will be run with the provided run configuration.
  103. func List(suite interface{}, runConf *RunConf) []string {
  104. var names []string
  105. runner := newSuiteRunner(suite, runConf)
  106. for _, t := range runner.tests {
  107. names = append(names, t.String())
  108. }
  109. return names
  110. }
  111. // -----------------------------------------------------------------------
  112. // Result methods.
  113. func (r *Result) Add(other *Result) {
  114. r.Succeeded += other.Succeeded
  115. r.Skipped += other.Skipped
  116. r.Failed += other.Failed
  117. r.Panicked += other.Panicked
  118. r.FixturePanicked += other.FixturePanicked
  119. r.ExpectedFailures += other.ExpectedFailures
  120. r.Missed += other.Missed
  121. if r.WorkDir != "" && other.WorkDir != "" {
  122. r.WorkDir += ":" + other.WorkDir
  123. } else if other.WorkDir != "" {
  124. r.WorkDir = other.WorkDir
  125. }
  126. }
  127. func (r *Result) Passed() bool {
  128. return (r.Failed == 0 && r.Panicked == 0 &&
  129. r.FixturePanicked == 0 && r.Missed == 0 &&
  130. r.RunError == nil)
  131. }
  132. func (r *Result) String() string {
  133. if r.RunError != nil {
  134. return "ERROR: " + r.RunError.Error()
  135. }
  136. var value string
  137. if r.Failed == 0 && r.Panicked == 0 && r.FixturePanicked == 0 &&
  138. r.Missed == 0 {
  139. value = "OK: "
  140. } else {
  141. value = "OOPS: "
  142. }
  143. value += fmt.Sprintf("%d passed", r.Succeeded)
  144. if r.Skipped != 0 {
  145. value += fmt.Sprintf(", %d skipped", r.Skipped)
  146. }
  147. if r.ExpectedFailures != 0 {
  148. value += fmt.Sprintf(", %d expected failures", r.ExpectedFailures)
  149. }
  150. if r.Failed != 0 {
  151. value += fmt.Sprintf(", %d FAILED", r.Failed)
  152. }
  153. if r.Panicked != 0 {
  154. value += fmt.Sprintf(", %d PANICKED", r.Panicked)
  155. }
  156. if r.FixturePanicked != 0 {
  157. value += fmt.Sprintf(", %d FIXTURE-PANICKED", r.FixturePanicked)
  158. }
  159. if r.Missed != 0 {
  160. value += fmt.Sprintf(", %d MISSED", r.Missed)
  161. }
  162. if r.WorkDir != "" {
  163. value += "\nWORK=" + r.WorkDir
  164. }
  165. return value
  166. }