-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-branch.go1.14] testing: reformat test chatty output
In #24929, we decided to stream chatty test output. It looks like, foo_test.go:138: TestFoo/sub-1: hello from subtest 1 foo_test.go:138: TestFoo/sub-2: hello from subtest 2 In this CL, we refactor the output to be grouped by === CONT lines, preserving the old test-file-before-log-line behavior: === CONT TestFoo/sub-1 foo_test.go:138 hello from subtest 1 === CONT TestFoo/sub-2 foo_test.go:138 hello from subtest 2 This should remove a layer of verbosity from tests, and make it easier to group together related lines. It also returns to a more familiar format (the pre-streaming format), whilst still preserving the streaming feature. Updates #38458. Fixes #39308. Change-Id: Iaef94c580d69cdd541b2ef055aa004f50d72d078 Reviewed-on: https://go-review.googlesource.com/c/go/+/229085 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> Reviewed-by: Andrew Bonventre <andybons@golang.org> Reviewed-on: https://go-review.googlesource.com/c/go/+/242057 Reviewed-by: Jean de Klerk <deklerk@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Run-TryBot: Jean de Klerk <deklerk@google.com>
- Loading branch information
Showing
10 changed files
with
370 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Run chatty tests. Assert on CONT lines. | ||
! go test chatty_test.go -v -bench . chatty_bench | ||
|
||
# Sanity check that output occurs. | ||
stdout -count=2 'this is sub-0' | ||
stdout -count=2 'this is sub-1' | ||
stdout -count=2 'this is sub-2' | ||
stdout -count=1 'error from sub-0' | ||
stdout -count=1 'error from sub-1' | ||
stdout -count=1 'error from sub-2' | ||
|
||
# Benchmarks should not print CONT. | ||
! stdout CONT | ||
|
||
-- chatty_test.go -- | ||
package chatty_bench | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func BenchmarkChatty(b *testing.B) { | ||
for i := 0; i < 3; i++ { | ||
b.Run(fmt.Sprintf("sub-%d", i), func(b *testing.B) { | ||
for j := 0; j < 2; j++ { | ||
b.Logf("this is sub-%d", i) | ||
} | ||
b.Errorf("error from sub-%d", i) | ||
}) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/cmd/go/testdata/script/test_benchmark_chatty_success.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Run chatty tests. Assert on CONT lines. | ||
go test chatty_test.go -v -bench . chatty_bench | ||
|
||
# Sanity check that output happens. We don't provide -count because the amount | ||
# of output is variable. | ||
stdout 'this is sub-0' | ||
stdout 'this is sub-1' | ||
stdout 'this is sub-2' | ||
|
||
# Benchmarks should not print CONT. | ||
! stdout CONT | ||
|
||
-- chatty_test.go -- | ||
package chatty_bench | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func BenchmarkChatty(b *testing.B) { | ||
for i := 0; i < 3; i++ { | ||
b.Run(fmt.Sprintf("sub-%d", i), func(b *testing.B) { | ||
for j := 0; j < 2; j++ { | ||
b.Logf("this is sub-%d", i) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Run chatty tests. Assert on CONT lines. | ||
! go test chatty_test.go -v | ||
|
||
# Sanity check that output occurs. | ||
stdout -count=2 'this is sub-0' | ||
stdout -count=2 'this is sub-1' | ||
stdout -count=2 'this is sub-2' | ||
stdout -count=1 'error from sub-0' | ||
stdout -count=1 'error from sub-1' | ||
stdout -count=1 'error from sub-2' | ||
|
||
# Non-parallel tests should not print CONT. | ||
! stdout CONT | ||
|
||
-- chatty_test.go -- | ||
package chatty_test | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func TestChatty(t *testing.T) { | ||
for i := 0; i < 3; i++ { | ||
t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) { | ||
for j := 0; j < 2; j++ { | ||
t.Logf("this is sub-%d", i) | ||
} | ||
t.Errorf("error from sub-%d", i) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Run parallel chatty tests. Assert on CONT lines. This test makes sure that | ||
# multiple parallel outputs have the appropriate CONT lines between them. | ||
! go test -parallel 3 chatty_parallel_test.go -v | ||
|
||
stdout -count=1 '^=== CONT TestChattyParallel/sub-0\n chatty_parallel_test.go:38: error from sub-0$' | ||
stdout -count=1 '^=== CONT TestChattyParallel/sub-1\n chatty_parallel_test.go:38: error from sub-1$' | ||
stdout -count=1 '^=== CONT TestChattyParallel/sub-2\n chatty_parallel_test.go:38: error from sub-2$' | ||
|
||
# Run parallel chatty tests with -json. Assert on CONT lines as above - make | ||
# sure there are CONT lines before each output line. | ||
! go test -json -parallel 3 chatty_parallel_test.go -v | ||
stdout -count=1 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-0","Output":"=== CONT TestChattyParallel/sub-0\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-0","Output":" chatty_parallel_test.go:38: error from sub-0\\n"}' | ||
stdout -count=1 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-1","Output":"=== CONT TestChattyParallel/sub-1\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-1","Output":" chatty_parallel_test.go:38: error from sub-1\\n"}' | ||
stdout -count=1 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-2","Output":"=== CONT TestChattyParallel/sub-2\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-2","Output":" chatty_parallel_test.go:38: error from sub-2\\n"}' | ||
|
||
-- chatty_parallel_test.go -- | ||
package chatty_paralell_test | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
"flag" | ||
) | ||
|
||
// This test ensures the the order of CONT lines in parallel chatty tests. | ||
func TestChattyParallel(t *testing.T) { | ||
t.Parallel() | ||
|
||
// The number of concurrent tests running. This is closely tied to the | ||
// -parallel test flag, so we grab it from the flag rather than setting it | ||
// to some constant. | ||
parallel := flag.Lookup("test.parallel").Value.(flag.Getter).Get().(int) | ||
|
||
// ready is a synchronization mechanism that causes subtests to execute | ||
// round robin. | ||
ready := make([]chan bool, parallel) | ||
for i := range ready { | ||
ready[i] = make(chan bool, 1) | ||
} | ||
ready[0] <- true | ||
|
||
for i := range ready { | ||
i := i | ||
t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Some basic log output to precede the failures. | ||
<-ready[i] | ||
t.Logf("this is sub-%d", i) | ||
ready[(i+1)%len(ready)] <- true | ||
|
||
// The actual failure messages we care about. | ||
<-ready[i] | ||
t.Errorf("error from sub-%d", i) | ||
ready[(i+1)%len(ready)] <- true | ||
}) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/cmd/go/testdata/script/test_chatty_parallel_success.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Run parallel chatty tests. Assert on CONT lines. This test makes sure that | ||
# multiple parallel outputs have the appropriate CONT lines between them. | ||
go test -parallel 3 chatty_parallel_test.go -v | ||
stdout -count=2 '^=== CONT TestChattyParallel/sub-0\n chatty_parallel_test.go:32: this is sub-0$' | ||
stdout -count=2 '^=== CONT TestChattyParallel/sub-1\n chatty_parallel_test.go:32: this is sub-1$' | ||
stdout -count=2 '^=== CONT TestChattyParallel/sub-2\n chatty_parallel_test.go:32: this is sub-2$' | ||
|
||
# Run parallel chatty tests with -json. Assert on CONT lines as above - make | ||
# sure there are CONT lines before each output line. | ||
go test -json -parallel 3 chatty_parallel_test.go -v | ||
stdout -count=2 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-0","Output":"=== CONT TestChattyParallel/sub-0\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-0","Output":" chatty_parallel_test.go:32: this is sub-0\\n"}' | ||
stdout -count=2 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-1","Output":"=== CONT TestChattyParallel/sub-1\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-1","Output":" chatty_parallel_test.go:32: this is sub-1\\n"}' | ||
stdout -count=2 '{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-2","Output":"=== CONT TestChattyParallel/sub-2\\n"}\n{"Time":"[0-9-TZ:.]{20,40}","Action":"output","Package":"command-line-arguments","Test":"TestChattyParallel/sub-2","Output":" chatty_parallel_test.go:32: this is sub-2\\n"}' | ||
|
||
-- chatty_parallel_test.go -- | ||
package chatty_paralell_test | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
"flag" | ||
) | ||
|
||
// This test ensures the the order of CONT lines in parallel chatty tests. | ||
func TestChattyParallel(t *testing.T) { | ||
t.Parallel() | ||
|
||
// The number of concurrent tests running. This is closely tied to the | ||
// -parallel test flag, so we grab it from the flag rather than setting it | ||
// to some constant. | ||
parallel := flag.Lookup("test.parallel").Value.(flag.Getter).Get().(int) | ||
|
||
// ready is a synchronization mechanism that causes subtests to execute | ||
// round robin. | ||
ready := make([]chan bool, parallel) | ||
for i := range ready { | ||
ready[i] = make(chan bool, 1) | ||
} | ||
ready[0] <- true | ||
|
||
for i := range ready { | ||
i := i | ||
t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) { | ||
t.Parallel() | ||
for j := 0; j < 2; j++ { | ||
<-ready[i] | ||
t.Logf("this is sub-%d", i) | ||
ready[(i+1)%len(ready)] <- true | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Run chatty tests. Assert on CONT lines. | ||
go test chatty_test.go -v | ||
|
||
# Non-parallel tests should not print CONT. | ||
! stdout CONT | ||
|
||
# The assertion is condensed into one line so that it precisely matches output, | ||
# rather than skipping lines and allow rogue CONT lines. | ||
stdout '=== RUN TestChatty\n=== RUN TestChatty/sub-0\n chatty_test.go:12: this is sub-0\n chatty_test.go:12: this is sub-0\n=== RUN TestChatty/sub-1\n chatty_test.go:12: this is sub-1\n chatty_test.go:12: this is sub-1\n=== RUN TestChatty/sub-2\n chatty_test.go:12: this is sub-2\n chatty_test.go:12: this is sub-2\n--- PASS: TestChatty' | ||
|
||
-- chatty_test.go -- | ||
package chatty_test | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func TestChatty(t *testing.T) { | ||
for i := 0; i < 3; i++ { | ||
t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) { | ||
for j := 0; j < 2; j++ { | ||
t.Logf("this is sub-%d", i) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.