In Go when using the Example... testing method is there a way to have it show a diff instead of got... want...?

StackOverflow https://stackoverflow.com/questions/22158782

  •  19-10-2022
  •  | 
  •  

Question

I've been using go for a bigger project and love it, and for my testing i've been using the

func ExampleXxx {
    ... code ...
    //Output:
    //...expected output ...
}

method for testing. When it fails it will say

got:
... bunch of lines showing the output of test ...
want:
... the comment you put in to show what you expected ...

is there any way to make it show just the difference? I can take the two and copy to separate files and run a diff etc, but I'd much rather just have it show the parts that were wrong as some of my tests have longer output.

Thanks in advance

EDIT:

I'm using http://golang.org/pkg/testing/#hdr-Examples and want the output to show a diff not the current output. I know I can do the diff manually.

Was it helpful?

Solution

No, you cannot do this. This is not the intended use of Examples.

Examples are a nice way to show how some function will behave: Examples exists to document. The main reason for validating the example output is to make sure the Examples are valid/correct, not that your code is okay. For the later you have Test functions

Most often the output of an Example displays input and output (or just output) of one invocation of a certain function/method per line; sometimes Examples use the different lines to show parts of a complex result, e.g. one line per element of the returned slice.

I think your use of Examples to "verify the flow of my program" contradicts the intention of Examples. I would use Test functions and use any of the available diff tools to generate a got, want, diff output myself if I'd like to test e.g. a text processor on large bunches of input.

OTHER TIPS

If I understand your question correctly, it sounds like GoConvey would do the trick... It's a TDD tool that runs in the browser, and it will show you colored diffs for most failures:

GoConvey failure

You can use it with your existing tests; if you don't want to convert to the GoConvey DSL, that's okay. You don't have to be using a TDD workflow per-se in order for it to work, but if you can run go test, this tool should be able to pick it up. (I've never used the Example functions for testing... I'm not sure what you mean by that, honestly.)

(There's a new web UI in the works that will still show the diff. See below.)

GoConvey new UI

These are contrived examples, but obviously the diff is more useful with longer output.

Is that kind of what you're looking for?

From the style guide: https://code.google.com/p/go-wiki/wiki/Style#Useful_Test_Failures

if got != tt.want {
     t.Errorf("Foo(%q) = %d; want %d", tt.in, got, tt.want)    // or Fatalf, if test can't test anything more past this point
}

This will put out only errors of course. There is nothing built into go that lets you show the diff. I think just piping this to whatever diff tool you are using against your last output would still be best.

Go is great, but no reason to re-invent tools that already do fantastic jobs that already exists at the terminal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top