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

如何测试依赖于android插件的本地gradle插件?,android,unit-testing,gradle,plugins,spock,Android,Unit Testing,Gradle,Plugins,Spock,我正在使用此项目开始gradle插件开发: 在本项目中,有两种测试: 使用spock的单元测试 使用gradle测试套件进行验收测试 我的插件旨在配置android插件。这是供内部使用的。我们有很多项目是以相同的方式配置的,所以我想使用相同的代码库来实现这一点 假设我的插件代码如下: class CopperPlugin implements Plugin<Project> { @Override void apply(Project project) { if(!pro

我正在使用此项目开始gradle插件开发:

在本项目中,有两种测试:

  • 使用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())
    }
}
验收测试代码为

@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来自
fixture-35
文件夹

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')
}
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')
}

GradleTestKit
只查看
插件
DSL定义。您正在通过
apply plugin:
语法应用插件,但该语法不起作用

GradleTestKit
只查看
插件
DSL定义。您正在通过
apply plugin:
语法应用插件,但该语法不起作用

您是否在
src/main/resources/META-INF/gradle plugins
中创建了一个
fr.coppernic.android.properties
,并使用了正确的
实现类
条目?`@LeonardBrünings是的!您是否在
src/main/resources/META-INF/gradle plugins
中创建了一个
fr.coppernic.android.properties
,并使用了正确的
实现类
条目?`@LeonardBrünings是的!
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')
}
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')
}