testing: print duration
TODO: account for time taken in Cleanup().
Этот коммит содержится в:
родитель
78a36f7724
коммит
0ed34e3cb0
3 изменённых файлов: 41 добавлений и 18 удалений
|
@ -15,6 +15,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -426,6 +427,11 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
|
||||||
actual = bytes.Replace(actual, []byte{0x1b, '[', '0', 'm'}, nil, -1)
|
actual = bytes.Replace(actual, []byte{0x1b, '[', '0', 'm'}, nil, -1)
|
||||||
actual = bytes.Replace(actual, []byte{'.', '.', '\n'}, []byte{'\n'}, -1)
|
actual = bytes.Replace(actual, []byte{'.', '.', '\n'}, []byte{'\n'}, -1)
|
||||||
}
|
}
|
||||||
|
if name == "testing.go" {
|
||||||
|
// Strip actual time.
|
||||||
|
re := regexp.MustCompile(`\([0-9]\.[0-9][0-9]s\)`)
|
||||||
|
actual = re.ReplaceAllLiteral(actual, []byte{'(', '0', '.', '0', '0', 's', ')'})
|
||||||
|
}
|
||||||
|
|
||||||
// Check whether the command ran successfully.
|
// Check whether the command ran successfully.
|
||||||
fail := false
|
fail := false
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Testing flags.
|
// Testing flags.
|
||||||
|
@ -54,6 +55,8 @@ type common struct {
|
||||||
parent *common
|
parent *common
|
||||||
level int // Nesting depth of test or benchmark.
|
level int // Nesting depth of test or benchmark.
|
||||||
name string // Name of test or benchmark.
|
name string // Name of test or benchmark.
|
||||||
|
start time.Time // Time test or benchmark started
|
||||||
|
duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Short reports whether the -test.short flag is set.
|
// Short reports whether the -test.short flag is set.
|
||||||
|
@ -73,6 +76,11 @@ func Verbose() bool {
|
||||||
return flagVerbose
|
return flagVerbose
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fmtDuration returns a string representing d in the form "87.00s".
|
||||||
|
func fmtDuration(d time.Duration) string {
|
||||||
|
return fmt.Sprintf("%.2fs", d.Seconds())
|
||||||
|
}
|
||||||
|
|
||||||
// TB is the interface common to T and B.
|
// TB is the interface common to T and B.
|
||||||
type TB interface {
|
type TB interface {
|
||||||
Error(args ...interface{})
|
Error(args ...interface{})
|
||||||
|
@ -265,21 +273,11 @@ func tRunner(t *T, fn func(t *T)) {
|
||||||
fmt.Fprintf(t.w, "=== RUN %s\n", t.name)
|
fmt.Fprintf(t.w, "=== RUN %s\n", t.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.start = time.Now()
|
||||||
fn(t)
|
fn(t)
|
||||||
|
t.duration += time.Since(t.start) // TODO: capture cleanup time, too.
|
||||||
|
|
||||||
// Process the result (pass or fail).
|
t.report() // Report after all subtests have finished.
|
||||||
if t.failed {
|
|
||||||
if t.parent != nil {
|
|
||||||
t.parent.failed = true
|
|
||||||
}
|
|
||||||
fmt.Fprintf(t.w, t.indent+"--- FAIL: %s\n", t.name)
|
|
||||||
t.w.Write(t.output.Bytes())
|
|
||||||
} else {
|
|
||||||
if flagVerbose {
|
|
||||||
fmt.Fprintf(t.w, t.indent+"--- PASS: %s\n", t.name)
|
|
||||||
t.w.Write(t.output.Bytes())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs f as a subtest of t called name. It waits until the subtest is finished
|
// Run runs f as a subtest of t called name. It waits until the subtest is finished
|
||||||
|
@ -381,6 +379,25 @@ func (m *M) Run() int {
|
||||||
return failures
|
return failures
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *T) report() {
|
||||||
|
dstr := fmtDuration(t.duration)
|
||||||
|
format := t.indent + "--- %s: %s (%s)\n"
|
||||||
|
if t.Failed() {
|
||||||
|
if t.parent != nil {
|
||||||
|
t.parent.failed = true
|
||||||
|
}
|
||||||
|
fmt.Fprintf(t.w, format, "FAIL", t.name, dstr)
|
||||||
|
t.w.Write(t.output.Bytes())
|
||||||
|
} else if flagVerbose {
|
||||||
|
if t.Skipped() {
|
||||||
|
fmt.Fprintf(t.w, format, "SKIP", t.name, dstr)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(t.w, format, "PASS", t.name, dstr)
|
||||||
|
}
|
||||||
|
t.w.Write(t.output.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// AllocsPerRun returns the average number of allocations during calls to f.
|
// AllocsPerRun returns the average number of allocations during calls to f.
|
||||||
// Although the return value has type float64, it will always be an integral
|
// Although the return value has type float64, it will always be an integral
|
||||||
// value.
|
// value.
|
||||||
|
|
4
testdata/testing.txt
предоставленный
4
testdata/testing.txt
предоставленный
|
@ -1,10 +1,10 @@
|
||||||
--- FAIL: TestBar
|
--- FAIL: TestBar (0.00s)
|
||||||
log Bar
|
log Bar
|
||||||
log g
|
log g
|
||||||
h
|
h
|
||||||
i
|
i
|
||||||
|
|
||||||
--- FAIL: TestBar/Bar2
|
--- FAIL: TestBar/Bar2 (0.00s)
|
||||||
log Bar2
|
log Bar2
|
||||||
a
|
a
|
||||||
b
|
b
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче