英文:
In Go when using the Example... testing method is there a way to have it show a diff instead of got... want...?
问题
我一直在使用Go语言进行一个较大的项目,并且非常喜欢它。在我的测试中,我一直在使用以下方法:
func ExampleXxx {
... 代码 ...
//Output:
//... 期望的输出 ...
}
当测试失败时,它会显示:
got:
... 一堆显示测试输出的行 ...
want:
... 你放在注释中表示期望的部分 ...
有没有办法只显示差异部分呢?我可以将这两部分复制到不同的文件中并运行diff等命令,但我更希望它只显示错误的部分,因为我的一些测试输出较长。
提前感谢!
编辑:
我正在使用http://golang.org/pkg/testing/#hdr-Examples,并希望输出显示差异而不是当前输出。我知道我可以手动进行diff操作。
英文:
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.
答案1
得分: 3
不,你不能这样做。这不是 Examples 的预期用法。
Examples 是展示某个函数行为的一种好方式:Examples 的存在是为了文档化。验证示例输出的主要原因是确保 Examples 是有效/正确的,而不是验证你的代码是否正确。对于后者,你可以使用测试函数。
大多数情况下,一个 Example 的输出会以每行一个函数/方法调用的输入和输出(或仅输出)的方式显示;有时,Examples 使用不同的行来展示复杂结果的各个部分,例如返回切片的每个元素一行。
我认为你将 Examples 用于“验证程序流程”与 Examples 的意图相矛盾。如果我想要测试大量输入的文本处理器,我会使用测试函数,并使用任何可用的差异工具生成自己的 got、want、diff 输出。
英文:
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.
答案2
得分: 2
如果我正确理解你的问题,似乎GoConvey可以解决这个问题...它是一个在浏览器中运行的TDD工具,可以为大多数失败显示彩色差异:
你可以将它与现有的测试一起使用;如果你不想转换为GoConvey DSL,也没关系。你不必使用TDD工作流程来使其工作,但如果你可以运行go test
,这个工具应该能够捕捉到它。(我从来没有使用过用于测试的Example函数...老实说,我不确定你指的是什么。)
(正在开发一个新的Web界面,仍然会显示差异。请参见下面的图片。)
这些是人为的例子,但显然,对于更长的输出,差异更有用。
这符合你的需求吗?
英文:
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:
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.)
These are contrived examples, but obviously the diff is more useful with longer output.
Is that kind of what you're looking for?
答案3
得分: 0
从样式指南中: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) // 或者使用Fatalf,如果测试无法通过此点进行更多测试
}
这当然只会输出错误。Go语言本身没有内置的工具来显示差异。我认为,将这个输出通过管道传递给你正在使用的任何diff工具,与你的上一次输出进行比较,仍然是最好的方法。
Go语言很棒,但没有理由重新发明已经在终端上出色完成工作的工具。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论