Android Plugin DSL Reference 参考文档 :
TestOptions ( build.gradle#android#testOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.html
UnitTestOptions ( build.gradle#android#testOptions#unitTests ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.UnitTestOptions.html
TestOptions#unitTests 脚本块配置 用于配置 设备化测试过程中是否关闭动画 ;
TestOptions#unitTests 脚本块配置原型 : 该脚本块用于配置 TestOptions#unitTests 属性 ;
unitTests { }
Configures unit test options.
配置单元测试选项.
Delegates to:
UnitTestOptions from unitTests
TestOptions#unitTests 属性配置原型 : 该配置是 UnitTestOptions 类型的 ;
UnitTestOptions unitTests
Configures unit test options.
TestOptions#unitTests 脚本块配置使用示例 :
android {
testOptions {
animationsDisabled true
unitTests.all {
if (it.name == 'testDebug') {
systemProperty 'debug', 'true'
}
}
}
}
UnitTestOptions ( build.gradle#android#testOptions#unitTests ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.UnitTestOptions.html
UnitTestOptions 配置类中 , 提供了 returnDefaultValues 属性配置 和 all(configClosure) 方法配置 ;
Properties 属性配置
returnDefaultValues
Whether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null).
是否从android中卸载了方法。jar应该抛出异常或返回默认值(即零或null)。
Methods 方法配置
all(configClosure)
Configures all unit testing tasks.
配置所有单元测试任务。
UnitTestOptions#returnDefaultValues 属性 : 在 本地化测试 的代码中 , 无法调用 android.jar 中的方法 , 只测试 Java 代码逻辑 , 不涉及 Android 的 API ;
该属性一般不进行配置 ;
UnitTestOptions#returnDefaultValues 属性原型 :
boolean returnDefaultValues
Whether unmocked methods from android.jar should throw exceptions or return default values (i.e. zero or null).
See Test Your App for details.
方法原型 :
void all(Closure<Test> configClosure)
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.
UnitTestOptions#all 方法 : 该方法的参数是代码块 , 需要传入闭包作为参数 ;
android {
testOptions {
unitTests.all {
if (it.name == 'testDebug') {
systemProperty 'debug', 'true'
}
}
}
}
该闭包是 Test 类代理的 , 该类是 Gradle 中的 org.gradle.api.tasks.testing.Test 类 , 参考文档 https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html
Gradle 中 Test 单元测试配置类参考 :
plugins {
id 'java' // adds 'test' task
}
test {
// Discover and execute JUnit4-based tests
useJUnit()
// Discover and execute TestNG-based tests
useTestNG()
// Discover and execute JUnit Platform-based tests
useJUnitPlatform()
// set a system property for the test JVM(s)
systemProperty 'some.prop', 'value'
// explicitly include or exclude tests
include 'org/foo/**'
exclude 'org/boo/**'
// show standard out and standard error of the test JVM(s) on the console
testLogging.showStandardStreams = true
// set heap size for the test JVM(s)
minHeapSize = "128m"
maxHeapSize = "512m"
// set JVM arguments for the test JVM(s)
jvmArgs '-XX:MaxPermSize=256m'
// listen to events in the test execution lifecycle
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
// Fail the 'test' task on the first test failure
failFast = true
// listen to standard out and standard error of the test JVM(s)
onOutput { descriptor, event ->
logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
}
}