更新时间:2023-10-21 23:48:16
有两种方法。简单的方法是使用 -run
标志并提供一个匹配要运行的测试名称的模式。
另一种方法是命名包含要运行的测试的特定文件:
$ go test foo_test .go
但有一个问题。如果
$ go test foo_test.go foo.go
我建议使用名称模式。或者,在/可能的情况下,始终运行所有包装测试。
My package test cases are scattered across multiple files, if I run go test <package_name>
it runs all test cases in the package.
It is unnecessary to run all of them though. Is there a way to specify a file for go test
to run, so that it only runs test cases defined in the file?
There are two ways. The easy one is to use the -run
flag and provide a pattern matching names of the tests you want to run.
ex: $ go test -run NameOfTest
. See the docs for more info.
The other way is to name the specific file, containing the tests you want to run:
$ go test foo_test.go
But there's a catch. This works well if
If 'foo_test.go' and 'foo.go' are the same package (a common case), then you must name all other files required to build 'foo_test'. In this example it would be:
$ go test foo_test.go foo.go
I'd recommend to use the name pattern. Or, where/when possible, always run all package tests.