我正在使用这个项目来启动gradle插件开发:https://github.com/int128/gradle-plugin-starter
在这个项目中,有两种测试:
我的插件旨在配置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/