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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
package testjson
import (
"bytes"
"io"
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
func TestSummary_String(t *testing.T) {
var testcases = []struct {
name string
summary Summary
expected string
}{
{
name: "none",
summary: SummarizeNone,
expected: "none",
},
{
name: "all",
summary: SummarizeAll,
expected: "skipped,failed,errors,output",
},
{
name: "one value",
summary: SummarizeErrors,
expected: "errors",
},
{
name: "a few values",
summary: SummarizeOutput | SummarizeSkipped | SummarizeErrors,
expected: "skipped,errors,output",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.summary.String(), tc.expected)
})
}
}
func TestPrintSummary_NoFailures(t *testing.T) {
patchTimeNow(t)
out := new(bytes.Buffer)
start := time.Now()
exec := &Execution{
started: start,
done: true,
packages: map[string]*Package{
"foo": {Total: 12},
"other": {Total: 1},
},
}
timeNow = func() time.Time {
return start.Add(34123111 * time.Microsecond)
}
PrintSummary(out, exec, SummarizeAll)
expected := "\nDONE 13 tests in 34.123s\n"
assert.Equal(t, out.String(), expected)
}
func TestPrintSummary_WithFailures(t *testing.T) {
patchPkgPathPrefix(t, "example.com")
patchTimeNow(t)
start := time.Now()
exec := &Execution{
started: start,
done: true,
packages: map[string]*Package{
"example.com/project/fs": {
Total: 12,
Failed: []TestCase{
{
Package: "example.com/project/fs",
Test: "TestFileDo",
Elapsed: 1411 * time.Millisecond,
ID: 1,
},
{
Package: "example.com/project/fs",
Test: "TestFileDoError",
Elapsed: 12 * time.Millisecond,
ID: 2,
},
},
output: map[int][]string{
1: multiLine(`=== RUN TestFileDo
Some stdout/stderr here
--- FAIL: TestFileDo (1.41s)
do_test.go:33 assertion failed
`),
2: multiLine(`=== RUN TestFileDoError
--- FAIL: TestFileDoError (0.01s)
do_test.go:50 assertion failed: expected nil error, got WHAT!
`),
0: multiLine("FAIL\n"),
},
action: ActionFail,
},
"example.com/project/pkg/more": {
Total: 1,
Failed: []TestCase{
{
Package: "example.com/project/pkg/more",
Test: "TestAlbatross",
Elapsed: 40 * time.Millisecond,
ID: 1,
},
},
Skipped: []TestCase{
{
Package: "example.com/project/pkg/more",
Test: "TestOnlySometimes",
Elapsed: 0,
ID: 2,
},
},
output: map[int][]string{
1: multiLine(`=== RUN TestAlbatross
--- FAIL: TestAlbatross (0.04s)
`),
2: multiLine(`=== RUN TestOnlySometimes
--- SKIP: TestOnlySometimes (0.00s)
good_test.go:27: the skip message
`),
},
},
"example.com/project/badmain": {
action: ActionFail,
output: map[int][]string{
0: multiLine("sometimes main can exit 2\n"),
},
},
},
errors: []string{
"pkg/file.go:99:12: missing ',' before newline",
},
}
timeNow = func() time.Time {
return start.Add(34123111 * time.Microsecond)
}
t.Run("summarize all", func(t *testing.T) {
out := new(bytes.Buffer)
PrintSummary(out, exec, SummarizeAll)
expected := `
=== Skipped
=== SKIP: project/pkg/more TestOnlySometimes (0.00s)
good_test.go:27: the skip message
=== Failed
=== FAIL: project/badmain (0.00s)
sometimes main can exit 2
=== FAIL: project/fs TestFileDo (1.41s)
Some stdout/stderr here
do_test.go:33 assertion failed
=== FAIL: project/fs TestFileDoError (0.01s)
do_test.go:50 assertion failed: expected nil error, got WHAT!
=== FAIL: project/pkg/more TestAlbatross (0.04s)
=== Errors
pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
})
t.Run("summarize no output", func(t *testing.T) {
out := new(bytes.Buffer)
PrintSummary(out, exec, SummarizeAll-SummarizeOutput)
expected := `
=== Skipped
=== SKIP: project/pkg/more TestOnlySometimes (0.00s)
=== Failed
=== FAIL: project/badmain (0.00s)
=== FAIL: project/fs TestFileDo (1.41s)
=== FAIL: project/fs TestFileDoError (0.01s)
=== FAIL: project/pkg/more TestAlbatross (0.04s)
=== Errors
pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
})
t.Run("summarize only errors", func(t *testing.T) {
out := new(bytes.Buffer)
PrintSummary(out, exec, SummarizeErrors)
expected := `
=== Errors
pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
})
}
func patchTimeNow(t *testing.T) {
timeNow = func() time.Time {
return time.Date(2022, 1, 2, 3, 4, 5, 600, time.UTC)
}
t.Cleanup(func() {
timeNow = time.Now
})
}
func multiLine(s string) []string {
return strings.SplitAfter(s, "\n")
}
func TestPrintSummary(t *testing.T) {
patchTimeNow(t)
type testCase struct {
name string
config func(t *testing.T) ScanConfig
expectedOut string
expected func(t *testing.T, exec *Execution)
}
run := func(t *testing.T, tc testCase) {
exec, err := ScanTestOutput(tc.config(t))
assert.NilError(t, err)
buf := new(bytes.Buffer)
PrintSummary(buf, exec, SummarizeAll)
golden.Assert(t, buf.String(), tc.expectedOut)
if tc.expected != nil {
tc.expected(t, exec)
}
}
testCases := []testCase{
{
name: "missing test fail event",
config: scanConfigFromGolden("input/go-test-json-missing-test-fail.out"),
expectedOut: "summary/missing-test-fail-event",
expected: func(t *testing.T, exec *Execution) {
for name, pkg := range exec.packages {
assert.Equal(t, len(pkg.running), 0, "package %v still had tests in running", name)
}
},
},
{
name: "output attributed to wrong test",
config: scanConfigFromGolden("input/go-test-json-misattributed.out"),
expectedOut: "summary/misattributed-output",
},
{
name: "with subtest failures",
config: scanConfigFromGolden("input/go-test-json.out"),
expectedOut: "summary/root-test-has-subtest-failures",
},
{
name: "with parallel failures",
config: scanConfigFromGolden("input/go-test-json-with-parallel-fails.out"),
expectedOut: "summary/parallel-failures",
},
{
name: "missing skip message",
config: scanConfigFromGolden("input/go-test-json-missing-skip-msg.out"),
expectedOut: "summary/bug-missing-skip-message",
},
{
name: "repeated test case",
config: func(t *testing.T) ScanConfig {
in := golden.Get(t, "input/go-test-json.out")
return ScanConfig{
Stdout: io.MultiReader(
bytes.NewReader(in),
bytes.NewReader(in),
bytes.NewReader(in)),
}
},
expectedOut: "summary/bug-repeated-test-case-output",
},
{
name: "with rerun id",
config: func(t *testing.T) ScanConfig {
return ScanConfig{
Stdout: bytes.NewReader(golden.Get(t, "input/go-test-json.out")),
RunID: 7,
}
},
expectedOut: "summary/with-run-id",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
run(t, tc)
})
}
}
func scanConfigFromGolden(filename string) func(t *testing.T) ScanConfig {
return func(t *testing.T) ScanConfig {
return ScanConfig{Stdout: bytes.NewReader(golden.Get(t, filename))}
}
}
|