Skip to content

About

Gradle plugin for PIT Mutation Testing in Android projects

Topics

Resources

Stars

81 stars

Watchers

3 watching

Forks

 
 

Repository files navigation

Experimental Gradle plugin for PIT Mutation Testing in Android projects

This is a fork of gradle-pitest-plugin which supports Android gradle projects.

Requirements

  • Android Gradle Plugin 8.5.1 or newer, including AGP 9 and the new Android DSL. The plugin depends on AGP 8.5.1, so builds using an older AGP get it upgraded to 8.5.1.
  • The Gradle version required by your AGP version, Gradle 9 included.
  • JDK 17 or newer.

Supported Android plugins: com.android.application, com.android.library, com.android.dynamic-feature, com.android.test and com.android.kotlin.multiplatform.library.

Applying plugin in build.gradle

plugins {
  id 'pl.droidsonroids.pitest' version '0.3.0'
}
plugins {
  id("pl.droidsonroids.pitest") version "0.3.0"
}

With Maven central repository

Versions 0.2.13 to 0.2.27 were published only to the Gradle Plugin Portal. Use gradlePluginPortal() instead of mavenCentral() for them.

buildscript {
  repositories {
    mavenCentral()
    google()
  }
  dependencies {
    classpath("pl.droidsonroids.gradle:gradle-pitest-plugin:0.3.0")
  }
}

plugins {
  id("com.android.application")
  //or id("com.android.library")
  //or id("com.android.test")
  id("pl.droidsonroids.pitest")
}

Usage

pitest<variant> tasks will be created for each build variant which has unit tests enabled (eg. pitestProDebug for pro product flavor and debug build type). Additionally pitest task will run tasks for all variants.

With AGP 9, unit tests and therefore pitest<variant> tasks exist only for the tested build type (debug by default). Set android.onlyEnableUnitTestForTheTestedBuildType=false in gradle.properties to get them for all build types.

In Kotlin Multiplatform modules (com.android.kotlin.multiplatform.library) the task is named after the Android compilation, eg. pitestAndroidMain.

After the measurements a report created by PIT will be placed in ${PROJECT_DIR}/build/reports/pitest/<variant> directory.

The pitest<variant> tasks are created after the project is evaluated, so tasks.named("pitestDebug") fails in a build script. Configure them lazily instead:

tasks.withType<pl.droidsonroids.gradle.pitest.PitestTask>().configureEach {
    // ...
}

The plugin works only in Android modules. For pure Java/Kotlin JVM modules use the upstream plugin (info.solidsoft.pitest).

For more information see README of source project

Aggregating reports from multiple modules

Apply pl.droidsonroids.pitest.aggregator in the root project and make the modules produce the files it needs:

pitest {
    outputFormats = ['HTML', 'XML']
    exportLineCoverage = true
    timestampedReports = false
}

The aggregator collects the reports of all pitest<variant> tasks that were run, it does not trigger them. To aggregate only one build type, run just those tasks, eg. ./gradlew pitestDebug pitestReportAggregate.

Classpath file

The classpath is passed to PIT in a file (build/pitClasspath<Variant>) by default, to avoid the command line length limit on Windows. To pass it on the command line instead, set useClasspathFile = false.

Incremental analysis with PIT 1.23+

PIT 1.23.0 moved incremental analysis (historyInputLocation, historyOutputLocation, withHistory) into a separate plugin. The default PIT version of this plugin still includes it. If you set pitestVersion to 1.23.0 or newer and use history, add the plugin:

dependencies {
    pitest 'org.pitest:pitest-history-plugin:0.0.1'
}

Robolectric and UnMock

This plugin by default adds mockable Android JAR (generated by Android Gradle Plugin) to classpath used under pitest tests.

If you are using alternative Android framework in tests, like Robolectric or UnMock Gradle Plugin, you may want to add excludeMockableAndroidJar to pitest configuration eg:

pitest {
    targetClasses = ['com.myapp.*']
    excludeMockableAndroidJar = true
}

In such case default mockable Android JAR won't be added and alternative one will be used under tests.

Using jetifier

This plugin tries to discover all the unit test dependencies and add them to the pitest classpath automatically. But, this feature doesn't work with jetifier (android.enableJetifier=true in gradle.properties).

If you are using jetifier and encounter NoClassDefFoundErrors in tests run under pitest, you may want to add some dependencies manually to the pitestTestCompile configuration of the module. For example:

dependencies {
    pitestTestCompile 'io.mockk:mockk-agent-jvm:1.14.11'
}

Troubleshooting

Tests fail when run under pitest but pass without it

Issue occurs when using Android API without mocking it. Pitest verbose logs may list exceptions like ExceptionInitializerError.

The fastest solution is to set android.testOptions.unitTests.returnDefaultValues = true. See Local unit testing documentation to see other consequences of this change.

Releasing

The release process is automated using GitHub Actions and publishes to the Gradle Plugin Portal, Maven Central (via Sonatype Central Portal), and GitHub Releases.

Required GitHub Secrets

Configure the following repository secrets under Settings > Secrets and variables > Actions:

  • GRADLE_PUBLISH_KEY: Gradle Plugin Portal API key
  • GRADLE_PUBLISH_SECRET: Gradle Plugin Portal API secret
  • MAVEN_CENTRAL_USERNAME: Sonatype Central Portal User Token Username
  • MAVEN_CENTRAL_PASSWORD: Sonatype Central Portal User Token Password
  • SIGNING_IN_MEMORY_KEY: PGP private key (ASCII-armored -----BEGIN PGP PRIVATE KEY BLOCK-----)
  • SIGNING_IN_MEMORY_KEY_PASSWORD: Passphrase for the PGP key (optional if unencrypted)
  • SIGNING_IN_MEMORY_KEY_ID: 8-character hex PGP Key ID (optional)

Release Flow

  1. Run the Prepare release workflow on master with the version to release (e.g. 0.2.28, without v prefix). It verifies the version, generates release notes via GitHub API, updates gradle.properties, README.md, and CHANGELOG.md, and pushes branch release/v<version>.
  2. Open the pull request using the link provided in the workflow step summary, add the ignore-for-release label, review the notes and versions, and merge after CI passes.
  3. Run the Release workflow on master with the same version. It validates the merged branch and changelog, executes the full cross-platform CI matrix (Linux and Windows), creates git tag v<version>, creates the GitHub release, and publishes to the Gradle Plugin Portal and Maven Central.

About

Gradle plugin for PIT Mutation Testing in Android projects

Topics

Resources

Stars

81 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages