Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically invoke a gradle task graph in a unit test

I am in the process of writing a custom plugin for gradle and as part of the unit testing I would like to invoke my task but in such away as it's prerequisite tasks are executed.

The actual plugin is unfortunately an internal project so I can't sure the exact source, but I have prepared a unit test that demonstrates the problem:

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.

like image 806
Gareth Davis Avatar asked Apr 28 '13 16:04

Gareth Davis


People also ask

How do I run a Gradle unit test?

Run Gradle testsIn your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.

How do I test Gradle tasks?

Within IntelliJ IDEA, find the check task in the Gradle tool window under verification. Double click it to run. Check is also run when you run the build task, thanks to a task dependency setup by the base plugin. Gradle runs the assemble task then check.

How do you run a test case using Gradle command?

Use the command ./gradlew test to run all tests.

Does Gradle build run tests?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.


2 Answers

Replace:

task2.execute() // <--- what magic do I need here instead of .execute()

With:

task2.actions.each { Action action ->
     action.execute(task2)
}
like image 79
TyeH Avatar answered Oct 21 '22 12:10

TyeH


Luke Daley (Gradle core dev) developed a cool functional testing system for my Gradle plugins, seen in use here.

Similar to @erdi's answer, this is a functional testing solution and does not use Gradle's ProjectBuilder. It requires that you include these utility classes for testing.

I realize that it's not the succinct answer you might have hoped for, but this system has served me very well while I develop a few Gradle plugins.

like image 34
Eric Wendelin Avatar answered Oct 21 '22 11:10

Eric Wendelin