Search code examples
unit-testinggocode-coveragejetbrains-idegoland

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


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<he name of my test>: 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.


Solution

  • 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 ./<path>, it compiles the code in the package contained in ./<path>, 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.