我想将build.gradle
文件加载到 gradle testkit 并使用 Junit 5 以便我可以测试 build.gradle 中的 buildscript
手册中的示例仅显示您如何在代码中以及使用 Junit4 编写和测试
我的测试类看起来像这样
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.nio.file.Path
import java.nio.file.Paths
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import static org.gradle.testkit.runner.TaskOutcome.*;
public class BuildLogicFunctionalTest {
private Project project;
private ProjectBuilder projectbuild;
@TempDir
private File projectfile;
@Test
public void testHelloWorldTask() throws IOException {
projectbuild = ProjectBuilder.builder();
project = projectbuild.build();
BuildResult result = GradleRunner.create()
.withProjectDir(project.getBuildDir())
.withArguments("helloWorld")
.build();
assertTrue(result.getOutput().contains("Hello world!"));
assertEquals(SUCCESS, result.task(":helloWorld").getOutcome());
}
}
Testkit 构建脚本如下所示
plugins {
id 'groovy'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation gradleTestKit()
implemenation localGroovy()
implementation gradleApi()
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}
wrapper {
gradleVersion = '5.0'
}
我要测试的 build.gradle 文件如下所示
task helloWorld {
doLast {
println 'Hello world!'
}
}
如何将 build.gradle 加载到程序中?
GradleRunner 代码应该如何测试任务 helloWorl?