@Seed(value="AF567B2B9F8A8F1C") public class Test005RecoveringRandomSeed extends RandomizedTest
RandomizedRunner uses several "contexts", each of which is assigned a
predictable Random and is modeled using a Randomness
instance. The "suite" or "master" context is available from
BeforeClass or AfterClass hooks, for example. Each test
method has a nested context with a random seed derived from the master. This
way even though the order of tests is shuffled and each test can make a
random number of calls to its own context's Random instance, the
global execution paths can always be repeated from the same master seed. The
question is: how do we know what master seed was used? There are at least two
ways to find out.
The master seed is always available from
RandomizedContext.getRunnerSeedAsString() so one can simply print it to the
console. The current context's Randomness itself can be printed to the
console. In two methods in this class printMasterContext() and printContext()
we print the master seed and current context's Randomness, note how the static
context's Randomness is identical with the runner's but the test context
is a derived value.
# Static context (@BeforeClass) AF567B2B9F8A8F1C [Randomness, seed=[AF567B2B9F8A8F1C]] # Test context (@Test) AF567B2B9F8A8F1C [Randomness, seed=[EE581D5EC61D6BCF]]In
Test006RepeatingTests we will see how this derived
seed is used with Repeat annotation.
Normally we will not be interested in a random seed if a test case passes. But if a test
case fails we will want to know the seed to be able to repeat the test. RandomizedRunner
augments the stack trace of all exceptions that cross the context boundary (this includes
assertion errors, assumption failures and any other exceptions). In method failure()
we demonstrate this by failing on a constant condition. If you run this test suite, you'll note
the stack trace of the failing method to be something like this:
java.lang.AssertionError at __randomizedtesting.SeedInfo.seed([AF567B2B9F8A8F1C:44E2D1A039274F2A]:0) at org.junit.Assert.fail(Assert.java:92)The first line of the stack trace is a synthetic (non-existing) class with "source file" entry containing all contexts' seeds on the stack (from master to the current test method). In this case, you can see the master context first (AF567B2B9F8A8F1C), followed by the test's context (44E2D1A039274F2A). The entire class has a fixed master seed so that the result will always be the same here:
@Seed("AF567B2B9F8A8F1C")
public class Test005RecoveringRandomSeed extends RandomizedTest { // ...
ISO8859_1, SYSPROP_MULTIPLIER, US_ASCII, UTF16, UTF32, UTF8| Constructor and Description |
|---|
Test005RecoveringRandomSeed() |
| Modifier and Type | Method and Description |
|---|---|
void |
failure() |
void |
printContext() |
static void |
printMasterContext() |
$, $$, assumeFalse, assumeFalse, assumeNoException, assumeNoException, assumeNotNull, assumeTrue, assumeTrue, atLeast, atMost, between, closeAfterSuite, closeAfterTest, frequently, getContext, getRandom, globalTempDir, isNightly, iterations, multiplier, newServerSocket, newTempDir, newTempDir, newTempFile, newTempFile, randomAsciiOfLength, randomAsciiOfLengthBetween, randomBoolean, randomByte, randomDouble, randomFloat, randomFrom, randomFrom, randomGaussian, randomInt, randomInt, randomIntBetween, randomLocale, randomLong, randomRealisticUnicodeOfCodepointLength, randomRealisticUnicodeOfCodepointLengthBetween, randomRealisticUnicodeOfLength, randomRealisticUnicodeOfLengthBetween, randomShort, randomTimeZone, randomUnicodeOfCodepointLength, randomUnicodeOfCodepointLengthBetween, randomUnicodeOfLength, randomUnicodeOfLengthBetween, rarely, scaledRandomIntBetween, sleep, systemPropertyAsBoolean, systemPropertyAsDouble, systemPropertyAsFloat, systemPropertyAsInt, systemPropertyAsLongassertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertTrue, assertTrue, fail, failCopyright © 2011-2012 Carrot Search s.c.. All Rights Reserved.