在单元测试中以编程方式调用Gradle任务图 [英] Programmatically invoke a gradle task graph in a unit test

查看:56
本文介绍了在单元测试中以编程方式调用Gradle任务图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为gradle编写自定义插件,作为单元测试的一部分,我想调用我的任务,但前提是要执行先决条件.

不幸的是,实际的插件是一个内部项目,所以我不确定确切的来源,但是我已经准备了演示该问题的单元测试:

 package toy

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Test

class ToyTasksTest {

    boolean task1Run = false
    boolean task2Run = false

    @Test
    public void taskDependencies(){


        Project p = ProjectBuilder.builder().build()

        p.task("task1") << {
            p.logger.info("task1 running")
            task1Run = true
        }

        def task2 = p.task("task2", dependsOn: 'task1') << {
            p.logger.info("task2 running")
            task2Run = true
        }
        task2.execute() // <--- what magic do I need here instead of .execute()

        assert task2Run == true
        assert task1Run == true
    }
}
 

输出为:

Assertion failed: 

assert task2Run == true
       |        |
       false    false

如果您想快速运行测试,则该项目可在github上获得

说而不是写的另一种方式:

task2.execute()

我想运行相当于:

gradle task2

在单元测试中.

解决方案

在我看来,您想要达到的目标更多是集成测试,而不是单元测试.过去,Gradle团队成员曾向我建议,编写插件和任务时,您要做的是将任务尽可能地/合理地分离为POJO并对其进行单元测试.对于Gradle为您和管道所做的所有其他事情,例如执行任务图,测试增量任务功能等等,您可能希望进行集成测试,而集成测试的速度肯定会慢一些,这就是为什么要尽可能进行单元测试的原因.

唯一的问题是Gradle当前没有提供用于编写​​这些集成测试的工具集.有一个设计规范,但是目前您如果需要,仍然需要手工制定解决方案.

您可以看看我使用的此行是必需的.

可以找到package toy import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder import org.junit.Test class ToyTasksTest { boolean task1Run = false boolean task2Run = false @Test public void taskDependencies(){ Project p = ProjectBuilder.builder().build() p.task("task1") << { p.logger.info("task1 running") task1Run = true } def task2 = p.task("task2", dependsOn: 'task1') << { p.logger.info("task2 running") task2Run = true } task2.execute() // <--- what magic do I need here instead of .execute() assert task2Run == true assert task1Run == true } }

The output is:

Assertion failed: 

assert task2Run == true
       |        |
       false    false

The project is available on github if you would like to quickly run the test.

Another way of saying this instead of writing:

task2.execute()

I'd like run the equivalent of:

gradle task2

In the unit test.

解决方案

It seems to me that what you are trying to get here is more of an integration test than a unit test. It was suggested to me in the past by Gradle team members that when writing plugins and task what you want to do is to separate as much as you can/makes sense from your task into POJO and unit test that. For everything else that Gradle does for you and the plumbing, like executing task graph, testing incremental task features and so on you probably want to have integration tests which definitely are slower, that's why you want to unit test as much as possible.

The only problem is that Gradle currently doesn't provide a toolset for writing those integration tests. There is a design spec for that but currently you still have to hand craft a solution if you need one.

You can have a look the one that I using here but keep in mind that it has some classpath issues, that's why this line is necessary.

An example of the other solution that is using GradleConnector and that I found recently can be found here.

这篇关于在单元测试中以编程方式调用Gradle任务图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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