Bug 443498 - [JUnit] support re-running 'virtual' suites, e.g. from the 'Parameterized' runner
Summary: [JUnit] support re-running 'virtual' suites, e.g. from the 'Parameterized' ru...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.5 M4   Edit
Assignee: Moritz Eysholdt CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 434093 (view as bug list)
Depends on:
Blocks: 511905
  Show dependency tree
 
Reported: 2014-09-08 09:33 EDT by Moritz Eysholdt CLA
Modified: 2018-03-16 05:41 EDT (History)
11 users (show)

See Also:
markus.kell.r: review+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Eysholdt CLA 2014-09-08 09:33:34 EDT
It would be awesome if the JDT JUnit View could re-run suites that are not explicitly backed by a Testclass, Suiteclass, or Method. 

Such 'virtual' suites are common if you use custom runners, such as 
org.junit.runners.Parameterized.

Here, the each 'virtual' suite describes as set of parameters for which each test method is executed. A bug for it has been filed here: bug 434093.

I'm filing this bug because I'd like to propose a solution that works with all custom runners and not just junit.Parameterized. 

Every JUnit runner first creates a tree of org.junit.runner.Description-Objects. Each Description represents a test or suite... currently, JDT can only re-run Descriptions that represent a Java Test- or Suite-Class or a test Method.

I'd like to propose that, for other cases, i.e. when the Description is not backed by a class/method, the parent Description should be the root for test execution and all unwanted siblings of the to-be-executed test can be filtered out with org.junit.runner.manipulation.Filter.

My personal use case is http://www.xpect-tests.org. There, 'virtual' tests represent a file or a Java method plus parameters. 

I've hacked a working solution and made it available in 
https://github.com/meysholdt/eclipse_jdt_junit_runners/tree/master/org.eclipse.jdt.junit4.runtime.patch

I'd be happy to clean it up and make a contribution.
Comment 1 Moritz Eysholdt CLA 2014-09-10 10:11:42 EDT
I've pushed contributions to
https://git.eclipse.org/r/33188 improved tests coverage for junit4 test execution
https://git.eclipse.org/r/33189 run subtrees of junit tests or a Java method when using custom runners

From the commit message: 
------------------------
For example, for the following tree of tests that you might get
from JUnit’s ‘Parameterized’ runner
—————————————-
MyTestClass
    [0]
        testMethod[0]
        testMethod2[0]
    [1]
        testMethod[1]
        testMethod2[1]
—————————————-
it is now possible to run only
—————————————-
[1]
    testMethod[1]
    testMethod2[1]
—————————————-
by clicking on the “run” action on “[1]” in the JUnit View.
Furthermore, it is possible to execute
—————————————-
MyTestClass
    [0]
       testMethod2[0]
    [1]
       testMethod2[1]
—————————————-
by choosing “run as” -> “JUnit Test” on the Java Method “testMethod2”.
To make this possible, this contribution contains the following changes:
- if a test suite (e.g. “[1]”) has no class, the containers test class
  is executed and the suite’s name is passed to the runner as the
  testName. The test runner will then filter JUnit’s tree of test
  descriptions accordingly.
- The runner now combines three strategies to filter test descriptions:
  - If the test name is specified as “foo (bar)” (this is JUnit’s
    formant), the test description that is an exact match will be
    executed.
  - If the test name is “foo”, the runner will execute suites with
    the name “foo” and methods that have the name “foo” and are in the
    root tests’s class.
  - in addition to the two cases above, and if foo is a valid Java
    identifier, all test cases will be matched if the leading part of
    their description’s name that is also a Java identifier, equals foo.
These changes are not specific to the 'Parameterized' runner and are
intend to work with all custom runners. For the method-based filitering,
however, a runner needs to prefix test descriptions the method name.
------------------------

I hope you consider this contribution to be useful. I tried to keep the quality of the contribution high, but this is still a fist shot, and it would be wonderful to have some feedback on it.
Comment 2 Moritz Eysholdt CLA 2014-09-17 06:58:33 EDT
I'm not sure if somebody found the time to look at this contribution, but it would be really great to have some feedback and discussion on how we can continue from here.
Comment 3 Noopur Gupta CLA 2014-09-17 08:38:37 EDT
Markus, please have a look.
Comment 4 Markus Keller CLA 2014-10-14 14:23:42 EDT
> https://git.eclipse.org/r/33188 improved tests coverage for junit4 test
> execution
Thanks, pushed as http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=de90ad8e2f8a6ced3368a0b475654e03bbc6f4fe

