Skip to content

Conversation

@paulkagiri
Copy link
Contributor

@paulkagiri paulkagiri commented Aug 30, 2021

Code Coverage Plugin

Intro

  • Code coverage is a software metric used to measure how many lines of our code are executed during automated tests.

  • JaCoCo is a free Java code coverage tool and the JaCoCo plugin provides code coverage metrics for Java code via integration with JaCoCo.

Why

In order to generate JaCoCo unit test coverage reports for Android projects you need to create JacocoReport tasks and configure them by providing paths to source code, execution data and compiled classes. It's not straightforward since Android projects can have different flavors and build types thus requiring additional paths to be set. This plugin configures these JacocoReport tasks automatically.

Usage

plugins {
    ...
    id 'com.gradle.plugin-publish' version '0.14.0' // or whatever version is most recent
}

codeCoverageReport{
    html.enabled = true // by default it's true
    xml.enabled = true // by default it's true
    csv.enabled = true // by default it's false

    unitTests.enabled = true // whether code coverage tasks for unit tests will be generated
    androidTests.enabled = true // whether code coverage tasks for instrumentation tests will be generated

    excludeFlavors = [''] // the product flavors to exclude when generating the code coverage tasks

    excludeClasses = [''] // additional classes to exclude - most are already catered for

    destination = '/some/other/directory' // if you want to configure a custom path to save the code coverage reports, by default your report gets saved in `[project]/build/jacoco/{flavor}{build type}{project}{test type}CoverageReport`

    includeNoLocationClasses = true // To include Robolectric tests in the Jacoco report this needs to be true
}

android {
    buildTypes {
        debug {
            testCoverageEnabled true // this instructs the plugin to generate code coverage reports for this build type
            ...
        }
        release {
            testCoverageEnabled false // this instructs the plugin to NOT generate code coverage reports for this build type
            ...
        }
    }
    ...
    productFlavors {
        local {}
        dist {}
    }
}

The above configuration creates a JacocoReport task for each variant in the form of {flavor}{build type}{project}{test type}CoverageReport

distDebugAppAndroidTestCoverageReport
distDebugAppUnitTestCoverageReport
localDebugAppAndroidTestCoverageReport
localDebugAppUnitTestCoverageReport

By default these are the excluded classes under Constants

@paulkagiri paulkagiri requested a review from a team as a code owner August 30, 2021 16:23
@AdamBJohnsonx
Copy link
Contributor

We should have a description indicating what this does, precisely, in the PR description, and links to the documentation of the features used in the code comments, so future authors can have a starting place to go when looking at this code.

@paulkagiri paulkagiri changed the title [WIP] Code Coverage Plugin Code Coverage Plugin Sep 3, 2021
@shahzaibj
Copy link
Contributor

An overall comment, was there a specific reason for using kotlin here?

I mean its nice, but I think we also need to get the team onboarded with kotlin as well.

@shahzaibj
Copy link
Contributor

How would this plugin work on Non-Android gradle modules such as common4j or broker4j? Have we tested it?

@paulkagiri
Copy link
Contributor Author

An overall comment, was there a specific reason for using kotlin here?

I mean its nice, but I think we also need to get the team onboarded with kotlin as well.

I agree with you, will arrange a brownbag on Kotlin

@paulkagiri
Copy link
Contributor Author

How would this plugin work on Non-Android gradle modules such as common4j or broker4j? Have we tested it?

Non-Android projects support is being added

@paulkagiri
Copy link
Contributor Author

How would this plugin work on Non-Android gradle modules such as common4j or broker4j? Have we tested it?

The support for Java projects is now available

Copy link
Contributor

@AdamBJohnsonx AdamBJohnsonx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments. If it works, I'm for it.

@shahzaibj
Copy link
Contributor

Were we able to verify that just running the tests doesn't run coverage? (basically only code coverage specific gradle tasks will run the coverage)

Copy link
Contributor

@shahzaibj shahzaibj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks fine to me. I think we just need to see it working e2e now.

@paulkagiri
Copy link
Contributor Author

Cool, I will go ahead and merge this in, thanks @AdamBJohnsonx & @shahzaibj

@paulkagiri
Copy link
Contributor Author

Were we able to verify that just running the tests doesn't run coverage? (basically only code coverage specific gradle tasks will run the coverage)

Yeah, these changes made that possible - c1145c5

paulkagiri and others added 2 commits October 5, 2021 12:26
…system/codecov/CodeCoverageReportExtension.kt

Co-authored-by: Shahzaib <37125644+shahzaibj@users.noreply.github.com>
@paulkagiri paulkagiri merged commit b448c91 into master Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants