Skip to content

testing: add Output #73703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/next/59928.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg testing, method (*B) Output() io.Writer #59928
pkg testing, method (*F) Output() io.Writer #59928
pkg testing, method (*T) Output() io.Writer #59928
4 changes: 4 additions & 0 deletions doc/next/6-stdlib/99-minor/testing/59928.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- go.dev/issue/59928 -->

The new [Output] method of [testing.T], [testing.B] and [testing.F] provides a Writer
that writes to the same test output stream as [TB.Log], but omits the file and line number.
3 changes: 3 additions & 0 deletions src/testing/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ func (s *benchState) processBench(b *B) {
benchFunc: b.benchFunc,
benchTime: b.benchTime,
}
b.setOutputWriter()
b.run1()
}
r := b.doBench()
Expand Down Expand Up @@ -831,6 +832,7 @@ func (b *B) Run(name string, f func(b *B)) bool {
benchTime: b.benchTime,
bstate: b.bstate,
}
sub.setOutputWriter()
if partial {
// Partial name match, like -bench=X/Y matching BenchmarkX.
// Only process sub-benchmarks, if any.
Expand Down Expand Up @@ -1007,6 +1009,7 @@ func Benchmark(f func(b *B)) BenchmarkResult {
benchFunc: f,
benchTime: benchTime,
}
b.setOutputWriter()
if b.run1() {
b.run()
}
Expand Down
3 changes: 3 additions & 0 deletions src/testing/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func (f *F) Fuzz(ff any) {
t.parent.w = captureOut
}
t.w = indenter{&t.common}
t.setOutputWriter()
if t.chatty != nil {
t.chatty.Updatef(t.name, "=== RUN %s\n", t.name)
}
Expand Down Expand Up @@ -529,6 +530,7 @@ func runFuzzTests(deps testDeps, fuzzTests []InternalFuzzTarget, deadline time.T
fstate: fstate,
}
f.w = indenter{&f.common}
f.setOutputWriter()
if f.chatty != nil {
f.chatty.Updatef(f.name, "=== RUN %s\n", f.name)
}
Expand Down Expand Up @@ -614,6 +616,7 @@ func runFuzzing(deps testDeps, fuzzTests []InternalFuzzTarget) (ok bool) {
tstate: tstate,
}
f.w = indenter{&f.common}
f.setOutputWriter()
if f.chatty != nil {
f.chatty.Updatef(f.name, "=== RUN %s\n", f.name)
}
Expand Down
19 changes: 19 additions & 0 deletions src/testing/panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ func TestPanic(t *testing.T) {
want: `
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
`,
}, {
desc: "subtest panics",
flags: []string{"-test_panic_test=TestPanicHelper/1"},
want: `
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "subtest panics with cleanup",
Expand All @@ -53,8 +56,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "subtest panics with outer cleanup panic",
Expand All @@ -65,6 +70,7 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
`,
}, {
desc: "subtest panics with middle cleanup panic",
Expand All @@ -75,8 +81,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "subtest panics with inner cleanup panic",
Expand All @@ -87,8 +95,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "parallel subtest panics with cleanup",
Expand All @@ -99,8 +109,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "parallel subtest panics with outer cleanup panic",
Expand All @@ -111,6 +123,7 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
`,
}, {
desc: "parallel subtest panics with middle cleanup panic",
Expand All @@ -121,8 +134,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}, {
desc: "parallel subtest panics with inner cleanup panic",
Expand All @@ -133,8 +148,10 @@ ran middle cleanup 1
ran outer cleanup
--- FAIL: TestPanicHelper (N.NNs)
panic_test.go:NNN: TestPanicHelper
TestPanicHelper
--- FAIL: TestPanicHelper/1 (N.NNs)
panic_test.go:NNN: TestPanicHelper/1
TestPanicHelper/1
`,
}}
for _, tc := range testCases {
Expand Down Expand Up @@ -165,6 +182,7 @@ func TestPanicHelper(t *testing.T) {
return
}
t.Log(t.Name())
t.Output().Write([]byte(t.Name()))
if t.Name() == *testPanicTest {
panic("panic")
}
Expand Down Expand Up @@ -195,6 +213,7 @@ func TestPanicHelper(t *testing.T) {
t.Parallel()
}
t.Log(t.Name())
t.Output().Write([]byte(t.Name()))
if chosen {
if *testPanicCleanup {
t.Cleanup(func() {
Expand Down
Loading