You may want to run certain tests repeatedly to measure performance
or to diagnose intermittent problems.[24] The
RepeatedTest
class makes this easy:
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));
Recipe 4.14 shows more examples of
RepeatedTest
.
Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.