Stay organized with collections
Save and categorize content based on your preferences.
UnitTestOptions
interface UnitTestOptions
Options for controlling unit tests execution.
Summary
Public functions |
Unit |
Configures all unit testing tasks.
|
Public properties |
Boolean |
Enables unit tests to use Android resources, assets, and manifests.
|
Boolean |
Whether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null).
|
Public functions
all
fun all(configAction: (Test) -> Unit): Unit
Configures all unit testing tasks.
See Test
for available options.
Inside the closure you can check the name of the task to configure only some test tasks, e.g.
android {
testOptions {
unitTests.all {
if (it.name == 'testDebug') {
systemProperty 'debug', 'true'
}
}
}
}
since 1.2.0
Public properties
isIncludeAndroidResources
var isIncludeAndroidResources: Boolean
Enables unit tests to use Android resources, assets, and manifests.
If you set this property to true
, the plugin performs resource, asset, and manifest merging before running your unit tests. Your tests can then inspect a file called com/android/tools/test_config.properties
on the classpath, which is a Java properties file with the following keys:
android_resource_apk
: the path to the APK-like zip file containing merged resources, which includes all the resources from the current subproject and all its dependencies. This property is available by default, or if the Gradle property android.enableUnitTestBinaryResources
is set to true
.
android_merged_resources
: the path to the directory containing merged resources, which includes all the resources from the current subproject and all its dependencies. This property is available only if the Gradle property android.enableUnitTestBinaryResources
is set to false
.
android_merged_assets
: the path to the directory containing merged assets. For app subprojects, the merged assets directory contains assets from the current subproject and its dependencies. For library subprojects, the merged assets directory contains only assets from the current subproject.
android_merged_manifest
: the path to the merged manifest file. Only app subprojects have the manifest merged from their dependencies. Library subprojects do not include manifest components from their dependencies.
android_custom_package
: the package name of the final R class. If you modify the application ID in your build scripts, this package name may not match the package
attribute in the final app manifest.
Note that starting with version 3.5.0, if the Gradle property android.testConfig.useRelativePath
is set to true
, the paths above will be relative paths (relative to the current project directory, not the root project directory); otherwise, they will be absolute paths. Prior to version 3.5.0, the paths are all absolute paths.
since 3.0.0
isReturnDefaultValues
var isReturnDefaultValues: Boolean
Whether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null).
See Test Your App for details.
since 1.1.0
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# UnitTestOptions\n===============\n\n\n```\ninterface UnitTestOptions\n```\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nOptions for controlling unit tests execution.\n\nSummary\n-------\n\n| ### Public functions ||\n|------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [all](/reference/tools/gradle-api/7.4/com/android/build/api/dsl/UnitTestOptions#all(kotlin.Function1))`(configAction: (`[Test](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html)`) `-\u003e` `[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)`)` Configures all unit testing tasks. |\n\n| ### Public properties ||\n|------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isIncludeAndroidResources](/reference/tools/gradle-api/7.4/com/android/build/api/dsl/UnitTestOptions#isIncludeAndroidResources()) Enables unit tests to use Android resources, assets, and manifests. |\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isReturnDefaultValues](/reference/tools/gradle-api/7.4/com/android/build/api/dsl/UnitTestOptions#isReturnDefaultValues()) Whether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null). |\n\nPublic functions\n----------------\n\n### all\n\n```\nfun all(configAction: (Test) -\u003e Unit): Unit\n```\n\nConfigures all unit testing tasks.\n\nSee [Test](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html) for available options.\n\nInside the closure you can check the name of the task to configure only some test tasks, e.g. \n\n```text\nandroid {\n testOptions {\n unitTests.all {\n if (it.name == 'testDebug') {\n systemProperty 'debug', 'true'\n }\n }\n }\n}\n```\n\nsince 1.2.0\n\nPublic properties\n-----------------\n\n### isIncludeAndroidResources\n\n```\nvar isIncludeAndroidResources: Boolean\n```\n\nEnables unit tests to use Android resources, assets, and manifests.\n\nIf you set this property to `true`, the plugin performs resource, asset, and manifest merging before running your unit tests. Your tests can then inspect a file called `com/android/tools/test_config.properties` on the classpath, which is a Java properties file with the following keys:\n\n`android_resource_apk`: the path to the APK-like zip file containing merged resources, which includes all the resources from the current subproject and all its dependencies. This property is available by default, or if the Gradle property `android.enableUnitTestBinaryResources` is set to `true`.\n\n`android_merged_resources`: the path to the directory containing merged resources, which includes all the resources from the current subproject and all its dependencies. This property is available only if the Gradle property `android.enableUnitTestBinaryResources` is set to `false`.\n\n`android_merged_assets`: the path to the directory containing merged assets. For app subprojects, the merged assets directory contains assets from the current subproject and its dependencies. For library subprojects, the merged assets directory contains only assets from the current subproject.\n\n`android_merged_manifest`: the path to the merged manifest file. Only app subprojects have the manifest merged from their dependencies. Library subprojects do not include manifest components from their dependencies.\n\n`android_custom_package`: the package name of the final R class. If you modify the application ID in your build scripts, this package name may not match the `package` attribute in the final app manifest.\n\nNote that starting with version 3.5.0, if the Gradle property `android.testConfig.useRelativePath` is set to `true`, the paths above will be relative paths (relative to the current project directory, not the root project directory); otherwise, they will be absolute paths. Prior to version 3.5.0, the paths are all absolute paths.\n\nsince 3.0.0 \n\n### isReturnDefaultValues\n\n```\nvar isReturnDefaultValues: Boolean\n```\n\nWhether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null).\n\nSee [Test Your App](https://developer.android.com/studio/test/index.html) for details.\n\nsince 1.1.0"]]