【问题标题】:How to separate source files per test type in an Android library project?如何在 Android 库项目中按测试类型分隔源文件?
【发布时间】:2019-01-29 02:21:07
【问题描述】:

我按照gradle docs 在 Java 项目中按照测试类型分隔源文件,我想在 Android 库项目中做同样的事情。默认Android插件com.android.library支持testandroidTest两种测试目录。如何添加我想在 test 之后运行的 integTest

sourceSets {
     integTest {
         java.srcDir file('src/integTest/java')
         resources.srcDir file('src/integTest/resources')
     }
}

当我尝试将上述sourceSet 添加到build.gradle 时,我收到错误

错误:Android 无法识别 SourceSet 'integTest' 梯度插件。也许你拼错了什么?

由于Android Gradle Plugin不像Java Plugin那样支持自定义sourceSets,有没有其他办法解决这个问题?

【问题讨论】:

  • androidTest.setRoot('integTest')
  • 那不是只在integTest 文件夹中查找测试文件吗?来自文档hereIf all the files for a source set are located under a single root directory, you can specify that directory using the setRoot property. When gathering sources for the source set, Gradle looks only in locations relative to the root directory you specify. For example, after applying the configuration below for the androidTest source set, Gradle looks for Java sources only in the src/tests/java/ directory.

标签: android gradle gradle-plugin source-sets android-sourcesets


【解决方案1】:

该错误的主要原因是在android 内部定义integTestsourceSet,只需将其移到外部即可解决问题。请参阅下面的正确build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
    }
}

configurations {
    integTestCompile.extendsFrom testCompile
    integTestRuntime.extendsFrom testRuntime
}

task integTest(type: Test) {
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.'
    testClassesDirs = sourceSets.integTest.output.classesDirs
    classpath = sourceSets.integTest.runtimeClasspath
}

check.dependsOn integTest

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    integTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

【讨论】:

  • 是否可以创建相同的integTest 任务来从自定义命名的sourceSet 运行android(instrumentation) 测试?
  • 我不确定我是否正确理解了你的问题,你能举个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 2020-12-06
相关资源
最近更新 更多