英文:
How to run test cases in a specified file?
问题
我的软件包测试用例分散在多个文件中,如果我运行go test <package_name>
,它会运行软件包中的所有测试用例。
然而,运行所有测试用例是不必要的。有没有办法指定一个文件给go test
运行,这样它只会运行文件中定义的测试用例?
英文:
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?
答案1
得分: 515
有两种方法。简单的方法是使用-run
标志,并提供一个匹配要运行的测试名称的模式。
示例:
$ go test packageName -run NameOfTest
有关更多信息,请参阅文档。
请注意,如果其他测试包含字符串NameOfTest
,-run
标志也可能运行其他测试,因为-run
标志匹配一个正则表达式。
因此,为了确保只运行一个名为'NameOfTest'的测试,
必须使用正则表达式^NameOfTest$
:
$ go test -run "^NameOfTest$"
另一种方法是命名包含要运行的测试的特定文件:
$ go test foo_test.go
但是有一个问题。如果满足以下条件,这种方法效果很好:
foo.go
在package foo
中。foo_test.go
在package foo_test
中,并且导入'foo'。
如果foo_test.go
和foo.go
是同一个包(常见情况),那么您必须命名构建foo_test
所需的所有其他文件。在这个例子中,它将是:
$ go test foo_test.go foo.go
我建议使用-run
模式。或者,在可能的情况下,始终运行所有包测试。
英文:
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.
Example:
$ go test packageName -run NameOfTest
See the docs for more info.
Note that the -run
flag may also run other tests if they contain the string NameOfTest
, as the -run
flag matches a regexp.
So to ensure that only a test named exactly 'NameOfTest' is run,
one has to use the regexp ^NameOfTest$
:
$ go test -run "^NameOfTest$"
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:
foo.go
is inpackage foo
.foo_test.go
is inpackage foo_test
and imports 'foo'.
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 -run
pattern. Or, where/when possible, always run all package tests.
答案2
得分: 138
@zzzz的答案基本上是完整的,但为了不让其他人必须查阅引用的文档,您可以按照以下方式运行一个包中的单个测试:
go test 包名 -run 测试名称
请注意,您要传递的是测试的名称,而不是测试所在的文件名。
-run
标志实际上接受一个正则表达式,因此您可以将测试运行限制为一类测试。根据文档:
-run regexp
仅运行与正则表达式匹配的测试和示例。
英文:
@zzzz's answer is mostly complete, but just to save others from having to dig through the referenced documentation you can run a single test in a package as follows:
go test packageName -run TestName
Note that you want to pass in the name of the test, not the file name where the test exists.
The -run
flag actually accepts a regex so you could limit the test run to a class of tests. From the docs:
-run regexp
Run only those tests and examples matching the regular
expression.
答案3
得分: 29
当运行单个测试时,我通常会执行以下操作:
go test -run TestSomethingReallyCool ./folder1/folder2/ -v -count 1
-count 1
还确保每次都运行测试,而不是缓存。当您在测试中遇到竞争条件并且有时测试失败时,这非常有用。在不使用模块的Go版本中,可以通过设置GOCACHE=off
来实现相同的效果,但这与Go模块的交互效果不佳。
英文:
When running a single test I usually do:
<!-- language: bash -->
go test -run TestSomethingReallyCool ./folder1/folder2/ -v -count 1
-count 1
also ensures that the test is ran every time instead of being cached. Useful when you are testing against race conditions and have a test that fails only sometimes. In Go versions not using modules the same could be achieved by setting GOCACHE=off
but this interacts poorly with Go modules.
答案4
得分: 7
防止测试结果缓存。
go test -count=1 ./<package_name> -run Test
英文:
go test -v ./<package_name> -run Test
Prevents caching of test results.
go test -count=1 ./<package_name> -run Test
答案5
得分: 7
Visual Studio Code在Go测试文件的顶部显示一个链接,让您可以仅运行该文件中的所有测试。
在“输出”窗口中,您可以看到它自动生成了一个包含当前文件中所有测试名称的正则表达式:
Running tool: C:\Go\bin\go.exe test -timeout 30s -run ^(TestFoo|TestBar|TestBaz)$ rootpackage\mypackage
注意:在VS Code中首次打开Go文件时,它会自动提供安装一些Go扩展的选项。我假设上述操作需要您之前接受了安装的提议。
英文:
Visual Studio Code shows a link at the top of a Go test file which lets you run all the tests in just that file.
In the "Output" window, you can see that it automatically generates a regex which contains all of the test names in the current file:
Running tool: C:\Go\bin\go.exe test -timeout 30s -run ^(TestFoo|TestBar|TestBaz)$ rootpackage\mypackage
Note: the very first time you open a Go file in VS Code it automatically offers to install some Go extensions for you. I assume the above requires that you have previously accepted the offer to install.
答案6
得分: 5
alias testcases="sed -n 's/func.(Test.)(.*/\1/p' | xargs | sed 's/ /|/g'"
go test -v -run $(cat coordinator_test.go | testcases)
英文:
alias testcases="sed -n 's/func.*\(Test.*\)(.*//p' | xargs | sed 's/ /|/g'"
go test -v -run $(cat coordinator_test.go | testcases)
答案7
得分: 0
go test -v -timeout 30s <path_to_package> -run ^(TestFuncRegEx)
- TestFunc必须在该包中的
go
测试文件中 - 我们可以提供一个正则表达式来匹配一组测试用例,或者只是精确匹配一个测试用例函数来运行单个测试用例。例如
-run TestCaseFunc
英文:
go test -v -timeout 30s <path_to_package> -run ^(TestFuncRegEx)
- The TestFunc must be inside a
go
test file in that package - We can provide a regular expression to match a set of test cases or just the exact test case function to run a single test case. For instance
-run TestCaseFunc
答案8
得分: 0
我在运行包中的一个特定测试时遇到了一些问题,这是我解决的方法:
go test -v -race ./parsers/xml2csv/stld/ -timeout 30s -run 'Test_stldItemParser_readNodes'
如果我不使用单引号,它就无法工作。我在Ubuntu-20.04上使用oh-my-zsh
。
这里:
-v 用于显示详细信息
-race 用于检测竞态条件
-timeout 用于设置测试的超时时间
这些是可选的,可以省略,但最好添加上。
英文:
I was facing some issue to run a single specific test from my package this is how I solved
go test -v -race ./parsers/xml2csv/stld/ -timeout 30s -run '^Test_stldItemParser_readNodes$'
If I run without single quote it wasn't working I use oh-my-zsh
in Ubuntu-20.04
Here
-v for verbosity
-race to detect race condition
-timeout for the timeout of the test
This are optional can be omitted but good to add.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论