JUnit下的测试和suite


      Eclipse下使用JUnit3来进行测试的框架为:

package test;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import server.db.DB;
 
import junit.framework.TestCase;
 
public class DBTest extends TestCase {
 
         protected void setUp()throws Exception {
                   super.setUp();
         }
 
         protected voidtearDown() throws Exception {
                   super.tearDown();
         }
 
         public void testDB()throws ClassNotFoundException, SQLException {
                   // 数据库读取
                   Connectionconn = DB.getConn();
                   Statementstmt = conn.createStatement();
                   ResultSet rs= stmt.executeQuery("select * from users;");
                   System.out.println("----------\nDB:Teststart");
                   while(rs.next()) {
                            System.out
                                               .println(rs.getInt(1)+ rs.getString(2) + rs.getString(3));
                   }
                   assertEquals(1,1);
                   System.out.println("DB:Statusok\n----------");
         }
}

      所有类似的测试方法都可以写在此类中,使用断言对测试结果进行分析。多个测试例子需要使用JUnit的Suite来进行封装。其结构为:

package test;
 
import junit.framework.Test;
import junit.framework.TestSuite;
 
public class AllTests {
 
         public static Testsuite() {
                   TestSuitesuite = new TestSuite("Test for test");
                   //$JUnit-BEGIN$
                   suite.addTestSuite(ProducerToolTest.class);
                   suite.addTestSuite(DBTest.class);
                   //$JUnit-END$
                   returnsuite;
         }
 
}

      上述程序可以使用Eclipse的自动生成功能来进行,Junit选项位于Java下。以上测试结果为:

JUnit下的测试和suite_第1张图片

你可能感兴趣的:(JUnit下的测试和suite)