All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.carrotsearch.examples.randomizedrunner.Test006RepeatingTests Maven / Gradle / Ivy

Go to download

Simple use-case examples meant to be included in some end-user documentation if it's ever going to be written...

There is a newer version: 2.1.17
Show newest version
package com.carrotsearch.examples.randomizedrunner;

import org.junit.*;

import com.carrotsearch.randomizedtesting.*;
import com.carrotsearch.randomizedtesting.annotations.Repeat;
import com.carrotsearch.randomizedtesting.annotations.Seed;

/**
 * In example {@link Test005RecoveringRandomSeed} we presented
 * {@link Randomness} contexts and how they are derived from a master seed. Once
 * you know a certain test case fails it is usually beneficial to immediately
 * check if it always fails on a given seed (which means there is a
 * deterministic failure scenario). A simple way to do so would be to re-run a
 * test case a few times. The same effect can be achieved by adding a
 * {@link Repeat} annotation with {@link Repeat#useConstantSeed()} set to
 * false attribute as shown in the method {@link #repeatFailure()}
 * below. 
 * 
 * {@literal @}{@link Repeat}(iterations = 5, useConstantSeed = true)
 * {@literal @}{@link Seed}("f00ddead")
 * {@literal @}{@link Test}
 * public void repeatFailure() { //...
 * 
* Note how the seed is fixed using {@link Seed} annotation (on the * method) rather than on the master. This ensures the method's context is * pinned to that value, but the master is still random. If you have * {@link BeforeClass} hooks that depend on randomness you should use * suite-level {@link Seed} annotation and pin the master seed instead. * *

You can also set {@link Repeat#useConstantSeed()} to false and * then every iteration of the test method will have a pseudo-random context derived * from the first one (pinned or not). This can be useful to tell how frequently * a given test case fails for a random seed. For {@link #halfAndHalf()} method * about 50% of iterations will fail. */ public class Test006RepeatingTests extends RandomizedTest { @Repeat(iterations = 5, useConstantSeed = true) @Seed("f00ddead") @Test public void repeatFailure() { assertTrue(randomBoolean()); } @Repeat(iterations = 10, useConstantSeed = false) @Test public void halfAndHalf() { assertTrue(randomBoolean()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy