如何使用go test选择性地测试go模块?

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

How to selective test go modules with go test?

问题

我正在一个庞大的遗留代码库中工作,其中有很多破损的测试,导致运行go test ./...返回了很多失败的测试,而我现在不想专注于这些测试。

我尝试使用+build来针对特定的子包进行测试,但它运行了所有未配置的测试。目前,我不能选择浏览所有的测试。

目前,我只能通过以下方式进行测试:

go test \
-coverprofile=coverage.out \
-tags=moduleB_feature1,moduleA_feature1,moduleA_feature3 \
code_dir/projectX/moduleA/... \
code_dir/projectY/moduleB/feature1

是否有一个命令可以让我忽略所有未配置+build的测试,这样我就可以使用./...

英文:

I'm working in a large legacy codebase with lots of broken tests making running go test ./... returning a lot of failed tests that I don't want to focus on right now.

I tried using +build which is good enough for targeting certain sub-package but it runs all of the test not configured as well. Going through all the tests right now is not an option for me.

Currently I have to do it the long way with:

go test \
-coverprofile=coverage.out \
-tags=moduleB_feature1,moduleA_feature1,moduleA_feature3 \
code_dir/projectX/moduleA/... \
code_dir/projectY/moduleB/feature1

Is there a command for me to ignore all the test without +build configured so I can use ./...?

答案1

得分: 2

你可以运行特定子包的测试。例如,如果你有一个包example.org/your/module/some/package,那么以下命令将执行some/package的测试:

go test example.org/your/module/some/package

它可以与通过正则表达式选择测试相结合:-run开关

go test example.org/your/module/some/package -run `Foo`

将执行与正则表达式Foo匹配的some/package包中的所有测试函数,例如TestFooBar(t *testing.T)

英文:

You can run test for specific sub-package. E.g., if you have package example.org/your/module/some/package then the command

go test example.org/your/module/some/package

executes tests for some/package only.

It could be combined with selecting tests by regular expression: -run switch

go test example.org/your/module/some/package -run `Foo`

executes all test functions from the package some/package that match the regular expression Foo, such as TestFooBar(t *testing.T)

huangapple
  • 本文由 发表于 2022年9月4日 19:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/73598911.html
匿名

发表评论

匿名网友

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

确定