public class Test004MoreRandomness extends Object
RandomizedRunner. An instance of
Random is of course available from a RandomizedContext, but
even tests themselves are randomized, or to be more precise: their order of
execution is shuffled.
In this example we have two classes that contain three methods (they're nested
under a single suite class for simplicity). Every execution of class
Test004MoreRandomness.OrderRandomized will be different, shuffling
test methods around (and the random numbers written to the output).
We can "pin" the execution order by forcing the master random
seed using Seed annotation on the class (or a system property
SysGlobals.SYSPROP_RANDOM_SEED). Doing so also fixes all derivative random
generators in all tests - this is shown in
Test004MoreRandomness.OrderRandomizedButFixed, every execution of this
class will be identical (and will emit identical pseudo-random numbers).
All this is meant for one purpose: help in reproducing a failed randomized test
case. Once a test case fails, make sure you write down the random seed number
that caused the failure and add appropriate Seeds annotation on the method
that failed like so:
@where 012345 is the replaced by the seed that caused the failure. This makes the test methods run with a fixed seed once and then with a random seed again, easily creating a new regression test so that the bug does not reoccur in the future. An example of that is shown inSeeds({ @Seed("012345"), @Seed() })
Test004MoreRandomness.OrderRegression.regression(). Also note
how RandomizedRunner modifies test method names for such "expanded" methods, appending
the random seed as a parameter. This is needed to avoid duplicate
test Description objects (a design flaw in JUnit). We will see these parameters
again in the example concerning parameterized tests.| Modifier and Type | Class and Description |
|---|---|
static class |
Test004MoreRandomness.OrderRandomized |
static class |
Test004MoreRandomness.OrderRandomizedButFixed |
static class |
Test004MoreRandomness.OrderRegression |
| Constructor and Description |
|---|
Test004MoreRandomness() |
Copyright © 2011-2012 Carrot Search s.c.. All Rights Reserved.