Saturday, December 31, 2011

Creating a JUnit Test Suite

A TestSuite is a simple way of running one class that, in turn, runs all test cases at one time. 
1. There are four ways to create a JUnit Test Suite Class. First, select the directory (usually tests/) that you wish to create the test suite class in.
  1. Select File > New > Other... > Java > JUnit > JUnit Test Suite.
  2. Select the arrow of the  New icon(shown at the end of the post) in the upper left of the toolbar. Select Other... > Java > JUnit > JUnit Test Suite,
  3.  Right click on a package in the Package Explorer view in the Java Perspective, and select New > Other... > Java > JUnit > JUnit Test Suite, or
  4. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestSuite as the super class of the test class you are creating.
2. In the New Junit Test Suite wizard, check to make sure that you are creating the TestSuite in the proper source folder and the proper package. Give the test suite a name. The default name is AllTests.java 
  • Use the Browse buttons to search for a source folder, and the package.
  • Select which test classes you would like to include in the test suite.
3. Click Finish. The new test suite class will be open in the editor. 
4. Below is a test suite template from the JUnit 3. This test suite demonstrates the basic functionality of the suite() method, which is what you add each of the test cases to the suite in. This should all be generated for you by Eclipse if you use the first 3 methods in step 1 to create the Test Suite. 
  1. import junit.framework.Test;
  2. import junit.framework.TestSuite;
  3. public class SampleTestSuite {
  4.     public static Test suite() {
  5.         TestSuite suite = new TestSuite(SampleTestSuite.class.getName());
  6.         //$JUnit-BEGIN$
  7.         // Add one entry for each test class
  8.         // or test suite.
  9.         suite.addTestSuite(SampleTest.class);
  10.         //$JUnit-END$
  11.         return suite;
  12.     }
  13. }
Icons Used:
New


No comments:

Post a Comment