Using go test – Is it possible to run tests for a single package WITHOUT the relative path to the package?

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

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 globstarshopt -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.

huangapple
  • 本文由 发表于 2021年11月3日 05:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69816973.html
匿名

发表评论

匿名网友

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

确定