Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gradle Goodness: Running All Tests From One Package

DZone's Guide to

Gradle Goodness: Running All Tests From One Package

Hubert Klein Ikkink shows how to run all tests in Gradle from one package, complete with a set of instructions for different scenarios.

· DevOps Zone
Free Resource

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

If we have a Gradle task of type Test we can use a filter on the command line when we invoke the task. We define a filter using the --tests option. If, for example, we want to run all tests from a single package, we must define the package name as value for the --tests option. It is good to define the filter between quotes, so it is interpreted as is, without any shell interference.

If we configure the test task to output the test class name, we can see that which tests are executed. In the following snippet, we reconfigure the test task:

...
test {
    beforeTest { descriptor ->
       logger.lifecycle("Running test: $descriptor.className")
    }
}
...

Suppose we have a project and we execute all tests:

$ gradle test
...
:test
Running test: mrhaki.gradle.model.CourseSpec
Running test: mrhaki.gradle.model.TeacherSpec
Running test: mrhaki.gradle.service.CourseServiceSpec
...
$

To only run the tests in the mrhaki.gradle.model package, we use the following command:

$ gradle test --tests "mrhaki.gradle.model.*"
...
:test
Running test: mrhaki.gradle.model.CourseSpec
Running test: mrhaki.gradle.model.TeacherSpec
...
$

We can even filter on a single test to be executed:

$ gradle test --tests "mrhaki.gradle.model.CourseSpec"
...
:test
Running test: mrhaki.gradle.model.CourseSpec
...
$

If we only want to run a single method of a test, we can specify the method name as a filter:

$ gradle test --tests "test --tests "mrhaki.gradle.model.CourseSpec.create new Course as immutable""
...
:test
Running test: mrhaki.gradle.model.CourseSpec
...
$


Written with Gradle 2.13.

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:
gradle ,gradle:goodness ,code ,continuous delivery ,devops

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}