简体   繁体   中英

How to have code coverage for test packages in Go

I'm trying to generate the code coverage for my golang project.

My setup is as follows:

- my_project
|  - my_package
|  |  - my_dev_file.go
|  |  - test
|  |  |  - my_dev_file_test.go

This setup allows to test the code from the point of view of a client that would call the package, without knowing anything about its internal implementation. At the sale time, dev dirs and test dirs are clearly separated which enforces global readability of the project.

The test code looks like this:

import (
    "..."
    "testing"
    "path-to/my_package"
    "..."
)

func TestSomething(t *testing.T) {
    t.Run("should do something", func(t *testing.T) {
       my_package.MyStruct.DoSomething()

       // test something...
    })
}

This setup works fine as far as testing is concerned.

However, i can't seem to be able to generate a coverage report. Coverage is 0% whatever command i launch, starting with:

go test -coverprofile=coverage.out ./.../test
OK path-to/test       0.005s  coverage: 0.0% of statements
OK other-path-to/test 0.007s  coverage: 0.0% of statements

I'm looking for a way to generate proper code coverage without compromising the way the project is organized.

Is it possible to do so?

You should run

go test -coverprofile=coverage.out ./...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM