Hi! I’m setting up kover in a legacy project befor...
# test
j
Hi! I’m setting up kover in a legacy project before I start breaking apart the spaghetti code. I some modules have 100% class coverage but the other metrics, method, branch, line are at 0% and instruction is at 1,2%. I didn’t find an explanation on what is measured to give those numbers. Are private functions took in account for the total of testable code for example? If yes, should I make them intern so I can test them?
c
class = ok if at least one thing from the class has been called (including constructors, static initializers, etc) → mostly useless method = ok if the method has been called line = ok if the execution went through this line branch = ok if the execution went through this line For example:
Copy code
if (foo) {
    println("a")
    println("b")
}
Here, there are 3 lines and 1 branch (the entire if)
Copy code
foo?.bar ?: return "foo"
Here, there is 1 line and 3 branches (everything not null, foo is null, bar is null)
Are private functions took in account for the total of testable code for example?
Yes
If yes, should I make them intern so I can test them?
No. If a test calls a public function, and that public function calls the private function, then the private function is marked as covered. If the private function is marked as uncovered, that means not a single test went through it, even through public functions. That either means you don't test the public functions, or the private function is dead code.
j
Ok, thanks for the explanation! But it’s quite weird I get so low results then, I do test a good amount of code.
c
Do you use mocks?
j
Yes, we have a lot of them actually. The legacy codebase I work on has a very strong coupling between its components making it difficult to test without mocks
c
I don't know your particular codebase, but a very common mistake people do with mocks is to mock everything. Let's say you have the flow: FooResource → FooService → FooRepository People write a test for FooResource and mock FooService. That test only tests your serialization library, not any of your code! It's a very common thing.
I've also seen people write (pseudo-code, I don't know which mocking lib you use)
Copy code
test {
    mock fooService.create() → Foo("foo")

    assert fooService.create() == Foo("foo")
}
This tests literally none of your codebase, at all, but it's surprisingly common to find in mocked codebases
j
We use mockk, so it’s similar to your example. I guess I need to focus on a particular test class first and see how I can improve it to get much better coverage. Then we can expand to the other classes. Thanks for the help!
I see a lot of generated code included in the report (I’m focusing on generated classes for Room first). I’m trying to exclude them with this
Copy code
reports.filters.excludes {

        annotatedBy("androidx.room.*")
        annotatedBy("*Composable")
        annotatedBy("*Preview")

        annotatedBy("*Generated")
        annotatedBy("org.junit.jupiter.api.Test")
    }
But it doesn’t work. All the class annotated with
@Generated
are still picked up. Do you know how I can prevent Kover of checking them?
c
I thought this would have worked… Maybe ask in #CDLD3845Q or directly in the Kover GitHub?
👍🏻 1