-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
testing: show diffs for incorrect output from Example tests #41980
Comments
The only way I was able to achieve what I want is via the following bad hack; diff --git a/src/testing/run_example.go b/src/testing/run_example.go
index 10bde49e5b..b1c86f54d2 100644
--- a/src/testing/run_example.go
+++ b/src/testing/run_example.go
@@ -16,6 +16,8 @@ import (
"os"
"strings"
"time"
+
+ "github.com/google/go-cmp/cmp"
)
func runExample(eg InternalExample) (ok bool) {
@@ -54,6 +56,12 @@ func runExample(eg InternalExample) (ok bool) {
os.Stdout = stdout
out := <-outC
+ got := strings.TrimSpace(out)
+ want := strings.TrimSpace(eg.Output)
+ diff := cmp.Diff(want, got)
+ fmt.Println("diff:")
+ fmt.Println(diff)
+
err := recover()
ok = eg.processRunResult(out, timeSpent, err)
}() |
Have you considered the
|
I guess that would work somehow. But I would have to run |
What: - Add example test. That test example is kind of blocked on; 1. golang/go#41980 2. golang/go#5128 (comment)
As another alternative, you could use You could either extract the |
Come to think of it, it would be pretty straightforward to encapsulate the approach from the above comment as an external library. The API could look something like: // RegisterExamples registers the given example functions so that they can be run by test functions.
//
// If this process is a subprocess started by ExampleOutput, RegisterExample
// runs the requested example function and then exits with status 0.
func RegisterExamples(m *testing.M, examples ...func())
// ExampleOutput returns the output of the given example function,
// obtained by executing the test executable as a subprocess.
//
// The function must have been passed to RegisterExamples in TestMain.
//
// If the current platform cannot execute subprocesses, RunExample skips t.
func RunExample(t testing.TB, example func()) (stderr, stdout string, err error)
// ExampleOutput returns the output of example, which must be an Example function
// recognized by 'go test'.
func ExampleOutput(t testing.TB, example func()) (want string) |
There is a trap here, namely adding infinite new API to package testing as individual needs come up. Looking at https://play.golang.org/p/969hijP9sMx, the issue I see is not that testing needs new API but instead that the example test output needs to do a better job highlighting the differences, such as by showing a diff. Maybe we should do that instead? |
That would be excellent. |
Retitled for the suggestion I made last week, namely show diffs. |
What diff package would we use for this? @josharian has been working on https://github.com/pkg/diff, so perhaps that could be vendored as an internal package. |
We can find a reasonable diff package. I have a few sitting around too. |
Based on the discussion, this seems like a likely accept. |
No change in consensus, so accepted. |
Change https://golang.org/cl/301589 mentions this issue: |
I've just filed #51254, which is a pretty similar proposal, FYI :) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Created test example (https://golang.org/pkg/testing/#hdr-Examples, https://blog.golang.org/examples).
A reproducer of the issue is at https://play.golang.org/p/969hijP9sMx
What did you expect to see?
That example test is failing because there is a mismatch between what is been output to stdOut and what the test runner expects the output to be.
Unfortunately for test examples(unlike normal tests), I'm not able to programmatically get the test output so that I can examine(via diffing or otherwise) it and compare it to the expected result.
What did you see instead?
I do not have a way to access(programmatically) the output of the test.
The text was updated successfully, but these errors were encountered: