TestSuite (see Figure B-10) is a class representing a collection of Test s. Since it implements Test , it can be run just like a TestCase . When run, a TestSuite runs all the Test s it contains. It may contain both TestCase s and other TestSuite s.

A TestSuite can be constructed by giving it the class name of a TestCase . The TestSuite constructor uses reflection to find all methods in the TestCase having names starting with test . The code below adds all of BookTest 's test methods to a TestSuite and runs it:

 TestSuite test = new TestSuite( BookTest.class ); test.run( new TestResult( ) ); 

Tests also can be added to a TestSuite using the addTest( ) method.

Figure B-10. The class TestSuite


 public class TestSuite extends Object implements Test 


TestSuite( )

A constructor that creates an empty TestSuite .

TestSuite(String name)

A constructor that creates an empty TestSuite with the given name.

TestSuite(Class class)

A constructor that takes a Class , uses reflection to find all methods with names starting with test , and adds them to the TestSuite as test methods.

TestSuite(Class class, String name)

A constructor that creates a TestSuite with the given name and all test methods found in the Class , as described for the previous constructor.

Public Methods

void addTest(Test test)

Adds a Test to the TestSuite .

void addTestSuite(Class testClass)

Adds the test methods from the Class to the TestSuite . Test methods are found using reflection.

int countTestCases( )

Returns the total number of test cases that will be run by this TestSuite . Test cases are counted by recursively calling countTestCases( ) for every Test in this TestSuite .

static Test createTest(Class theClass, String name)

Creates an instance of Class as a Test with the given name.

String getName( )

Returns the name of the TestSuite .

static java.lang.reflect.Constructor getTestConstructor(Class theClass)

Gets a constructor for the given Class that takes a single String as its argument, or gets a constructor that takes no arguments.

void run(TestResult result)

Runs the Test s in this TestSuite and collects the results in TestResult .

void runTest(Test test, TestResult result)

Runs Test and collects the results in TestResult .

void setName(String name)

Sets the name of the TestSuite .

Test testAt(int index)

Returns the Test at the given index.

int testCount( )

Returns the number of Test s in this TestSuite .

java.util.Enumeration tests( )

Returns the Test s as an Enumeration .

String toString( )

Returns a string representation of this TestSuite .

Protected/Private Methods

private void addTestMethod(java.lang.reflect.Method m, Vector names, Class class)

A private method to add a test method to this TestSuite .

private static String exceptionToString(Throwable t)

Returns the Throwable 's stack trace as a string.

private boolean isPublicTestMethod(java.lang.reflect.Method m)

Returns TRUE if Method has public access.

private boolean isTestMethod(java.lang.reflect.Method m)

A private method that returns TRUE if Method has no arguments, returns void , and has public access.

private static Test warning(String message)

Returns a Test that will fail and logs a warning message.


private String fName

The name of this TestSuite .

private Vector fTests

The Test s contained by this TestSuite .

Unit Test Frameworks
Unit Test Frameworks
ISBN: 0596006896
EAN: 2147483647
Year: 2006
Pages: 146
Authors: Paul Hamill

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net