-1

I am new to golang and trying to understand how i can make this scenario work?

Here is my structure GOPATH set to /Users/xyz/project

/Users/xyz/project/src/main.go // import calculator and call SUM with two integeres
/Users/xyz/project/src/main_test.go // test function
/Users/xyz/project/src/calculator/sum.go // SUM function (add two integers)

i have a main go file that imports "calculator" which is a local packages. When i run

go test -cover

it only gives the coverage of main but not for the package "calculator" imported by main. i know i can write a test inside calculator and that would do the trick but is there any way possible to get the coverage of locally imported package from main?

Bigger Context - The reason i want to do this is because i have a micro service written in go using gin framework and i want to spin it up as a service and make http calls and further see how the coverage looks like (like component test). I can easily spin it up by writing a main_test go file which starts the service but i am not getting the coverage of the imported packages.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ashish Sharma
  • 331
  • 1
  • 5
  • 12
  • 1
    There are no "locally imported packages" in Go. There are imported packages and packages which are not imported. – Volker Jun 17 '18 at 09:22
  • @Volker what would you call calculator in the above example? – Ashish Sharma Jun 18 '18 at 12:37
  • 1
    @user1707312: `calculator` is a package just like any other. The fact that it doesn't have a hostname in the path only prevents you from using `go get` on it directly. – JimB Jun 18 '18 at 16:37
  • Agree! Thats what i meant by “locally” imported (because you have to import them before use).And there is a real usecase for having such packages. Not every package you want to host separately from the application as it may make no sense outside of the app. In this case unit test doesnt calculate the coverage of these packages if run form outside of it. The link below has the answer – Ashish Sharma Jun 19 '18 at 22:13

1 Answers1

0

Finally found response here. looks like go has a test binary that can be used https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests

Ashish Sharma
  • 331
  • 1
  • 5
  • 12