dev@glassfish.java.net

Re: randomize junit test order

From: Andreas Loew <Andreas.Loew_at_oracle.com>
Date: Sun, 26 Feb 2012 00:16:23 +0100

Bill,

indeed, it must not make any difference in what order JUnit test cases
are executed.

But I would not conclude from the scenario you describe that that you
need some means to run the tests in random order, rather I think that a
JUnit test suite/class which has such an order dependency between its
testXXX() methods is faulty/broken as such, i.e. it does not make proper
use of JUnit's setUp() and tearDown() facilities in order to make every
single test run in a clearly defined scenario where there must not be
any notion of "state" left for testBBB() from a previous invocation of
any testAAA() method within the same class, whether it happened to run
before or afterwards.

The bug here is in the test class as such: Either the setUp() or the
tearDown() method overlooks to remove some remaining "state"
information, and the true solution is to fix the test class to remove
any state dependencies rather than trying to make JUnit run the tests in
random order.

After all, we would like our software and our JUnit tests to behave in a
deterministic way, wouldn't we? Not addressing the state dependency
between two testXXX() methods, but only relying on JUnit to execute the
tests in arbitrary order makes the whole JUnit testing
non-deterministic, which is about the last thing you would expect from
your unit testing.

Base line: IMO, you must fix the test and remove the state dependency
between its methods rather than worrying about introducing random order...

HTH & best regards,

Andreas


Am 25.02.2012 21:26, schrieb Bill Shannon:
> I've run into a few cases recently where a junit test had an order
> dependency, causing it to fail on one of my machines. What I'd like
> is a way to randomize the order of tests within a test file.
>
> Looking at
> http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html
> It looks like setting runOrder=random will cause the test *files* to
> be run in random order (at least, that's the way I interpret it based
> on the default value being "filesystem"), but what I want is the tests
> within each test file to run in random order.
>
> I found this plugin http://randomjunit.sourceforge.net/ that seems to
> do what I want, except that it requires modifying the tests themselves
> to specify that you want to run them in random order.
>
> I'm really looking for something that we can configure in pom.xml
> without needing to change any tests. Does anyone know how to do this?

-- 
Andreas Loew | Senior Java Architect
ACS Principal Service Delivery Engineer
ORACLE Deutschland B.V. & Co. KG