android - 如何测试依赖于Android插件的本地Gradle插件?

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

    最佳答案

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

    关于android - 如何测试依赖于Android插件的本地Gradle插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47162480/

    相关文章:

    android - 堆不会在 Genymotion 模拟器上扩展

    java - 带背景图片的抽屉导航

    java - 如何为自定义 Spring Batch Reader 编写 junit

    unit-testing - 编写难以进行单元测试的代码

    android - 如何运行具有不同首选项的Android Studio的两个实例,例如不同的SDK位置,不同的Gradle版本

    java - Maven-publish 不能在 pom 文件上应用 withXML(没有方法签名)

    安卓开发环境 : Eclipse or Motodev or Tegra Android Development Pack?

    android - 如何在 Android 上非常大的 WebView 下设置按钮?

    python - 如何使用 Travis-CI 运行 Tox

    java - 如何使用 ProductFlavors 来定义 App 中的开发行为?