...
1# Builds and runs test binaries, so skip in short mode.
2[short] skip
3
4env GO111MODULE=on
5
6# If a test invoked by 'go test' exits with a zero status code,
7# it will panic.
8! go test ./zero
9! stdout ^ok
10! stdout 'exit status'
11stdout 'panic'
12stdout ^FAIL
13
14# If a test exits with a non-zero status code, 'go test' fails normally.
15! go test ./one
16! stdout ^ok
17stdout 'exit status'
18! stdout 'panic'
19stdout ^FAIL
20
21# Ensure that other flags still do the right thing.
22go test -list=. ./zero
23stdout ExitZero
24
25! go test -bench=. ./zero
26stdout 'panic'
27
28# 'go test' with no args streams output without buffering. Ensure that it still
29# catches a zero exit with missing output.
30cd zero
31! go test
32stdout 'panic'
33cd ../normal
34go test
35stdout ^ok
36cd ..
37
38# If a TestMain exits with a zero status code, 'go test' shouldn't
39# complain about that. It's a common way to skip testing a package
40# entirely.
41go test ./main_zero
42! stdout 'skipping all tests'
43stdout ^ok
44
45# With -v, we'll see the warning from TestMain.
46go test -v ./main_zero
47stdout 'skipping all tests'
48stdout ^ok
49
50# Listing all tests won't actually give a result if TestMain exits. That's okay,
51# because this is how TestMain works. If we decide to support -list even when
52# TestMain is used to skip entire packages, we can change this test case.
53go test -list=. ./main_zero
54stdout 'skipping all tests'
55! stdout TestNotListed
56
57# Running the test directly still fails, if we pass the flag.
58go test -c -o ./zero.exe ./zero
59! exec ./zero.exe -test.paniconexit0
60
61# Using -json doesn't affect the exit status.
62! go test -json ./zero
63! stdout '"Output":"ok'
64! stdout 'exit status'
65stdout 'panic'
66stdout '"Output":"FAIL'
67
68# Running the test via test2json also fails.
69! go tool test2json ./zero.exe -test.v -test.paniconexit0
70! stdout '"Output":"ok'
71! stdout 'exit status'
72stdout 'panic'
73
74-- go.mod --
75module m
76
77-- ./normal/normal.go --
78package normal
79-- ./normal/normal_test.go --
80package normal
81
82import "testing"
83
84func TestExitZero(t *testing.T) {
85}
86
87-- ./zero/zero.go --
88package zero
89-- ./zero/zero_test.go --
90package zero
91
92import (
93 "os"
94 "testing"
95)
96
97func TestExitZero(t *testing.T) {
98 os.Exit(0)
99}
100
101-- ./one/one.go --
102package one
103-- ./one/one_test.go --
104package one
105
106import (
107 "os"
108 "testing"
109)
110
111func TestExitOne(t *testing.T) {
112 os.Exit(1)
113}
114
115-- ./main_zero/zero.go --
116package zero
117-- ./main_zero/zero_test.go --
118package zero
119
120import (
121 "fmt"
122 "os"
123 "testing"
124)
125
126func TestMain(m *testing.M) {
127 fmt.Println("skipping all tests")
128 os.Exit(0)
129}
130
131func TestNotListed(t *testing.T) {}
View as plain text