How to measure Golang integration test coverage?

后端 未结 3 1498
暗喜
暗喜 2021-02-12 19:35

I am trying to use go test -cover to measure the test coverage of a service I am building. It is a REST API and I am testing it by spinning it up, making test HTTP

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-12 20:06

    As far as I know, if you want coverage you need to run go test -cover.

    However it is easy enough to add a flag which you can pass in which will enable these extra tests, so you can make them part of your test suite but don't run them normally.

    So add a command line flag in your whatever_test.go

    var integrationTest = flag.Bool("integration-test", false, "Run the integration tests")
    

    Then in each test do something like this

    func TestSomething(t *testing.T){
        if !*integrationTest {
            t.Skip("Not running integration test")
        }
        // Do some integration testing
    }
    

    Then to run the integration tests

    go run -cover -integration-test
    

提交回复
热议问题