Junit4.x及之前版本版本使用方法比较(断言)

自从junit4加入注解(Annotation)以后,其使用方式相对于以前版本简单了许多,这里将分别从测试用例(TestCase)和测试集(TestSuit)两个方面来说明。

 

一. TestCase

 

在TestCase方面区别主要有以下两个方面:

1. 4.x之前,测试类都必须继承TestCase类才能进行单元测试,而在4.X中则没有了这个限制;

2. 4.x之前,测试类中方法必须以test开头,而在4.x中不再强制要求每个测试方法必须以test开口,改由在方法之上增加"@Test"注解即可。

 

具体差异可参加如下两个示例

 

junit4.x之前版本

  1. package test;  
  2.   
  3. import org.junit.Assert;  
  4. import junit.framework.TestCase;  
  5.   
  6. public class TestCaseSample extends TestCase{  
  7.   
  8.     /** 
  9.      * 测试初始化 
  10.      */  
  11.     public void setUp() throws Exception {  
  12.       
  13.     }  
  14.       
  15.     /** 
  16.      * 测试方法 
  17.      * 
  18.      */  
  19.     public void testMethod1(){  
  20.         Assert.assertTrue(true);  
  21.     }  
  22.       
  23.     /** 
  24.      * 测试方法 
  25.      * 
  26.      */  
  27.     public void testMethod2(){  
  28.         Assert.assertTrue(true);  
  29.     }  
  30.       
  31.     /** 
  32.      *释放资源(测试结束时) 
  33.      */  
  34.     public void tearDown() throws Exception {  
  35.       
  36.     }  
  37.   
  38. }

junit4.x

  1. package test;  
  2.   
  3. import org.junit.After;  
  4. import org.junit.Assert;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7.   
  8. public class TestCaseSample4 {  
  9.   
  10.     /** 
  11.      * 测试初始化 
  12.      */  
  13.     @Before  
  14.     public void setUp() throws Exception {  
  15.       
  16.     }  
  17.       
  18.     /** 
  19.      * 测试方法 
  20.      * 
  21.      */  
  22.     @Test  
  23.     public void testMethod1(){  
  24.         Assert.assertTrue(true);  
  25.     }  
  26.       
  27.     /** 
  28.      * 测试方法 
  29.      * 
  30.      */  
  31.     @Test  
  32.     public void testMethod2(){  
  33.         Assert.assertTrue(true);  
  34.     }  
  35.       
  36.     /** 
  37.      *释放资源(测试结束时) 
  38.      */  
  39.     @After  
  40.     public void tearDown() throws Exception {  
  41.       
  42.     }  
  43.   
  44. }

从上面比较可看出,采用注解后的测试用例写起来更简单。并且,测试方法可以单独的写在任何地方,从而增加了测试的灵活性。

 

二 TestSuite

 

采用注解后的TestSuite不必再实例化TestSuite,而是通过@Suite即可直接实现测试集的建立。而且,采用注解后的TestSuite添加测试类更方便。具体差异参加如下示例:

 

junit4.x之前版本

  1. package test;  
  2.   
  3. import junit.framework.Test;  
  4. import junit.framework.TestSuite;  
  5.   
  6. public class TestsSuite {  
  7.   
  8.     public static Test suite() {  
  9.         TestSuite suite = new TestSuite("Test for test");  
  10.         //$JUnit-BEGIN$   
  11.         suite.addTestSuite(TestCaseSample.class);  
  12.         //$JUnit-END$   
  13.         return suite;  
  14.     }  
  15.   
  16. }

junit4.x

  1. package test;  
  2.   
  3. import org.junit.runner.RunWith;  
  4. import org.junit.runners.Suite;  
  5.   
  6. @RunWith(Suite.class)  
  7. @Suite.SuiteClasses({  
  8.     TestCaseSample.class  
  9. })  
  10. public class TestSuite4 {  
  11.   
  12. }

(文/gaoyusi4964238)

本文来源:http://blog.csdn.net/gaoyusi4964238/article/details/4782537


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。