Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Coverage stats when tests are in another package

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?

like image 608
Nathan Cooper Avatar asked May 26 '16 14:05

Nathan Cooper


People also ask

How to calculate test coverage?

To calculate test coverage, you need to follow the below-given steps: Step 1) The total lines of code in the piece of software quality you are testing Step 2) The number of lines of code all test cases currently execute Now, you need to find (X divided by Y) multiplied by 100. The result of this calculation is your test coverage %.

How to achieve Test coverage in software testing?

At code level or unit test level, test coverage can be achieved by availing the automated code coverage or unit test coverage tools Functional test coverage can be done with the help of proper test management tools It can help identify what portions of the code were actually touched for the release or fix

What is the difference between integration testing and code coverage testing?

Integration tests on the other hand are much slower, but they can test the application under real conditions, test HTTP protocol interaction, etc. Going back to code coverage, code coverage is nothing more but checking which lines of code were executed during a test run.

What is the difference between code coverage and unit testing?

Unit tests help to ensure functionality, and provide a means of verification for refactoring efforts. Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods.


1 Answers

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()
}
like image 101
Oleg Kovalov Avatar answered Oct 05 '22 20:10

Oleg Kovalov