How to execute a single test method in both JUnit and TestNG?

(Mark Petrovic) #1


I know how to run all the tests in a single class using Gradle.

gradle -Dtest.single=FooTest test

But if FooTest contains a particular test method of special interest and I want to run only that method, how would I do that if using JUnit? And TestNG? I know with maven you can do something like this in the case of JUnit:

mvn -Dtest='FooClass#testMethod1' test

For JUnit I tried

$ gradle test -Dtest.single='FooClass#testMethod1'

but that did not work. Nor does (I’m grasping)

$ gradle test -Dtest.single='FooClass.testMethod1'

Is there a way to do that?

Thank you.

(Peter Niederwieser) #2

From the user guide:

[…] The testNamePattern will be used to form an include pattern of “**/testNamePattern*.class”.

In other words, you can run individual test classes, but not individual test methods.

(Mark Petrovic) #3

Thanks. That’s what I feared :slight_smile:

(Eric Thelin) #4


I also had the need run single JUnit test methods from Gradle. I have created a small project on GitHub that demonstrates how to implement a custom JUnit single test method runner and how to integrate it with Gradle. There are examples for both single and multi-project Gradle builds.

Check it out: JUnit Single Method Runner

(Mark Petrovic) #5

Nice, Eric. Thanks for sharing.

(Matt Hauck) #6

Curious, is there a feature request filed for this anywhere?

(Peter Niederwieser) #7

I haven’t found one, and have raised GRADLE-2865.

(Matt Hauck) #8

Great, thanks. I’ve replied there.

(Matt Hauck) #9

FYI: it hasn’t received much attention, but I’ve submitted a pull request for this issue: I thought I’d put a note here as well in case anybody else from google comes across this page and is looking for a solution.

I’ve been using this in a local build (branched off 1.7) for several weeks now and it works great. =)

(Bartosz Baranowski) #10

FooTest#theMethodToBeTested may do the trick?

(Mike Richardson) #11

You can test a single method using gradle. See:

For example: gradle test --tests *MyServicesTest.methodTest1

(Matt Hauck) #12

@Mike, this was not available when this question was originally asked.