[GRADLE-2865] How to execute a single test method in both JUnit and TestNG? Created: 12/Aug/13  Updated: 28/May/14  Resolved: 10/Jan/14

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 1.10-rc-1

Type: Task
Reporter: Gradle Forums Assignee: Unassigned
Resolution: Fixed Votes: 10


 Description   

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.



 Comments   
Comment by Gradle Forums [ 12/Aug/13 ]

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.

Comment by Gradle Forums [ 12/Aug/13 ]

Thanks. That's what I feared

Comment by Gradle Forums [ 12/Aug/13 ]

Hi!

I also had the need run single JUnit test methods from Gradle. I have created a [small project]([1]http://github.com/eric-thelin/junit-s...) on [GitHub]([2]http://github.com) 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]([3]http://github.com/eric-thelin/junit-s...)
----------------------------------------------------------------------------------------
[1] http://github.com/eric-thelin/junit-single-method-runner
[2] http://github.com
[3] http://github.com/eric-thelin/junit-single-method-runner

Comment by Gradle Forums [ 12/Aug/13 ]

Nice, Eric. Thanks for sharing.

Comment by Gradle Forums [ 12/Aug/13 ]

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

Comment by Matt Hauck [ 12/Aug/13 ]

Thanks for filing this. I would really like to see this added if possible. When running tests as I develop, it is nice to be able to run just one single test method at a time so as to not be distracted by the other tests and focus on the one I'm working on at the moment. (I currently do this with maven and it is working out quite well. It is not a dealbreaker for transitioning to gradle or not, but I think it will reduce productivity.)

I downloaded the source for gradle from github and did a grep for `test.single` and didn't see any references to it anywhere that looked significant. Where should I poke around in the code to look at supporting this?

Comment by Peter Niederwieser [ 12/Aug/13 ]

The testTask.single handling is done in org.gradle.api.plugins.JavaBasePlugin. Supporting execution of single test methods will require a somewhat bigger change though.

Comment by Matt Hauck [ 26/Aug/13 ]

Took a look at this briefly last night. Looks like it would involve a change of `JUnitTestClassExecuter` to use `Request.method`, a change of `TestNGTestClassProcessor` to use `testNg.setTestNames()`, as well as some logic further up the stack that parses out the 'single' property for test names (after the "#" sign, separated by "+" signs like surefire?) – and passes those down the stack to the test class processor to handle them if present.

Supporting test name wildcards would be tricky since the test frameworks themselves don't appear to support that – as it would require doing some reflection to find matching test methods to the patterns – but that doesn't seem necessary for a first version of supporting single test methods.

Comment by Matt Hauck [ 07/Sep/13 ]

FYI. I have something working right now with testng and junit. It's pretty sweet. The format goes like this:

`gradle test -Dtest.single=TestClass#method1,method2`

TestNG needed a bit more extensive a change than I had thought, and it has broken most of the testng test processor test cases. After I get those fixed I will submit a pull request.

Comment by Matt Hauck [ 08/Sep/13 ]

Submitted. https://github.com/gradle/gradle/pull/193

Comment by Matt Hauck [ 19/Feb/14 ]

I suggest this ticket should be re-opened as the implemented feature does not work.

I just downloaded gradle 1.11 and it still does not work. It is mentioned in the above linked pull request (#193) that others have reported similar issues, however, I don't see anything in the "known issues" for 1.10 or 1.11 about this. I'm a bit disappointed that another release has gone by without fixing this released feature...

Comment by Chris Wensel [ 27/May/14 ]

I'm assuming there has been no progress on this?

update: found the comments here
https://github.com/gradle/gradle/pull/193

Comment by Szczepan Faber [ 28/May/14 ]

So far, we haven't been able to reproduce the misbehavior reported late in this thread. If you can provide a reproducible sample we will surely fix any problem. I use --tests flag very often in many projects and it works for me

Comment by Chris Wensel [ 28/May/14 ]

Sorry, I did finally sort it out and get it working.

we have our on Runner implementation, and it took us to push the filter method down to the children.

Generated at Wed Jun 30 12:33:34 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.