Running Tests Concurrently
Problem
You want to run several tests concurrently using threads.
Solution
Use the
junit.extensions.ActiveTestSuite
class to build a suite of tests that run
concurrently.
Discussion
The ActiveTestSuite class runs each of its tests
in a separate thread. The suite does not finish until all of the test
threads are complete. Example 4-6 shows how to run
three different test methods in three different
threads.
Example 4-6. Running tests in different threads
public static Test suite( ) {
TestSuite suite = new ActiveTestSuite( );
suite.addTest(new TestGame("testCreateFighter"));
suite.addTest(new TestGame("testGameInitialState"));
suite.addTest(new TestGame("testSameFighters"));
return suite;
}While you probably won’t use this technique often,
running tests in threads can serve as a rudimentary stress tester.
You might also use ActiveTestSuite to help
identify threading problems in your code. By combining
ActiveTestSuite with
RepeatedTest, you can uncover threading problems
that only show up intermittently.
Example 4-7 shows how you can combine repeated tests
and other test suites into an ActiveTestSuite.
Each of the repeated tests runs in a different thread; therefore, you
end up with four threads. If you are experiencing occasional
threading glitches, you might want to increase the number of
iterations and run a similar test suite overnight.
Example 4-7. A more advanced test suite
public static Test suite( ) { TestSuite suite = new ActiveTestSuite( ); // run one ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access