Configuration DSL

Marcel Schnelle edited this page Jul 27, 2018 · 9 revisions

When you apply the android-junit 5 plugin to your project, you will get some additional configuration options for your new power. This configuration can be found as a new category on android.testOptions, and it's called junitPlatform.

apply plugin: "com.android.application"
apply plugin: "de.mannodermaus.android-junit5"

// ...

android {
    testOptions {
        junitPlatform {
            // Here it is!
        }
    }
}

Users of Kotlin DSL, you will be delighted to hear that this configuration object is available to you as a TestOptions extension, located under de.mannodermaus.gradle.plugins.junit5.junitPlatform!

Configuration Parameters

You can pass a Configuration Parameter to the JUnit Platform using the configurationParameter(String, String) method, or its pluralized companion, configurationParameters(Map<String, String>).

android.testOptions {
    junitPlatform {
        configurationParameter "junit.jupiter.testinstance.lifecycle.default", "per_class"
        configurationParameters [
            "some.other.parameter": "a.value",
            "another.parameter": "false"
        ]
    }
}

Per-Variant Test Filters

Arguably the most common usage of configuring unit tests is related to filtering and selecting which tests to actually run. The android-junit5 plugin provides a powerful and expressive language to specify Tags & Engines on each build variant of your project.

To specify rules for all build variants at once, use the filters clause:

android.testOptions {
    junitPlatform {
        filters {
            // An example: Include all test classes that either start with "Test", or end with "Test" or "Tests"
            includePattern "^(Test.*|.+[.$]Test.*|.*Tests?)$"
            excludePattern "^(|.*Ignored?)$"

            // Include all test classes tagged with @Tag("slow"), but exclude those annotated with @Tag("integration")
            includeTags "slow"
            excludeTags "integration"

            // Include the "my-framework" TestEngine, but exclude the "other-framework" TestEngine
            includeEngines "my-framework"
            excludeEngines "other-framework"
        }
    }
}

On top of these global settings, you can specify specialized rules for each build variant, build type or flavor of your project. All clauses follow the xxxFilters scheme, where xxx is the name of a build type, product flavor or build variant. Have a look at the following example:

android {
    // Assume two product flavors, and the default build types, "debug" and "release"
    flavorDimensions = ["my-dimension"]
    productFlavors {
        flavor1 {
            dimension "my-dimension"
        }
        flavor2 {
            dimension "my-dimension"
        }
    }

    testOptions {
        junitPlatform {
            filters {
                // Exclude all "slow" tests by default
                excludeTags "slow"
            }
            debugFilters {
                // Include all test classes tagged with @Tag("slow"), but only for the flavor1Debug & flavor2Debug variants.
                // This will overwrite the value inherited from the "filters" closure!
                includeTags "slow"
            }
            flavor1ReleaseFilters {
                // Exclude the "staging-engine" TestEngine, but only for the flavor1Release variant
                excludeEngines "staging-engine"
            }
        }
    }
}

The different specialization levels take priority over each other. From top to bottom, the following list shows the order of how the filters are applied to a build variant. Settings applied at a later point in the list overwrite contrarian settings applied before then:

  1. Default
  2. Build-type-specific (e.g. "debug")
  3. Flavor-specific (e.g. "flavor1")
  4. Variant-specific (e.g. "flavor1Debug")

Users of Kotlin DSL can utilize the filters(String, FiltersExtension.() -> Unit) method on the plugin's extension object to achieve the same effect! For instance, to apply filters for all debug build variants, use filters("debug") { // ... }!

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.