zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Android Gradle 插件】TestOptions 配置 ⑤ ( Test 单元测试配置类 | 设置包含或排除单元测试 | 设置堆大小 | 设置测试前后执行的逻辑 )

2023-06-13 09:18:06 时间

文章目录

Android Plugin DSL Reference 参考文档 :

一、org.gradle.api.tasks.testing.Test 单元测试配置类


UnitTestOptions ( build.gradle#android#testOptions#unitTests ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.UnitTestOptions.html

org.gradle.api.tasks.testing.Test 单元测试配置类 : https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html

1、Test 单元测试配置回顾

在上一篇博客 【Android Gradle 插件】TestOptions 配置 ③ ( TestOptions#unitTests 脚本块配置 | UnitTestOptions 配置简介 ) 中 , 参考文档 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 )
   }
 }

2、设置包含或排除单元测试

设置单元测试中 包含 或 排除 哪些测试 ,

  • include 用于设置包含哪些测试 ,
  • exclude 用于设置排除哪些测试 ;
   // explicitly include or exclude tests
   include 'org/foo/**'
   exclude 'org/boo/**'

3、设置堆大小

设置单元测试的 JVM 的堆大小参数 :

   // set heap size for the test JVM(s)
   minHeapSize = "128m"
   maxHeapSize = "512m"

4、设置测试前执行的逻辑

配置测试前执行的逻辑 :

   // listen to events in the test execution lifecycle
   beforeTest { descriptor ->
      logger.lifecycle("Running test: " + descriptor)
   }

同理 afterTest 配置的是 测试后 执行的代码逻辑 ;