testing: print test results to standard output

Errors in the code under test go to standard output.
Errors in testing or its usage go to standard error.

R=r
CC=golang-dev
https://golang.org/cl/5374090
This commit is contained in:
Russ Cox 2011-11-15 13:09:19 -05:00
parent 3db596113d
commit a3fb1aec6b
3 changed files with 24 additions and 26 deletions

View file

@ -21,24 +21,23 @@ type InternalExample struct {
func RunExamples(examples []InternalExample) (ok bool) {
ok = true
var eg InternalExample
stdout, stderr := os.Stdout, os.Stderr
defer func() {
os.Stdout, os.Stderr = stdout, stderr
if e := recover(); e != nil {
if err, ok := e.(error); ok {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
panic(e)
fmt.Printf("--- FAIL: %s\npanic: %v\n", eg.Name, e)
os.Exit(1)
}
}()
for _, eg := range examples {
for _, eg = range examples {
if *chatty {
fmt.Fprintln(os.Stderr, "=== RUN:", eg.Name)
fmt.Printf("=== RUN: %s\n", eg.Name)
}
// capture stdout and stderr for testing purposes
// capture stdout and stderr
r, w, err := os.Pipe()
if err != nil {
fmt.Fprintln(os.Stderr, err)
@ -50,7 +49,7 @@ func RunExamples(examples []InternalExample) (ok bool) {
buf := new(bytes.Buffer)
_, err := io.Copy(buf, r)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintf(stderr, "testing: copying pipe: %v\n", err)
os.Exit(1)
}
outC <- buf.String()
@ -67,16 +66,15 @@ func RunExamples(examples []InternalExample) (ok bool) {
out := <-outC
// report any errors
tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9)
if out != eg.Output {
fmt.Fprintf(
os.Stderr,
"--- FAIL: %s\ngot:\n%s\nwant:\n%s\n",
eg.Name, out, eg.Output,
fmt.Printf(
"--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
eg.Name, tstr, out, eg.Output,
)
ok = false
} else if *chatty {
tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9)
fmt.Fprintln(os.Stderr, "--- PASS:", eg.Name, tstr)
fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)
}
}