1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Test2json converts go test output to a machine-readable JSON stream.
9 // go tool test2json [-p pkg] [-t] [./pkg.test -test.v [-test.paniconexit0]]
11 // Test2json runs the given test command and converts its output to JSON;
12 // with no command specified, test2json expects test output on standard input.
13 // It writes a corresponding stream of JSON events to standard output.
14 // There is no unnecessary input or output buffering, so that
15 // the JSON stream can be read for “live updates” of test status.
17 // The -p flag sets the package reported in each test event.
19 // The -t flag requests that time stamps be added to each test event.
21 // The test must be invoked with -test.v. Additionally passing
22 // -test.paniconexit0 will cause test2json to exit with a non-zero
23 // status if one of the tests being run calls os.Exit(0).
25 // Note that test2json is only intended for converting a single test
26 // binary's output. To convert the output of a "go test" command,
27 // use "go test -json" instead of invoking test2json directly.
31 // The JSON stream is a newline-separated sequence of TestEvent objects
32 // corresponding to the Go struct:
34 // type TestEvent struct {
35 // Time time.Time // encodes as an RFC3339-format string
39 // Elapsed float64 // seconds
43 // The Time field holds the time the event happened.
44 // It is conventionally omitted for cached test results.
46 // The Action field is one of a fixed set of action descriptions:
48 // run - the test has started running
49 // pause - the test has been paused
50 // cont - the test has continued running
51 // pass - the test passed
52 // bench - the benchmark printed log output but did not fail
53 // fail - the test or benchmark failed
54 // output - the test printed output
55 // skip - the test was skipped or the package contained no tests
57 // The Package field, if present, specifies the package being tested.
58 // When the go command runs parallel tests in -json mode, events from
59 // different tests are interlaced; the Package field allows readers to
62 // The Test field, if present, specifies the test, example, or benchmark
63 // function that caused the event. Events for the overall package test
66 // The Elapsed field is set for "pass" and "fail" events. It gives the time
67 // elapsed for the specific test or the overall package test that passed or failed.
69 // The Output field is set for Action == "output" and is a portion of the test's output
70 // (standard output and standard error merged together). The output is
71 // unmodified except that invalid UTF-8 output from a test is coerced
72 // into valid UTF-8 by use of replacement characters. With that one exception,
73 // the concatenation of the Output fields of all output events is the exact
74 // output of the test execution.
76 // When a benchmark runs, it typically produces a single line of output
77 // giving timing results. That line is reported in an event with Action == "output"
78 // and no Test field. If a benchmark logs output or reports a failure
79 // (for example, by using b.Log or b.Error), that extra output is reported
80 // as a sequence of events with Test set to the benchmark name, terminated
81 // by a final event with Action == "bench" or "fail".
82 // Benchmarks have no events with Action == "run", "pause", or "cont".
89 exec
"internal/execabs"
93 "cmd/internal/test2json"
97 flagP
= flag
.String("p", "", "report `pkg` as the package being tested in each event")
98 flagT
= flag
.Bool("t", false, "include timestamps in events")
102 fmt
.Fprintf(os
.Stderr
, "usage: go tool test2json [-p pkg] [-t] [./pkg.test -test.v]\n")
110 var mode test2json
.Mode
112 mode |
= test2json
.Timestamp
114 c
:= test2json
.NewConverter(os
.Stdout
, *flagP
, mode
)
117 if flag
.NArg() == 0 {
121 cmd
:= exec
.Command(args
[0], args
[1:]...)
122 w
:= &countWriter
{0, c
}
128 // Assume command printed why it failed.
130 fmt
.Fprintf(c
, "test2json: %v\n", err
)
141 type countWriter
struct {
146 func (w
*countWriter
) Write(b
[]byte) (int, error
) {