Gradle 4.0-milestone2上的集成测试

时间:2017-05-30 07:42:17

标签: gradle android-gradle build.gradle gradlew

我正在将Java库迁移到Gradle 4.0-milestone2,我遇到了下面定义的集成任务的一些问题。

此任务允许我有两个不同的源目录src/testsrc/integration,并具有单独的gradle任务来运行它们./gradlew test./gradlew integration

integration.gradle

的内容
sourceSets {
  integration {
    java {
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
      srcDir file('src/integration/java')
    }
    resources.srcDir file('src/integration/resources')
  }
}
configurations {
  integrationCompile.extendsFrom testCompile
  integrationRuntime.extendsFrom testRuntime
}
idea {
  module {
    testSourceDirs += file('src/integration/java')
    scopes.TEST.plus += [configurations.integrationCompile]
  }
}
task integration(type: Test) {
  testClassesDir = sourceSets.integration.output.classesDir
  classpath = sourceSets.integration.runtimeClasspath
}

integration.mustRunAfter test

build.gradle

的内容
apply plugin: 'java-library'
apply plugin: 'idea'

apply from: './integration.gradle'

targetCompatibility = '1.7'
sourceCompatibility = '1.7'

dependencies {

  api 'com.github.ihsanbal:LoggingInterceptor:2.0.0'
  api 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
  api 'com.squareup.retrofit2:retrofit:2.0.2'
  api 'com.squareup.retrofit2:converter-gson:2.0.2'
  api 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

  testImplementation 'junit:junit:4.12'
}

test {
  testLogging.showStandardStreams = true
}


task copyTestResources(type: Copy) {
  from sourceSets.test.resources
  into sourceSets.test.output.classesDir
}
processTestResources.dependsOn copyTestResources

此配置存在两个问题。

1)copyTestResources任务不起作用,因为classesDir已弃用且classesDirs不兼容。

2)类路径不包括库依赖项。 编译未能完成以下错误

error: cannot find symbol
error: package SOMETHING does not exist

有关如何解决此问题的任何线索?谢谢

1 个答案:

答案 0 :(得分:3)

经过一些tips on the Gradle forum之后,我有了它的工作。

build.gradle

apply plugin: 'java-library'
apply plugin: 'idea'

apply from: './integration.gradle'

targetCompatibility = '1.7'
sourceCompatibility = '1.7'

dependencies {

  api 'com.github.ihsanbal:LoggingInterceptor:2.0.0'
  api 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
  api 'com.squareup.retrofit2:retrofit:2.0.2'
  api 'com.squareup.retrofit2:converter-gson:2.0.2'
  api 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

  testImplementation 'junit:junit:4.12'
}
test {
  testLogging.showStandardStreams = true
}

integration.gradle

sourceSets {
  integration {
    java {
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
      srcDir file('src/integration/java')

      integration.runtimeClasspath += integration.output
    }
    resources.srcDir file('src/integration/resources')
  }
}
configurations {
  integrationImplementation.extendsFrom testImplementation
  integrationRuntimeOnly.extendsFrom testRuntimeOnly
}
idea {
  module {
    testSourceDirs += file('src/integration/java')
    scopes.TEST.plus += [configurations.integrationRuntimeClasspath, configurations.integrationCompileClasspath]
  }
}
task integration(type: Test) {
  testClassesDirs += sourceSets.integration.output.classesDirs
  classpath = sourceSets.integration.runtimeClasspath
}
integration.mustRunAfter test

仍然需要弄清楚如何从IDE内部运行它们,因为它目前无法运行,但至少我可以再次使用CI。