运行测试并跳过一些软件包

huangapple go评论86阅读模式
英文:

Running tests and skipping some packages

问题

可以通过使用-run标志来跳过特定目录的测试。例如,你可以运行以下命令来测试mypackagemypackage/othermypackage/net,但跳过mypackage/scripts

go test -run="^(mypackage|mypackage/other|mypackage/net)"

这将只运行与指定模式匹配的测试。在这个例子中,模式是^(mypackage|mypackage/other|mypackage/net),它匹配以mypackagemypackage/othermypackage/net开头的测试函数或测试文件。

希望这可以帮助到你!如果你有任何其他问题,请随时问。

英文:

Is it possible to skip directories from testing. For example given the structure below is it possible to test mypackage, mypackage/other and mypackage/net but not mypackage/scripts? I mean without to write a go test command for each (
e.g. go test; go test net; go test other)

mypackage
mypackage/net
mypackage/other
mypackage/scripts

答案1

得分: 51

Go test命令可以在命令行上接受一个要测试的包列表(参见go help packages),因此您可以使用单个调用来测试任意一组包,例如:

go test import/path/to/mypackage import/path/to/mypackage/other import/path/to/mypackage/net

或者,根据您的shell不同,可以这样写:

go test import/path/to/mypackage{,/other,/net}


您可能可以使用go list的有趣调用作为参数(同样取决于您的shell):

go test `go list`

根据您的评论,您希望跳过一个子目录,因此(根据您的shell)可以尝试以下命令:

go test `go list ./... | grep -v directoriesToSkip`

像任何其他命令一样,如果您经常使用该命令,可以为其创建一个shell别名或其他方式。


如果您希望跳过测试的原因是,例如,您有一些长时间/昂贵的测试通常希望跳过,那么测试本身可以检查testing.Short()并根据需要调用t.Skip()

然后,您可以运行以下命令之一:

go test -short import/path/to/mypackage/...

或者在mypackage目录内运行:

go test -short ./...

您还可以使用其他条件触发跳过,而不仅仅是testing.Short()

英文:

Go test takes a list of packages to test on the command line (see go help packages) so you can test an arbitrary set of packages with a single invocation like so:

go test import/path/to/mypackage import/path/to/mypackage/other import/path/to/mypackage/net

Or, depending on your shell:

go test import/path/to/mypackage{,/other,/net}


You might be able to use interesting invocations of go list as the arguments (again, depending on your shell):

go test `go list`

Your comment says you want to skip one sub-directory so (depending on your shell) perhaps this:

go test `go list ./... | grep -v directoriesToSkip`

as with anything, if you do that a lot you could make a shell alias/whatever for it.


If the reason you want to skip tests is, for example, that you have long/expensive tests that you often want to skip, than the tests themselves could/should check testing.Short() and call t.Skip() as appropriate.

Then you could run either:

go test -short import/path/to/mypackage/...

or from within the mypackage directory:

go test -short ./...

You can use things other testing.Short() to trigger skipping as well.

huangapple
  • 本文由 发表于 2015年3月16日 01:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/29064197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定