Get Coverage stats when tests are in another package

前端 未结 1 659
傲寒
傲寒 2021-01-12 04:13

My tests aren\'t in the same package as my code. I find this a less cluttered way of organising a codebase with a lot of test files, and I\'ve read that it\'s a good idea in

1条回答
  •  星月不相逢
    2021-01-12 04:40

    As it is mentioned in comments you can run

    go test -cover -coverpkg "api_client" "api_client_tests"

    to run the tests with coverage.

    But splitting code files from tests files to a different directories isn't a Go's way.

    I suppose that you want to have a black-box testing(nothing package-private stuff can be accessible outside, even for tests).

    To accomplish this it's allowed to have tests in another package(without moving the files). Example:

    api_client.go

    package api_client
    
    // will not be accessible outside of the package
    var privateVar = 10
    
    func Method() {
    }
    

    api_client_test.go

    package api_client_tests
    
    import "testing"
    
    func TestClient(t *testing.T) {
        Method()
    }
    

    0 讨论(0)
提交回复
热议问题