How do I fully test a custom task?


(emmanue.pot) #1

Hello,

In Gradle documentation, the following code is proposed:

class GreetingTaskTest {
    @Test
    public void canAddTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        def task = project.task('greeting', type: GreetingTask)
        assertTrue(task instanceof GreetingTask)
    }
}

This is nice, but it does not test much. I have a custom plugin with custom tasks that I would like to fully test with Gradle testing framework, but I do not find any way to execute completely a task - with its dependencies. Is there any way to do it ? If it is not, how can I completely test my plugin ? :S

Thanks !

Emmanuel


(Luke Daley) #2

Unfortunately the story right now is not a perfect one. There’s now easy out of the box way to functionally test your task. We are working on this (see spec: https://github.com/gradle/gradle/blob/master/design-docs/testing-user-build-logic.md).

Your best option is to keep your task implementation as skinny as possible (this is a good thing to do for other reasons as well). By this I mean, use your task as an adapter to the Gradle execution mechanism and keep your real logic in POJOs. This may seem like overkill, but it’s the simplest way to exercise your code right now.

If you’re interested in living on the edge, you can take a look at some spike code I did on the side to explore possibilities in this space.

https://github.com/alkemist/gradle-test-kit

That’s not part of the Gradle distribution and not supported as such.

That’s the situation as of today.


(emmanue.pot) #3

Thanks for the detailed and quick answer. I will do as you suggest, I was just kind of hoping something more adequate existed :-). And I will anxiously wait for the spec implementation :-).