[ Team LiB ] Previous Section Next Section

4.10 Repeating Tests

4.10.1 Problem

You want to run certain tests repeatedly.

4.10.2 Solution

Use the junit.extensions.RepeatedTest class.

4.10.3 Discussion

You may want to run certain tests repeatedly to measure performance or to diagnose intermittent problems.[7] The RepeatedTest class makes this easy:

[7] Threading bugs are often intermittent.

public static Test suite(  ) {
    // run the entire test suite ten times
    return new RepeatedTest(new TestSuite(TestGame.class), 10);
}

RepeatedTest's first argument is another Test to run; the second argument is the number of iterations. Since TestSuite implements the Test interface, we can repeat the entire test as just shown. Here is how you can build a test suite where different tests are repeated differently:

TestSuite suite = new TestSuite(  );
// repeat the testCreateFighter test 100 times
suite.addTest(new RepeatedTest(new TestGame("testCreateFighter"), 100));
// run testSameFighters once
suite.addTest(new TestGame("testSameFighters"));
// repeat the testGameInitialState test 20 times
suite.addTest(new RepeatedTest(new TestGame("testGameInitialState"), 20));

4.10.4 See Also

Recipe 4.14 shows more examples of RepeatedTest.

    [ Team LiB ] Previous Section Next Section