| | | 1 | | package domain |
| | | 2 | | |
| | | 3 | | import "time" |
| | | 4 | | |
| | | 5 | | // TestStatus is the outcome of a single test case. |
| | | 6 | | type TestStatus int |
| | | 7 | | |
| | | 8 | | // Test outcome constants. |
| | | 9 | | const ( |
| | | 10 | | StatusPass TestStatus = iota |
| | | 11 | | StatusFail |
| | | 12 | | StatusSkip |
| | | 13 | | StatusError |
| | | 14 | | ) |
| | | 15 | | |
| | | 16 | | func (s TestStatus) String() string { |
| | | 17 | | switch s { |
| | | 18 | | case StatusPass: |
| | | 19 | | return "pass" |
| | | 20 | | case StatusFail: |
| | | 21 | | return "fail" |
| | | 22 | | case StatusSkip: |
| | | 23 | | return "skip" |
| | | 24 | | case StatusError: |
| | | 25 | | return "error" |
| | | 26 | | default: |
| | | 27 | | return "unknown" |
| | | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | // TestResult holds the outcome of one `it` block. |
| | | 32 | | type TestResult struct { |
| | | 33 | | // Name is the full path of describe/it labels, e.g. "mymodule > behaves correctly". |
| | | 34 | | Name string |
| | | 35 | | Status TestStatus |
| | | 36 | | Duration time.Duration |
| | | 37 | | // Output is any text the test wrote to stdout. |
| | | 38 | | Output string |
| | | 39 | | // Error is the failure or error message, empty when Status == StatusPass or StatusSkip. |
| | | 40 | | Error string |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // SuiteResult aggregates all test results from a run. |
| | | 44 | | type SuiteResult struct { |
| | | 45 | | Tests []TestResult |
| | | 46 | | Duration time.Duration |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // Counts returns the pass, fail, skip, and error counts. |
| | 7 | 50 | | func (s *SuiteResult) Counts() (pass, fail, skip, errors int) { |
| | 7 | 51 | | for _, t := range s.Tests { |
| | 11 | 52 | | switch t.Status { |
| | 5 | 53 | | case StatusPass: |
| | 5 | 54 | | pass++ |
| | 2 | 55 | | case StatusFail: |
| | 2 | 56 | | fail++ |
| | 2 | 57 | | case StatusSkip: |
| | 2 | 58 | | skip++ |
| | 2 | 59 | | case StatusError: |
| | 2 | 60 | | errors++ |
| | | 61 | | } |
| | | 62 | | } |
| | 7 | 63 | | return |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Passed reports whether the suite has no failures or errors. |
| | 5 | 67 | | func (s *SuiteResult) Passed() bool { |
| | 5 | 68 | | _, fail, _, errors := s.Counts() |
| | 5 | 69 | | return fail == 0 && errors == 0 |
| | 5 | 70 | | } |