测试Gradle脚本指南 [英] Guide for Testing Gradle Scripts

查看:125
本文介绍了测试Gradle脚本指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试 Gradle 脚本的最佳做法是什么?

What are the best practices for testing Gradle Scripts?

我目前使用 antunit单元测试我的 ant 脚本,但我期待迁移到Gradle。我只能从Gradle或Groovy中找到有关测试Java代码的文章,但没有详细测试我创建或测试Groovy的Gradle任务。 Gradle有没有相当于antunit的?有没有人玩过BDD框架(如 cucumber )来测试Gradle脚本?

I currently unit test my ant scripts with antunit, but I'm looking to migrate to Gradle. I can only find articles on testing Java code from Gradle or Groovy, but nothing on testing the Gradle tasks that I create or testing Groovy in general. Is there an equivalent of antunit for Gradle? Has anyone played with a BDD framework (like cucumber) to test Gradle scripts?

例如,我目前有以下Ant目标

For instance, I currently have the following Ant Target

<target name="dist-bin" depends="build" description="creates a zip distribution of the current build">
    <zip destfile="build/TIBant-bin.zip">
        <zipfileset dir="src/ant" includes="**" />
        <zipfileset dir="test" includes="**" prefix="test" />
        <zipfileset dir="build" includes="TIBant.jar" />
        <zipfileset dir="build" includes="TIBant-*.html" />
        <zipfileset dir="build/examples/XMLtoProperties"
                    includes="XMLtoProperties.html"
                    prefix="examples/XMLtoProperties" />
        <zipfileset dir="lib" includes="**" prefix="lib" />
        <zipfileset dir="src/xslt" includes="**" excludes="test/**,userguide.xslt" prefix="lib/xslt" />
        <zipfileset dir="." includes="copyright.html,LICENSE.txt" />
        <zipfileset dir="examples"
                    includes="**"
                    excludes="**/build/**,**/config/default.properties"
                    prefix="examples" />
    </zip>
</target>

可以想象,在重构项目时很容易破解这个,所以我有以下

As you can imagine, it's pretty easy to break this when refactoring the project, so I have the following antunit test to check it.

<target name="test-dist-bin">
    <delete file="build/TIBant-bin.zip" />
    <au:assertFalse message="Bin dist still present">
        <available file="build/TIBant-bin.zip" />
    </au:assertFalse>
    <antcall target="dist-bin" />
    <au:assertTrue message="Bin dist not created">
        <available file="build/TIBant-bin.zip" />
    </au:assertTrue>
    <delete dir="build/testBinDist" />
    <au:assertFalse message="TestBinDist still present">
        <available file="build/testBinDist" />
    </au:assertFalse>
    <mkdir dir="build/testBinDist" />
    <unzip src="build/TIBant-bin.zip" dest="build/testBinDist" />
    <au:assertFalse message="config dir present">
        <available file="build/testBinDist/config/default.properties" />
    </au:assertFalse>
    <au:assertTrue message="Ant Macros missing">
        <available file="build/testBinDist/tibant.xml" />
    </au:assertTrue>
    <au:assertTrue message="Engine Stopper Jar missing">
        <available file="build/testBinDist/TIBant.jar" />
    </au:assertTrue>
    <au:assertTrue message="Ant-contrib-missing">
        <available file="build/testBinDist/lib/ant-contrib-1.0b3.jar" />
    </au:assertTrue>
    <au:assertTrue message="ant-unit missing">
        <available file="build/testBinDist/lib/ant-antunit-1.2.jar" />
    </au:assertTrue>
    <au:assertTrue message="Copyright missing">
        <available file="build/testBinDist/copyright.html" />
    </au:assertTrue>
    <au:assertTrue message="License missing">
        <available file="build/testBinDist/LICENSE.txt" />
    </au:assertTrue>
    <au:assertFalse message="Src present">
        <available file="build/testBinDist/src/java/org/windyroad/tibant/EngineStopper.jar" />
    </au:assertFalse>
    <au:assertTrue message="example missing">
        <available file="build/testBinDist/examples/SimpleProject/src/bw/example/Build/example.archive" />
    </au:assertTrue>
    <au:assertFalse message="example has build files">
        <available file="build/testBinDist/examples/SimpleProject/build/*" />
    </au:assertFalse>
    <au:assertFalse message="example has default config file">
        <available file="build/testBinDist/examples/SimpleProject/config/default.properties" />
    </au:assertFalse>
    <property name="doc.file"
              location="build/testBinDist/TIBant-User-Guide.html" />
    <au:assertTrue message="doc missing: ${doc.file}">
        <available file="${doc.file}" />
    </au:assertTrue>
    <au:assertTrue message="xslt missing">
        <available file="build/testBinDist/lib/xslt/configure-ear.xslt" />
    </au:assertTrue>
    <subant target="run-quick-tests">
        <fileset dir="build/testBinDist" includes="build.xml" />
    </subant>
</target>

这是由以下片段调用以产生一个很好的xml报告

which is called by the following snippet to produce a nice xml report

                <au:antunit failonerror="@{failonerror}">
                    <propertyset>
                        <propertyref name="config.filename" />
                    </propertyset>
                    <path>
                        <pathelement location="@{antunit}" />
                    </path>
                    <au:plainlistener logLevel="info" />
                    <au:xmllistener todir="build" loglevel="info" />
                </au:antunit>

我了解如何迁移 dist-bin gradle,但我不确定什么是迁移 test-dist-bin au:antunit call。

I understand how to migrate dist-bin to gradle, but I'm not sure what's the right way to migrate test-dist-bin and the au:antunit call.

推荐答案

Gradle 3.x测试工具包可用!
请检查用户指南: https://docs.gradle.org /current/userguide/test_kit.html

Gradle 3.x test Toolkit available! Please, check out userguide here: https://docs.gradle.org/current/userguide/test_kit.html

然后可以通过声明以下内容来验证逻辑的正确性:

The correctness of the logic can then be verified by asserting the following, potentially in combination:


  • 构建的输出;
  • 构建的日志记录(即控制台输出);
  • 由构建执行的任务集及其结果(例如失败,最新日期等)。
  • The build's output;
  • The build's logging (i.e. console output);
  • The set of tasks executed by the build and their results (e.g. FAILED, UP-TO-DATE etc.).

副本 - 例子:

copy-pasted example:

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

public class BuildLogicFunctionalTest {
    @Rule public final TemporaryFolder testProjectDir = new TemporaryFolder();
    private File buildFile;

    @Before
    public void setup() throws IOException {
        buildFile = testProjectDir.newFile("build.gradle");
    }

    @Test
    public void testHelloWorldTask() throws IOException {
        String buildFileContent = "task helloWorld {" +
                                  "    doLast {" +
                                  "        println 'Hello world!'" +
                                  "    }" +
                                  "}";
        writeFile(buildFile, buildFileContent);

        BuildResult result = GradleRunner.create()
            .withProjectDir(testProjectDir.getRoot())
            .withArguments("helloWorld")
            .build();

        assertTrue(result.getOutput().contains("Hello world!"));
        assertEquals(result.task(":helloWorld").getOutcome(), SUCCESS);
    }

    private void writeFile(File destination, String content) throws IOException {
        BufferedWriter output = null;
        try {
            output = new BufferedWriter(new FileWriter(destination));
            output.write(content);
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
}

这篇关于测试Gradle脚本指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