public class Test003UsingRandomness extends RandomizedTest
Random provided by
RandomizedRunner. The idea behind randomized tests is to, for each
test execution:
Let's see this on a simple example of a method that adds two
integers (Test003UsingRandomness.Adder.add(int, int)). We can test this method using a "fixed" test
case as shown in fixedTesting() but this test will always execute in
an identical way (which is good if you're looking for regression coverage but
bad if you want to expand your tested domain).
A randomized test, on the other hand, will pick parameters from a larger
spectrum of values and assert on the method's contract. Here, we can make
sure the sum is always larger or equal than the arguments given two positive
arguments. This assertion will fail quite often because of integer overflows as shown
in randomizedTesting() (re-run the test a few times if it doesn't
fail the first time).
While the above example is trivial most of the bugs in code stem from similar
subtleties (possibly resulting from more complex interactions). In many cases
the "contract" that we can assert on can be stated as "throw no-exception" given
valid arguments. An example of that is in method expectNoException().
At first glance this method will work most of the time, but try passing
Integer.MIN_VALUE as a random number and see what will happen.
| Modifier and Type | Class and Description |
|---|---|
static class |
Test003UsingRandomness.Adder |
ISO8859_1, SYSPROP_MULTIPLIER, US_ASCII, UTF16, UTF32, UTF8| Constructor and Description |
|---|
Test003UsingRandomness() |
| Modifier and Type | Method and Description |
|---|---|
void |
expectNoException() |
void |
fixedTesting() |
void |
randomizedTesting() |
$, $$, 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.