> https://git.eclipse.org/r/33189 run subtrees of junit tests or a Java method
> when using custom runners
Will review tomorrow.
Comment 5 Markus Keller CLA 2014-10-27 20:03:21 EDT
Sorry, didn't find a big enough time slot to review https://git.eclipse.org/r/33189 .
Comment 6 Dani Megert CLA 2014-11-04 10:04:38 EST
*** Bug 434093 has been marked as a duplicate of this bug. ***
Comment 7 Yami Kuronue CLA 2014-11-13 09:44:29 EST
What is the status of this patch? I would very much like to have this functionality in 4.5 if possible.
Comment 8 Moritz Eysholdt CLA 2014-11-13 10:24:53 EST
The patch is feature-complete and has test coverage. I'm waiting for the JDT team to suggest improvements or merge it.
Comment 9 Markus Keller CLA 2014-11-14 14:53:24 EST
Thanks, looks good. Sorry for the long delay.

Pushed as http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=3b64020e89d0cefe9dca260edf6f9f633a03f4e9 and adjusted a few things with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=94f85f49f97873b37d3444cf7f2c003d71348970 :

- removed @SuppressWarnings("unused") on exception parameters, since these problems will be put into a separate compiler option and disabled for Mars.
- added missing copyright headers to new files
- fixed API Tools errors
- removed exception thrown in JUnitLaunchShortcut#createLaunchConfiguration(IJavaElement, String). The exception was not documented in the API, and I don't see much harm in just considering the testName as discardable input in that special case.
Comment 10 Moritz Eysholdt CLA 2014-11-17 10:28:33 EST
Awesome, thank you! I'm fine with your adjustments.
Comment 11 Moritz Eysholdt CLA 2014-11-18 08:42:26 EST
I blogged about this here:
http://blog.moritz.eysholdt.de/2014/11/new-eclipse-junit-feature-run-subtrees.html
Comment 12 Dani Megert CLA 2014-11-18 08:50:48 EST
(In reply to Moritz Eysholdt from comment #11)
> I blogged about this here:
> http://blog.moritz.eysholdt.de/2014/11/new-eclipse-junit-feature-run-subtrees.html
> 

Nice!
Comment 13 Dani Megert CLA 2015-03-18 11:15:29 EDT
Moritz, this was contributed before the great fixes period/competition started.
Comment 14 Moritz Eysholdt CLA 2015-03-19 05:23:53 EDT
that's a pitty ;)
Comment 15 Christopher Gerking CLA 2015-09-03 11:53:41 EDT
For parametrized test cases, this feature is restricted to "constant" test names which are precisely reproducible between two executions. The default @Parameters annotation ensures this by using only the index as a name. However, problems arise when specifying custom test names as for example:

@Parameters(name = "{0}")

This will invoke toString() on the parameter at position 0, whereas the default implementation of toString() doesn't give constant values.

I wonder if it is possible to use some "constant" identifier to filter the tree, for example using only the index instead of the user-defined test name.
Comment 16 Isaac Gainey CLA 2018-03-15 15:04:24 EDT
This bug occurs when character '\n' is being used in the title.

e.g.

@Parameters( name = "{0}" )
public static List<Object[]> testCases(){ 
    return Arrays.asList( new Object[]{ "This is a working title" }, 
                          new Object[]{ "This is a buggy title\nThis is a title" } );
}
Comment 17 Dani Megert CLA 2018-03-16 05:41:52 EDT
(In reply to Isaac Gainey from comment #16)
> This bug occurs when character '\n' is being used in the title.
> 
> e.g.
> 
> @Parameters( name = "{0}" )
> public static List<Object[]> testCases(){ 
>     return Arrays.asList( new Object[]{ "This is a working title" }, 
>                           new Object[]{ "This is a buggy title\nThis is a
> title" } );
> }

This bug is closed. If you see a problem using Photon M7 (4.8 M7), then please file a new bug report.