英文:
Using go test - Is it possible to run tests for a single package WITHOUT the relative path to the package?
问题
使用go test
命令,有没有一种方法可以在当前项目中运行一个包的测试,而不需要提供相对路径?
例如,我可以运行go test fmt
,无论我的当前工作目录是什么,go
都会查找fmt
包并开始运行它的测试。这是go test
的包列表模式。
然而,我无法弄清楚如何在我的当前项目中运行go test
命令。
例如,给定以下文件夹结构:
mySpecialProject
|--foo
|--bar
|--bizz
|--buzz
我已经知道可以运行go test ./bar/bizz
来运行bizz
包中的测试;我知道可以使用go test -run
来运行特定命名的测试;我也知道可以使用go test ./...
来运行当前目录下所有包的所有测试。但这不是我想要的。我想要的是在项目的根目录下为单个包运行测试,而不需要提供相对路径。
阅读关于go test
的文档,关于包列表模式,它“感觉”应该可以运行go test bizz
或类似的命令来运行bizz包中的测试,而不需要提供相对路径。
然而,当我运行该命令时,它只会显示package bizz is not in GOROOT (/usr/local/Cellar/go/1.17.1/libexec/src/bizz)
。
有什么想法吗?我已经找到了一个解决方法(find . -type d -name "bizz" | xargs go test
),但我更愿意使用内置在go test
中的功能。
英文:
Using go test
- is there a way to run tests for a package inside the current project without providing the relative path?
I can run go test fmt
for example regardless of my current working directory and go
will look around for the fmt
package and will start running its tests. This is go test
in package list mode.
However, I can’t figure out how to run go test
for a package in my current project.
For example - given this folder structure:
mySpecialProject
|--foo
|--bar
|--bizz
|--buzz
I already know that I can run go test ./bar/bizz
to run the tests in the bizz
package; I know about using go test -run
to run specifically named tests; I also know about go test ./...
to run all tests for all packages inside the current directory. That's not what I'm asking for here. I'm looking to run tests from the root of a project for a single package without providing the relative path.
Reading the docs about go test
regarding package list mode, it "feels" like it should work to run go test bizz
or something like it to run the tests in bizz without providing the relative path.
When I run that command however, it just says package bizz is not in GOROOT (/usr/local/Cellar/go/1.17.1/libexec/src/bizz)
Any ideas? I've figured out a workaround (find . -type d -name "bizz" | xargs go test
) but I'd much rather use something built into go test.
答案1
得分: 2
我相当确定这是不可能的。go test
调用PackagesAndErrors函数来处理参数。
通过代码调试,我没有看到任何方法可以实现你想要的原生go test
功能。你正在使用的shell技巧似乎是实现你想要的最佳方式。
由于可能有其他方法解决你的问题,你能描述一下为什么你想要这种特定的行为吗?
英文:
I'm fairly certain this is not possible. go test
calls PackagesAndErrors function to process the args.
Stepping through the code i don't see any way for what you want to work natively to go test
. The shell trickery you are using seems to be the best way to do what you are wanting.
As there might be another way around your issue, can you describe why you are wanting this specific behavior?
答案2
得分: 1
你应该能够执行 go test ./**/bizz
。这是一个称为 globstar 的 shell 功能,所以根据你使用的 shell,这可能对你有效。你可能需要先使用 set -o globstar
或 shopt -o globstar
来启用它。
编辑:
go test ./.../bizz
也可以工作,这是 Go 的一个特性。唯一的区别是,如果 bizz 是当前工作目录的直接子目录,这个命令就不起作用。
英文:
You should be able to execute go test ./**/bizz
. This is a shell feature called globstar, so depending on your shell this might work for you. You may need to enable first with set -o globstar
or shopt -o globstar
.
Edit:
go test ./.../bizz
also works, in this case it is a go feature. Only difference is that this command doesn't work of bizz is a direct sub-directory of the current working dir.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论