繁体   English   中英

如何使用gradletestkit测试现有的gradle.build?

[英]How do I test a existing gradle.build with gradletestkit?

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

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

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

我建议您遵循此Gradle指南,以了解有关测试构建逻辑的更多信息。

解决方案如下所示:

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());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM