forked from goddenrich/go-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
154 lines (121 loc) · 5.04 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
package junit
import (
"time"
)
// Status represents the result of a single a JUnit testcase. Indicates if a
// testcase was run, and if it was successful.
type Status string
const (
// StatusPassed represents a JUnit testcase that was run, and did not
// result in an error or a failure.
StatusPassed Status = "passed"
// StatusSkipped represents a JUnit testcase that was intentionally
// skipped.
StatusSkipped Status = "skipped"
// StatusFailed represents a JUnit testcase that was run, but resulted in
// a failure. Failures are violations of declared test expectations,
// such as a failed assertion.
StatusFailed Status = "failed"
// StatusError represents a JUnit testcase that was run, but resulted in
// an error. Errors are unexpected violations of the test itself, such as
// an uncaught exception.
StatusError Status = "error"
)
// Totals contains aggregated results across a set of test runs. Is usually
// calculated as a sum of all given test runs, and overrides whatever was given
// at the suite level.
//
// The following relation should hold true.
// Tests == (Passed + Skipped + Failed + Error)
type Totals struct {
// Tests is the total number of tests run.
Tests int `json:"tests" yaml:"tests"`
// Passed is the total number of tests that passed successfully.
Passed int `json:"passed" yaml:"passed"`
// Skipped is the total number of tests that were skipped.
Skipped int `json:"skipped" yaml:"skipped"`
// Failed is the total number of tests that resulted in a failure.
Failed int `json:"failed" yaml:"failed"`
// Error is the total number of tests that resulted in an error.
Error int `json:"error" yaml:"error"`
// Duration is the total time taken to run all tests.
Duration time.Duration `json:"duration" yaml:"duration"`
}
// Suite represents a logical grouping (suite) of tests.
type Suite struct {
// Name is a descriptor given to the suite.
Name string `json:"name" yaml:"name"`
// Package is an additional descriptor for the hierarchy of the suite.
Package string `json:"package" yaml:"package"`
// Properties is a mapping of key-value pairs that were available when the
// tests were run.
Properties map[string]string `json:"properties,omitempty" yaml:"properties,omitempty"`
// Tests is an ordered collection of tests with associated results.
Tests []Test `json:"tests,omitempty" yaml:"tests,omitempty"`
// SystemOut is textual test output for the suite. Usually output that is
// written to stdout.
SystemOut string `json:"stdout,omitempty" yaml:"stdout,omitempty"`
// SystemErr is textual test error output for the suite. Usually output that is
// written to stderr.
SystemErr string `json:"stderr,omitempty" yaml:"stderr,omitempty"`
// Totals is the aggregated results of all tests.
Totals Totals `json:"totals" yaml:"totals"`
}
// Aggregate calculates result sums across all tests.
func (s *Suite) Aggregate() {
totals := Totals{Tests: len(s.Tests)}
for _, test := range s.Tests {
totals.Duration += test.Duration
switch test.Status {
case StatusPassed:
totals.Passed++
case StatusSkipped:
totals.Skipped++
case StatusFailed:
totals.Failed++
case StatusError:
totals.Error++
}
}
s.Totals = totals
}
// Test represents the results of a single test run.
type Test struct {
// Name is a descriptor given to the test.
Name string `json:"name" yaml:"name"`
// Classname is an additional descriptor for the hierarchy of the test.
Classname string `json:"classname" yaml:"classname"`
// Duration is the total time taken to run the tests.
Duration time.Duration `json:"duration" yaml:"duration"`
// Status is the result of the test. Status values are passed, skipped,
// failure, & error.
Status Status `json:"status" yaml:"status"`
// Error is a record of the failure or error of a test, if applicable.
//
// The following relations should hold true.
// Error == nil && (Status == Passed || Status == Skipped)
// Error != nil && (Status == Failed || Status == Error)
Error error `json:"error" yaml:"error"`
// Additional properties from XML node attributes.
// Some tools use them to store additional information about test location.
Properties map[string]string `json:"properties" yaml:"properties"`
}
// Error represents an erroneous test result.
type Error struct {
// Message is a descriptor given to the error. Purpose and values differ by
// environment.
Message string `json:"message,omitempty" yaml:"message,omitempty"`
// Type is a descriptor given to the error. Purpose and values differ by
// framework. Value is typically an exception class, such as an assertion.
Type string `json:"type,omitempty" yaml:"type,omitempty"`
// Body is extended text for the error. Purpose and values differ by
// framework. Value is typically a stacktrace.
Body string `json:"body,omitempty" yaml:"body,omitempty"`
}
// Error returns a textual description of the test error.
func (err Error) Error() string {
return err.Body
}