TODO: account for time taken in Cleanup().
Этот коммит содержится в:
Dan Kegel 2022-01-14 09:25:40 -08:00 коммит произвёл Ron Evans
родитель 78a36f7724
коммит 0ed34e3cb0
3 изменённых файлов: 41 добавлений и 18 удалений

Просмотреть файл

@ -15,6 +15,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"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{'.', '.', '\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.
fail := false

Просмотреть файл

@ -15,6 +15,7 @@ import (
"io"
"os"
"strings"
"time"
)
// Testing flags.
@ -51,9 +52,11 @@ type common struct {
cleanups []func() // optional functions to be called at the end of the test
finished bool // Test function has completed.
parent *common
level int // Nesting depth of test or benchmark.
name string // Name of test or benchmark.
parent *common
level int // Nesting depth 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.
@ -73,6 +76,11 @@ func Verbose() bool {
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.
type TB 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)
}
t.start = time.Now()
fn(t)
t.duration += time.Since(t.start) // TODO: capture cleanup time, too.
// Process the result (pass or fail).
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())
}
}
t.report() // Report after all subtests have 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
}
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.
// Although the return value has type float64, it will always be an integral
// value.

4
testdata/testing.txt предоставленный
Просмотреть файл

@ -1,10 +1,10 @@
--- FAIL: TestBar
--- FAIL: TestBar (0.00s)
log Bar
log g
h
i
--- FAIL: TestBar/Bar2
--- FAIL: TestBar/Bar2 (0.00s)
log Bar2
a
b