dtmbc1606 2016-05-26 14:45
浏览 70
已采纳

当测试在另一个软件包中时,获取覆盖率统计信息

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 order to limit tests to interacting via the package's public api.

So it looks something like this:

api_client:
    Client.go
    ArtistService.go
    ...
api_client_tests
    ArtistService.Events_test.go
    ArtistService.Info_test.go
    UtilityFunction.go
    ...

I can type go test bandsintown-api/api_client_tests -cover and see 0.181s coverage: 100.0% of statements. But that's actually just coverage over my UtilityFunction.go (as I say when I ran go test bandsintown-api/api_client_tests -cover=cover.out and go tool cover -html=cover.out).

Is there any way to get the coverage for the actual api_client package under test, without bringing it all into the same package?

  • 写回答

1条回答 默认 最新

  • duanfengwang9157 2017-06-20 08:01
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题