LEFT | RIGHT |
1 package gocheck | 1 package gocheck |
2 | 2 |
3 import ( | 3 import ( |
| 4 "bufio" |
4 "flag" | 5 "flag" |
5 "fmt" | 6 "fmt" |
| 7 "os" |
6 "testing" | 8 "testing" |
| 9 "time" |
7 ) | 10 ) |
8 | 11 |
9 // ----------------------------------------------------------------------- | 12 // ----------------------------------------------------------------------- |
10 // Test suite registry. | 13 // Test suite registry. |
11 | 14 |
12 var allSuites []interface{} | 15 var allSuites []interface{} |
13 | 16 |
14 // Register the given value as a test suite to be run. Any methods starting | 17 // Register the given value as a test suite to be run. Any methods starting |
15 // with the Test prefix in the given value will be considered as a test to | 18 // with the Test prefix in the given value will be considered as a test to |
16 // be run. | 19 // be run. |
17 func Suite(suite interface{}) interface{} { | 20 func Suite(suite interface{}) interface{} { |
18 allSuites = append(allSuites, suite) | 21 allSuites = append(allSuites, suite) |
19 return suite | 22 return suite |
20 } | 23 } |
21 | 24 |
22 // ----------------------------------------------------------------------- | 25 // ----------------------------------------------------------------------- |
23 // Public running interface. | 26 // Public running interface. |
24 | 27 |
25 var filterFlag = flag.String("gocheck.f", "", | 28 var ( |
26 » "Regular expression selecting what to run") | 29 » filterFlag = flag.String("gocheck.f", "", "Regular expression selecting
which tests and/or suites to run") |
27 var verboseFlag = flag.Bool("gocheck.v", false, | 30 » verboseFlag = flag.Bool("gocheck.v", false, "Verbose mode") |
28 » "Verbose mode") | 31 » streamFlag = flag.Bool("gocheck.vv", false, "Super verbose mode (disabl
es output caching)") |
29 var streamFlag = flag.Bool("gocheck.vv", false, | 32 » benchFlag = flag.Bool("gocheck.b", false, "Run benchmarks") |
30 » "Super verbose mode (disables output caching)") | 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 te
sts that will be run") |
| 35 ) |
31 | 36 |
32 // Run all test suites registered with the Suite() function, printing | 37 // Run all test suites registered with the Suite() function, printing |
33 // results to stdout, and reporting any failures back to the 'testing' | 38 // results to stdout, and reporting any failures back to the 'testing' |
34 // module. | 39 // module. |
35 func TestingT(testingT *testing.T) { | 40 func TestingT(testingT *testing.T) { |
36 » result := RunAll(&RunConf{Filter: *filterFlag, Verbose: *verboseFlag, St
ream: *streamFlag}) | 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) |
37 println(result.String()) | 57 println(result.String()) |
38 if !result.Passed() { | 58 if !result.Passed() { |
39 testingT.Fail() | 59 testingT.Fail() |
40 } | 60 } |
41 } | 61 } |
42 | 62 |
43 // Run all test suites registered with the Suite() function, using the | 63 // RunAll runs all test suites registered with the Suite() function, using the |
44 // given run configuration. | 64 // given run configuration. |
45 func RunAll(runConf *RunConf) *Result { | 65 func RunAll(runConf *RunConf) *Result { |
46 result := Result{} | 66 result := Result{} |
47 for _, suite := range allSuites { | 67 for _, suite := range allSuites { |
48 result.Add(Run(suite, runConf)) | 68 result.Add(Run(suite, runConf)) |
49 } | 69 } |
50 return &result | 70 return &result |
51 } | 71 } |
52 | 72 |
53 // Run the given test suite using the provided run configuration. | 73 // Run runs the given test suite using the provided run configuration. |
54 func Run(suite interface{}, runConf *RunConf) *Result { | 74 func Run(suite interface{}, runConf *RunConf) *Result { |
55 runner := newSuiteRunner(suite, runConf) | 75 runner := newSuiteRunner(suite, runConf) |
56 return runner.run() | 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 |
57 } | 99 } |
58 | 100 |
59 // ----------------------------------------------------------------------- | 101 // ----------------------------------------------------------------------- |
60 // Result methods. | 102 // Result methods. |
61 | 103 |
62 func (r *Result) Add(other *Result) { | 104 func (r *Result) Add(other *Result) { |
63 r.Succeeded += other.Succeeded | 105 r.Succeeded += other.Succeeded |
64 r.Skipped += other.Skipped | 106 r.Skipped += other.Skipped |
65 r.Failed += other.Failed | 107 r.Failed += other.Failed |
66 r.Panicked += other.Panicked | 108 r.Panicked += other.Panicked |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 value += fmt.Sprintf(", %d PANICKED", r.Panicked) | 143 value += fmt.Sprintf(", %d PANICKED", r.Panicked) |
102 } | 144 } |
103 if r.FixturePanicked != 0 { | 145 if r.FixturePanicked != 0 { |
104 value += fmt.Sprintf(", %d FIXTURE-PANICKED", r.FixturePanicked) | 146 value += fmt.Sprintf(", %d FIXTURE-PANICKED", r.FixturePanicked) |
105 } | 147 } |
106 if r.Missed != 0 { | 148 if r.Missed != 0 { |
107 value += fmt.Sprintf(", %d MISSED", r.Missed) | 149 value += fmt.Sprintf(", %d MISSED", r.Missed) |
108 } | 150 } |
109 return value | 151 return value |
110 } | 152 } |
LEFT | RIGHT |