How to coverage with GoLand 2023.1.3? I have 31 tests files: either coverage don't run cover what is expected, either it issues a file name too long

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

How to coverage with GoLand 2023.1.3? I have 31 tests files: either coverage don't run cover what is expected, either it issues a file name too long

问题

使用_JetBrains GoLand 2023.1.3_,我正在寻找自定义测试覆盖的方法,因为我无法正确运行覆盖率。

我有一个包含31个测试文件的tests文件夹。

  1. 如果我在IDE中从tests文件夹中运行__go test__,测试会正确执行,但当然没有覆盖率。

  2. 如果我从tests文件夹中使用__带覆盖率的go test__运行,并将测试类型参数化为"Directory",测试会正确执行,但覆盖率奇怪地只覆盖测试而不覆盖代码,因此它会显示:"执行了100%的测试代码"。(这是真的)。

  3. 如果我选择所有的31个测试文件一起运行并进行覆盖率测试,测试会正确执行,但无法进行覆盖率测试,我会收到"文件名太长"的错误,因为_GoLand_发出了一个命令/usr/local/go/bin/go tool test2json,希望将其转储到一个.out文件中,该文件名是所有文件测试名称的连接=>成百上千个字符。
    我无法手动运行_JetBrains GoLand_告诉我正在运行的命令。如果我这样做,我会收到很多bash: QTest<我的测试名称>: command not found错误。

  4. 如果我选择我的测试文件中的几个文件,例如四个或五个,以便连接的输出文件不会太长,_Golang_既可以成功执行测试,也可以进行覆盖率测试。但以这种方式执行无法对我的代码进行完整的覆盖。

我正在寻找一种自定义配置的方法,以便我可以缩短这个过长的.out文件名。但是我在覆盖率的运行配置中没有看到任何内容。

英文:

Using JetBrains GoLand 2023.1.3, I'm searching the way to customise test covering, because I can't run coverage correctly, yet.

I have a tests folder which has 31 test files inside.

  1. If, using the IDE, I Run go test from the tests folder, the tests execute correctly, but of course, there is no coverage.

  2. If I Run go test with coverage from the tests folder, and I parameterize the test kind with "Directory", the tests execute correctly, but the coverage strangely covers only tests, and not the code, so that it respond: "100% of the test code is executed". (That's true).

  3. If I select all the 31 tests files together and do a Run with coverage, the tests execute correctly, but the coverage cannot be done, I receive "File name too long" error, because GoLand issues a command /usr/local/go/bin/go tool test2json wishing to dump into an .out file whose name is the concatenation of all the file tests names => hundred of characters.
    I cannot run manually the command JetBrains GoLand tells me it is running. If I do so, I'm receiving plenty of bash: QTest&lt;he name of my test&gt;: command not found errors.

  4. If I select few files among my tests files, four or five of them so that the concatenated output file isn't too long, Golang both succeed the tests and the coverage. But performing this way the whole coverage of my code isn't possible.

I'm looking for a way to customise the configuration so that this .out file name that is too long, I could shorten it. But I see nothing in the run configuration of the coverage.

答案1

得分: 1

我有一个测试文件夹,里面有31个测试文件。

这是你的问题。

Go工具链希望测试文件与被测试的代码放在同一个目录下。这也是计算覆盖率统计的方式。

当你运行go test ./<path>时,它会编译./<path>中的包,并根据该包计算统计信息。在你的情况下,由于该包中只有测试文件,所以显示的覆盖率为0。

因此,解决方案是将测试文件放在正确的位置。假设你有文件./foo.go和测试文件./test/foo_test.go,只需将后者移动到顶级目录,这样你就会有./foo.go./foo_test.go

英文:

> I have a tests folder which has 31 test files inside.

This is your problem.

The Go toolchain expects tests to live in the same directory with the code it is testing. And this is how coverage stats are calculated.

When you run go test ./&lt;path&gt;, it compiles the code in the package contained in ./&lt;path&gt;, and calculates stats against that package. In your case, since only tests exist in that package, it will show 0 coverage.

So the solution is to put the tests in the correct place. Let's assume you have file ./foo.go and test file ./test/foo_test.go, simply move the latter to the top-level directory so that you instead have ./foo.go and ./foo_test.go.

huangapple
  • 本文由 发表于 2023年7月13日 16:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677425.html
匿名

发表评论

匿名网友

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

确定