如何测试依赖于android插件的本地gradle插件?

时间:2017-11-07 16:09:18

标签: android unit-testing gradle plugins spock

我正在使用此项目来启动gradle插件开发:https://github.com/int128/gradle-plugin-starter

在这个项目中,有两种测试:

  • 使用spock进行单元测试
  • 使用gradle测试工具包进行验收测试

我的插件旨在配置android插件。它供内部使用。我们有很多项目以相同的方式配置,所以我想使用相同的代码库来做到这一点。

假设我的插件代码如下:

class CopperPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
    if(!project.plugins.hasPlugin("com.android.application") && !project.plugins.hasPlugin("com.android.library")){
        throw new GradleScriptException("Android plugin needs to be applied first", new ClassNotFoundException())
    }
}

}

我不知道如何让验收测试工作!!! 错误是:

* What went wrong:
A problem occurred evaluating root project 'fixture-42'.
Plugin with id 'fr.coppernic.android' not found.

验收测试代码是

@Unroll
def 'acceptance test should pass on Gradle #gradleVersion'() {
    given:
    def runner = GradleRunner.create()
        .withProjectDir(new File("fixture-${gradleVersion.replace(".","")}"))
        .withArguments('test')
        .withPluginClasspath()
        .withGradleVersion(gradleVersion)

    when:
    runner.build()

    then:
    noExceptionThrown()

    where:
    gradleVersion << ['4.2', '3.5']
}

验收测试 build.gradle

plugins {
    id 'groovy'
    id 'java-gradle-plugin'
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

repositories {
    jcenter()
}

dependencies {
    testCompile('org.spockframework:spock-core:1.1-groovy-2.4') {
        exclude module: 'groovy-all'
    }
}

gradlePlugin {
    pluginSourceSet parent.sourceSets.main
}
来自fixture-35文件夹

build.gradle

buildscript {
repositories {
    jcenter()
    maven { url 'http://arti-01:8081/artifactory/plugins-release' }
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.2'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

apply plugin:'com.android.application'
apply plugin:'fr.coppernic.android'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
}

task test << {
    assert project.plugins.hasPlugin('fr.coppernic.android')
}
来自fixture-42文件夹

build.gradle

buildscript {
repositories {
    google()
    jcenter()
    maven { url 'http://arti-01:8081/artifactory/plugins-release' }
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

apply plugin: 'com.android.application'
apply plugin: 'fr.coppernic.android'

task test << {
    assert project.plugins.hasPlugin('fr.coppernic.android')
}

1 个答案:

答案 0 :(得分:0)

GradleTestKit仅在plugins DSL定义中查找。您正在通过apply plugin:语法应用插件,但无法使用。