1

我想将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?

4

2 回答 2

3

TestKit 要求您将 Gradle 配置文件放入给定的项目目录中。您将在此示例测试中看到为测试创建了一个新的build.gradlesettings.gradle

此外,该工具主要用于测试自定义任务和 Gradle 插件。我感觉到您正在尝试使用它来测试您的根build.gradle脚本。这是有风险的,因为测试引擎可能会修改传递给的目录的内容withProjectDir(...)并修改或删除您的源。

我建议你按照这个 Gradle 指南来了解更多关于测试你的构建逻辑的信息。

于 2019-06-30T15:41:16.893 回答
0

解决方案如下所示:

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 org.gradle.api.tasks.TaskContainer;


import static org.gradle.testkit.runner.TaskOutcome.*;

public class BuildLogicFunctionalTest {




    @Test
    public void testHelloWorldTask() throws IOException {
        File projectdir = new File("/Users/stein/Development/ReactSpringBooTutorial/todoapp")



        BuildResult result = GradleRunner.create()
        .withProjectDir(projectdir. getAbsoluteFile())
        .withArguments("helloWorld")
        .build();



        assertTrue(result.getOutput().contains("Hello world!"));
        assertEquals(SUCCESS, result.task(":helloWorld").getOutcome());
    }
于 2019-06-30T14:36:19.433 回答