1 # Builds and runs test binaries, so skip in short mode.
2 [short] skip
3
4 env 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'
11 stdout 'panic'
12 stdout ^FAIL
13
14 # If a test exits with a non-zero status code, 'go test' fails normally.
15 ! go test ./one
16 ! stdout ^ok
17 stdout 'exit status'
18 ! stdout 'panic'
19 stdout ^FAIL
20
21 # Ensure that other flags still do the right thing.
22 go test -list=. ./zero
23 stdout ExitZero
24
25 ! go test -bench=. ./zero
26 stdout 'panic'
27
28 # 'go test' with no args streams output without buffering. Ensure that it still
29 # catches a zero exit with missing output.
30 cd zero
31 ! go test
32 stdout 'panic'
33 cd ../normal
34 go test
35 stdout ^ok
36 cd ..
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.
41 go test ./main_zero
42 ! stdout 'skipping all tests'
43 stdout ^ok
44
45 # With -v, we'll see the warning from TestMain.
46 go test -v ./main_zero
47 stdout 'skipping all tests'
48 stdout ^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.
53 go test -list=. ./main_zero
54 stdout 'skipping all tests'
55 ! stdout TestNotListed
56
57 # Running the test directly still fails, if we pass the flag.
58 go 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'
65 stdout 'panic'
66 stdout '"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'
72 stdout 'panic'
73
74 -- go.mod --
75 module m
76
77 -- ./normal/normal.go --
78 package normal
79 -- ./normal/normal_test.go --
80 package normal
81
82 import "testing"
83
84 func TestExitZero(t *testing.T) {
85 }
86
87 -- ./zero/zero.go --
88 package zero
89 -- ./zero/zero_test.go --
90 package zero
91
92 import (
93 "os"
94 "testing"
95 )
96
97 func TestExitZero(t *testing.T) {
98 os.Exit(0)
99 }
100
101 -- ./one/one.go --
102 package one
103 -- ./one/one_test.go --
104 package one
105
106 import (
107 "os"
108 "testing"
109 )
110
111 func TestExitOne(t *testing.T) {
112 os.Exit(1)
113 }
114
115 -- ./main_zero/zero.go --
116 package zero
117 -- ./main_zero/zero_test.go --
118 package zero
119
120 import (
121 "fmt"
122 "os"
123 "testing"
124 )
125
126 func TestMain(m *testing.M) {
127 fmt.Println("skipping all tests")
128 os.Exit(0)
129 }
130
131 func TestNotListed(t *testing.T) {}
132
View as plain text