如何确定在测试用例期间导入的文件树?

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

How to determine the tree of files which are imported during a test case?

问题

当我在Go中运行测试时,有没有办法让我直接或间接地获取代码导入的文件列表?例如,这可以帮助我在调试失败的测试时排除代码库中某些部分的更改。

另外,通过Git,我们可以找出给定测试中所使用的文件的最低公共祖先git树节点吗?

背景:我正在研究自动化的测试稳定性检测,我希望能够了解每个测试的依赖树,以便更好地检测到不稳定的测试。

例如,如果TestX在代码的版本x上失败,然后稍后更改了同一代码库中根本未被TestX使用的一些文件,然后TestX通过了,我希望能够检测到这是一个不稳定的测试,即使测试套件运行的整体代码库发生了变化。

英文:

When I run a test in Go, is there any way for me to get the list of files that the code imports, directly or indirectly? For example, this could help me rule out changes from certain parts of the codebase when debugging a failing test.

Alternatively, with Git, can we find out what the lowest common ancestor git tree node is for the files exercised in a given test?

Context: I'm looking into automated flakiness detection for my test suite, and I want to be able to know the dependency tree for every test so that I can detect flaky tests better.

For example, if TestX fails for version x of the code, and later on some files in the same codebase which are not used at all by TestX are changed, and then TestX passes, I want to be able to detect that this is a flaky test, even though the overall codebase that the test suite ran on has changed.

答案1

得分: 3

你可能正在寻找的是 go list -test -deps [packages] 命令。

关于这些标志的解释,你可以查看 Go 命令 List packages or modules

-deps

-deps 标志会导致 list 命令遍历指定的包及其所有依赖项。它以深度优先的后序遍历方式访问它们,这样一个包只有在其所有依赖项之后才会被列出。[...]

-test

-test 标志会导致 list 命令不仅报告指定的包,还报告它们的测试二进制文件(对于带有测试的包),以向源代码分析工具准确传达测试二进制文件的构建方式。测试二进制文件的导入路径是包的导入路径后跟一个 ".test" 后缀,例如 "math/rand.test"。[...]

也许我说的很明显,但请记住,list 命令适用于包,而不是单个文件,因此上述命令将包括非测试源文件的依赖项(这应该是你想要的)。

英文:

You are probably looking for go list -test -deps [packages].

For an explanation of what the flags do, you can check Go command List packages or modules:

-deps:
> The -deps flag causes list to iterate over not just the named packages but also all their dependencies. It visits them in a depth-first post-order traversal, so that a package is listed only after all its dependencies. [...]

-test:

> The -test flag causes list to report not only the named packages but also their test binaries (for packages with tests), to convey to source code analysis tools exactly how test binaries are constructed. The reported import path for a test binary is the import path of the package followed by a ".test" suffix, as in "math/rand.test". [...]

Maybe I'll state the obvious, but remember that list works on packages, not single files, so the command above will include dependencies of the non-test sources (which should be what you want anyway).

huangapple
  • 本文由 发表于 2021年8月4日 13:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/68645956.html
匿名

发表评论

匿名网友

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

确定