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

Hello.

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.

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.

Thanks. That’s what I feared :slight_smile:

Hi!

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

Nice, Eric. Thanks for sharing.

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

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

Great, thanks. I’ve replied there.

FYI: it hasn’t received much attention, but I’ve submitted a pull request for this issue: https://github.com/gradle/gradle/pull/193. 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. =)

FooTest#theMethodToBeTested may do the trick?

You can test a single method using gradle. See: http://www.gradle.org/docs/current/userguide/java_plugin.html

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

1 Like

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