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