Go cover tool - Wrong capital case for package on windows

Hi,

I am using Windows and I did a mistake regarding capital case for package name, like below.

It is: %GOPATH%\src\WrongCapitalCase
But it should be: %GOPATH%\src\wrongCapitalCase

Please note that word wrong should be all lower case, but I put it as Wrong with first letter as upper case by mistake.

When I run tool go test -cover it work, I mean, it build and run, but coverage is 0%.
If I fix the package name (rename directory or change file package instruction) it works fine, it shows the correct coverage.

I know the problem is the wrong package name between files and directory, but my point is why build and run works and cover doesn’t?

File: main.go

package wrongCapitalCase

import "fmt"

// DoSomething : 
func DoSomething()  {
	fmt.Println("init")
}

File: main_test.go

package wrongCapitalCase_test

import "testing"
import "wrongCapitalCase"

func Test1(t *testing.T) {
	wrongCapitalCase.DoSomething()
}

Output:

Running tool: C:\Go\bin\go.exe test -v -race -cover -coverprofile=cover.out -timeout 30s -tags 

=== RUN   Test1
init
--- PASS: Test1 (0.00s)
PASS
coverage: 0.0% of statements
ok  	WrongCapitalCase	1.364s
Success: Tests passed.

I replicated this on a Mac, and I found two alternate ways of testing that do not run into this issue.

  1. Use a whitebox test - ie, where the test package is the same as the package to be tested.

    package wrongCapitalCase
    
    import "testing"
    
    func TestDoSomething(t *testing.T) {
        DoSomething()
    }
    
  2. Use the -coverpkg flag:

    go test -v -race -coverprofile=cover.out -coverpkg=wrongCapitalCase -timeout 30s
    

If you import the package with an uppercase W, then the -coverpkg flag also must use an uppercase W.

It looks like some of the tools or flags used here is unnecessarily case sensitive.

I personally prefer the “whitebox testing” style (option 1).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.