Android Test Instrumentation Runner在执行阶段设置的参数

时间:2018-05-18 21:05:56

标签: java android gradle

我正在尝试创建gradle任务,以便仅根据注释运行一组子测试,详细内容为in the gradle dsl。我试图在gradle执行阶段设置testInstrumentationRunnerArguments。虽然我成功设置了参数,但它似乎并没有真正进行任何过滤。

我们确实将测试放在不同的源集路径中,但我们已将它们添加到默认的androidTest源集。所有运行的测试都没有被过滤。

我的问题随后变成:测试参数可以在gradle的执行阶段动态设置还是需要在配置阶段设置?如果在执行期间可以设置它们,任何人都可以指出为什么下面的gradle不能工作?

这是我的test.gradle,它在android库配置关闭后应用。

/*
 * Copyright (c) 2018 company. All rights reserved.
 */

ext {
    integrationTest = false
    networkIntegrationTest = false
}

android.sourceSets {
    androidTest {
        java {
            // add our custom test source sets so android studio gives us good tooling
            srcDirs += './src/integrationNetworkTest/java'
            srcDirs += './src/integrationTest/java'
        }
        res {
            srcDirs += './src/integrationNetworkTest/res'
            srcDirs += './src/integrationTest/res'
        }
    }
}

tasks.whenTaskAdded { task ->
    def name = task.name.toLowerCase()
    if(name.contains('connectedcheck') || task.name.matches("connected([a-zA-Z]?)+AndroidTest")) {
        task.doFirst {
            if(!integrationTest && !networkIntegrationTest) {
                android.productFlavors.each {
                    it.testInstrumentationRunnerArguments.put('notAnnotation',
                    'com.company.android.infrastructure.test.IntegrationTest,com.company.android.infrastructure.test.IntegrationNetworkTest')
                }
            }
            else {
                StringBuilder sb = new StringBuilder()
                if(integrationTest && networkIntegrationTest) {
                    sb.append('com.company.android.infrastructure.test.IntegrationTest')
                    sb.append(',')
                    sb.append('com.company.android.infrastructure.test.IntegrationNetworkTest')
                }
                else if (integrationTest) {
                    sb.append('com.company.android.infrastructure.test.IntegrationTest')
                }
                else if (networkIntegrationTest) {
                    sb.append('com.company.android.infrastructure.test.IntegrationNetworkTest')
                }
                if(integrationTest || networkIntegrationTest) {
                    android.productFlavors.each {
                        it.testInstrumentationRunnerArguments.put('annotation', sb.toString())
                    }
                }
            }

            android.productFlavors.each {
                println "${it.name} Test Annotations: ${it.testInstrumentationRunnerArguments}"
            }
        }
    }
}
project.afterEvaluate {
    android.libraryVariants.each { variant ->
        tasks.create("${variant.name}IntegrationTest") {
            group 'company'
            description 'Run company Integration tests with only network requests mocked'
            finalizedBy "connected${variant.name.capitalize()}AndroidTest"
            doLast {
                integrationTest = true
            }
        }

        tasks.create("${variant.name}IntegrationNetworkTest") {
            group 'company'
            description 'Run company Integration tests with real network requests'
            finalizedBy "connected${variant.name.capitalize()}AndroidTest"
            doLast {
                networkIntegrationTest = true
            }
        }
    }
}

Gradle:4.1

Android插件:3.0.1

*编辑:*我认为这是一个错误:Android Bug Tracker

0 个答案:

没有答案