如何在Android项目中仅执行uiTests Gradle任务?
这问题我之前帮团队解决过,直接在Gradle里定义自定义任务就完美解决了——既能提交到版本控制,Jenkins上也能直接调用,完全满足你的需求。下面给你两种靠谱的实现方案:
解决方案:通过Gradle自定义任务分离测试
方法1:利用已有的测试套件类(推荐)
既然你已经有两个指定测试套件的Kotlin类,直接在app/build.gradle.kts(如果用Groovy就写app/build.gradle)里添加以下配置,创建两个独立的测试任务:
Kotlin DSL 版本
// 仅运行UI测试的自定义任务 tasks.register<com.android.build.gradle.tasks.ConnectedAndroidTest>("connectedUiTest") { description = "Runs only UI tests from the uiTests package" // 替换成你的UI测试套件类的全路径 testInstrumentationRunnerArguments["suiteClass"] = "com.yourpackage.testing.UiTestSuite" // 继承默认connectedAndroidTest的所有配置(比如设备、覆盖率等) copyFrom(tasks.named("connectedAndroidTest").get()) } // 仅运行集成测试的自定义任务 tasks.register<com.android.build.gradle.tasks.ConnectedAndroidTest>("connectedIntegrationTest") { description = "Runs only integration tests from the integrationTests package" // 替换成你的集成测试套件类的全路径 testInstrumentationRunnerArguments["suiteClass"] = "com.yourpackage.testing.IntegrationTestSuite" copyFrom(tasks.named("connectedAndroidTest").get()) }
Groovy 版本
// 仅运行UI测试的自定义任务 task connectedUiTest(type: com.android.build.gradle.tasks.ConnectedAndroidTest) { description = 'Runs only UI tests from the uiTests package' testInstrumentationRunnerArguments += ['suiteClass': 'com.yourpackage.testing.UiTestSuite'] // 继承默认任务配置 copyFrom connectedAndroidTest } // 仅运行集成测试的自定义任务 task connectedIntegrationTest(type: com.android.build.gradle.tasks.ConnectedAndroidTest) { description = 'Runs only integration tests from the integrationTests package' testInstrumentationRunnerArguments += ['suiteClass': 'com.yourpackage.testing.IntegrationTestSuite'] copyFrom connectedAndroidTest }
配置完成后,你就可以分别执行:
./gradlew connectedUiTest:只跑UI测试./gradlew connectedIntegrationTest:只跑集成测试
这些任务定义会被提交到版本控制,Jenkins构建时直接调用对应的命令即可。
方法2:按包名过滤测试(无需测试套件类)
如果不想依赖测试套件类,也可以直接通过包名来过滤测试任务,配置更简单:
Kotlin DSL 版本
tasks.register<com.android.build.gradle.tasks.ConnectedAndroidTest>("connectedUiTest") { description = "Runs only UI tests in the uiTests package" testInstrumentationRunnerArguments["package"] = "com.yourpackage.androidTest.uiTests" copyFrom(tasks.named("connectedAndroidTest").get()) } tasks.register<com.android.build.gradle.tasks.ConnectedAndroidTest>("connectedIntegrationTest") { description = "Runs only integration tests in the integrationTests package" testInstrumentationRunnerArguments["package"] = "com.yourpackage.androidTest.integrationTests" copyFrom(tasks.named("connectedAndroidTest").get()) }
Groovy 版本
task connectedUiTest(type: com.android.build.gradle.tasks.ConnectedAndroidTest) { description = 'Runs only UI tests in the uiTests package' testInstrumentationRunnerArguments += ['package': 'com.yourpackage.androidTest.uiTests'] copyFrom connectedAndroidTest } task connectedIntegrationTest(type: com.android.build.gradle.tasks.ConnectedAndroidTest) { description = 'Runs only integration tests in the integrationTests package' testInstrumentationRunnerArguments += ['package': 'com.yourpackage.androidTest.integrationTests'] copyFrom connectedAndroidTest }
额外提示
- 记得把代码里的
com.yourpackage替换成你项目实际的包名 - 如果需要测试覆盖率报告,自定义任务会自动继承默认任务的配置,不用额外设置
- Jenkins上直接在构建步骤里调用对应的
./gradlew命令就行,和本地执行完全一致
内容的提问来源于stack exchange,提问作者jbehrens94
相关产品推荐
相关产品推荐

