0

I created a couple of Rest API based Microservices running of different ports of a server. Then i wrote a Test code that actually sends a Rest API call over HTTP to serviceA (which in turn calls serviceB), and gets its response, decodes it and compares it against the expected response.
This all works OK, but the test shows 0.0% coverage when i ran it as go test -cover

src/
|--serviceA/
|     |--main.go
|     |--integration_test.go
|--serviceB/
      |--main.go

I manually ran each service.

go run serviceA/main.go   // Listens on port X
go run serviceB/main.go   // Listens on port Y

My integration_test.go has a TestFunc() that literally calls one REST API endpoint of serviceA/main.go using http.Client{} and http.NewRequest() and then examines the response. Response is as expected but coverage of serviceA is 0.0%, which should not be the case as it actually sent some data back so multiple lines were executed.

The services A & B are not a lib package so i couldn't use the -coverpkg cmd as mentioned here

Probably I am looking for tool that can give coverage of a blackbox component/service.

kernelman
  • 992
  • 1
  • 13
  • 28
  • Are these "API calls" made via an HTTP request from one service to the other? – Christian Mar 24 '21 at 07:52
  • Yes. Rest Calls over HTTP. TestCode ----> ServiceA ----> ServiceB – kernelman Mar 24 '21 at 08:05
  • I am not sure that go cover supports coverage with integration tests (via HTTP calls). I thought it was just for units tests / `_test.go` files. Are Service A and Service B, in the same binary as TestCode? – Christian Mar 24 '21 at 08:12
  • you are right. So what is the way to get coverage for integration tests ? – kernelman Mar 24 '21 at 08:13
  • I think the coverage comes from unit tests. And for an integration test, I would think (although I am no expert) to choose an acceptance criteria that _means something_. 100% code coverage doesn't mean much if the application doesn't do what it is supposed to. – Christian Mar 24 '21 at 08:15
  • [Start your servers in the tests](https://golang.org/pkg/net/http/httptest/#NewServer), not in their own processes. You will have to move the handlers/routers out of their main packages so you can import them in the test. – Peter Mar 24 '21 at 10:09
  • @Peter, Thanks for the comment. Calling the handlers directly from test code is the unit test way. I am not sure if this qualifies as a integration test / component test ? – kernelman Mar 24 '21 at 11:40

0 Answers0