【Android Gradle 插件】TestOptions 配置 ③ ( TestOptions#unitTests 脚本块配置 | UnitTestOptions 配置简介 )

Android Plugin DSL Reference 参考文档 :





一、TestOptions#unitTests 脚本块配置



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


1、配置简介


TestOptions#unitTests 脚本块配置 用于配置 设备化测试过程中是否关闭动画 ;

2、配置原型


TestOptions#unitTests 脚本块配置原型 : 该脚本块用于配置 TestOptions#unitTests 属性 ;

unitTests {
    
     }
Configures unit test options.
配置单元测试选项.

Delegates to:
UnitTestOptions from unitTests

TestOptions#unitTests 属性配置原型 : 该配置是 UnitTestOptions 类型的 ;

UnitTestOptions unitTests
Configures unit test options.

3、使用示例


TestOptions#unitTests 脚本块配置使用示例 :

android {
    
    
	testOptions {
    
    
		animationsDisabled true
        unitTests.all {
    
    
            if (it.name == 'testDebug') {
    
    
                systemProperty 'debug', 'true'
            }
        }
	}
}




二、UnitTestOptions 配置简介



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.
配置所有单元测试任务。

1、UnitTestOptions#returnDefaultValues 属性


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.

2、UnitTestOptions#all 方法


方法原型 :

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 )
   }
 }

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/124919287
今日推荐