Skip to content

testing: add Output #71575

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 62 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
cf4506e
Add a new output writer and use it in t.Log
Feb 5, 2025
dfd2396
Refactor output writer implementation
Feb 12, 2025
a233f16
Fix output indentation
Feb 18, 2025
7ecfb1a
Remove call site from outputWriter
Feb 19, 2025
521b634
Add outputWriter as a common field
Feb 20, 2025
cecfa65
Add outputWriter tests
Mar 5, 2025
a90fc68
Build line as bytes instead of string
Mar 6, 2025
f556750
Name outputWriter fields when initialising
Mar 10, 2025
7785719
Refactor chatty case of writeLine
Mar 10, 2025
6ed9fbd
Address feedback
Mar 10, 2025
53cc952
Refactor outputWriter.Write
Mar 10, 2025
2b3bd9a
Test outputWriter directly
Mar 10, 2025
6f9d54a
Make indent global, rename line length in outputWriter.Write
Mar 10, 2025
2386803
Remove unnecessary variable in outputWriter.Write
Mar 11, 2025
010277f
Improve variable naming
Mar 13, 2025
da48f4d
Use ReplaceAll, TrimSuffix in log
Mar 13, 2025
4a5b4a7
Convert indent to string
Mar 13, 2025
8dda922
Lock common mutex before appending to buffer
Mar 13, 2025
cdd09ba
Add common method setOutputWriter
Mar 14, 2025
90996a3
Refactor writeLine signature
Mar 14, 2025
d4b63d6
Check if test is done in log method
Apr 10, 2025
91934a0
Refactor selection of nesting level for test output
Apr 10, 2025
0eb55d7
Flush outputWriter's buffer when test finishes
Apr 18, 2025
afa16e4
Add outputWriter chatty and json test cases
Apr 21, 2025
d8ebbea
Add Output method
Apr 24, 2025
f9bd701
Select outputWriter in Output
Apr 25, 2025
9972e60
Improve documentation and field naming of outputWriter
Apr 29, 2025
80ed937
Use os.PathSeparator in callSite
Apr 29, 2025
7143d3f
Refactor outputWriter Write method
Apr 29, 2025
9ef20be
Refactor addition of indentation to line
Apr 29, 2025
6818a54
Refactor writeLine
Apr 29, 2025
29df600
Fix setting of indentation in log
Apr 29, 2025
15c7a59
Replace outputWriter with t.Output in some tests
Apr 30, 2025
3a0cc20
Add release notes
Apr 30, 2025
6ea1cd0
Add check for test completion when calling Write
Apr 30, 2025
941ebda
Improve release notes
Apr 30, 2025
7511b1d
Add comments to tests
Apr 30, 2025
0e8712e
Log input type when testing output after complete
Apr 30, 2025
71e5327
Change indent back to string
Apr 30, 2025
cb44232
Simplify callSite
Apr 30, 2025
70322f5
Refactor indentation implementation in writeLine
Apr 30, 2025
4310885
Use Concat instead of Join in Write
Apr 30, 2025
f6b388a
Fix TestOutput
May 1, 2025
8d65366
Check chatty case in TestOutput
May 1, 2025
4cfb37e
Fix formatting of release notes
May 1, 2025
a9398d1
Add test for outputing from a finished sub test
May 2, 2025
1e434ad
Add test for output in finished sub test with chatty
May 2, 2025
da8fbfc
Combine some output tests with TestTRun
May 2, 2025
4e1c73d
Update test for output in finished sub test with chatty
May 2, 2025
0c549d1
Remove some extra output checks
May 2, 2025
16989bd
Remove trailing whitespace from release notes
May 2, 2025
232b539
Flush parent buffers and test output in panic
May 2, 2025
197e54c
Check fuzz function for Ourput
May 2, 2025
28b61ff
Add test to show lack of newline between output and log
May 2, 2025
3aa2470
Update flush comments in tRunner
May 2, 2025
5c7e71f
Fix doc comment about when Output, Write can't be called
May 2, 2025
bdeb45b
Flush buffered log before calling log
May 5, 2025
439381d
Improve method of flushing buffer in log
May 6, 2025
9604771
Lock mutex before checking buffer in log
May 7, 2025
1134425
Lock mutex when flushing partial
May 7, 2025
aec3488
Update Output doc comment
May 7, 2025
18af0e1
Lock mutex when selecting destination test
May 7, 2025
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
2 changes: 2 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
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