views:

1161

answers:

4

The JUnit view in Eclipse seems to order the tests randomly. How can I order them by class name?

A: 

it orders them base on execution time, maybe you should sort your methods? source/sort members

01
+1  A: 

mark wrote:

it orders them base on execution time, maybe you should sort your methods? source/sort members

mark is right. But you cannot sort your unit test. It's not allowed to speculate about the order of execution.

Unit tests have to be built independently and it's random, how they are called by the UnitRunner.

In most cases, the test methods are sorted alphabetically. The classes are random. Try to use a TestSuite to order your tests.

furtelwart
Well, I'm trying to avoid using TestSuite. I appreciate unit test independence, and I maintain that when creating them, but it would be nice if Unit Runner could be told to go ahead and order them by class name. Hmm, maybe I should look into the source code...
Gary Kephart
+1  A: 

If you really need hard dependency between your JUnit test, try JExample extension

JExample introduces producer-consumer relationships to unit-testing.
A producer is a test method that yields its unit under test as return value.
A consumer is a test method that depends on one or more producers and their return values.

You can install it in Eclipse, for Junit4.4 or 4.5.

import jexample.Depends;

  @Test
  @Depends("#testEmpty") 
  public Stack<Integer> testPush(Stack<Integer> $) { 
      $.push(42); 
      assertFalse($.isEmpty()); 
      return $; 
  }


As mentioned in this IBM article "In pursuit of code quality: JUnit 4 vs. TestNG":

One thing the JUnit framework tries to achieve is test isolation.
On the downside, this makes it very difficult to specify an order for test-case execution, which is essential to any kind of dependent testing.
Developers have used different techniques to get around this, like specifying test cases in alphabetical order or relying heavily on fixtures (@Before @After) to properly set things up.

These workarounds are fine for tests that succeed, but for tests that fail, they have an inconvenient consequence: every subsequent dependent test also fails. In some situations, this can lead to large test suites reporting unnecessary failures

So beware: if you retain any solution for ordering your JUnit tests the way you want... you need to think if that solution support a "skip" feature in order to allow other tests to proceed even if one of them fails.

VonC
I think he just want to see the _results_ in a specific order.
furtelwart
... yes hence my answer: since you can not sort the results, you can try to order the tests.
VonC
Yeah, just the results. And only because I might want to look for a specific test result after the unit tests have ran.I'm not sure why Eclipse can't order the test results as they're being run. Just use a SortedSet.
Gary Kephart
+1  A: 

As Gary said in the comments:

it would be nice if Unit Runner could be told to go ahead and order them by class name. Hmm, maybe I should look into the source code...

I did look but there's no hint of a functionality to sort these names. I would suggest a change request to the JUnit plugin, but I don't think, that there are lot of people using this thing, so: DIY.

I would like to see the solution if you modify the plugin code.

furtelwart
Yeah, probably the best solution is to modify the plugin code. Wish I had the time.
Gary Kephart
I know this problem :)
furtelwart