分享

junit

 helloworld_sh 2011-03-11
 近期写单元测试计划,公司准备把junit3.8升级到junit4以上的版本,所以研究了一下,写下学习过程和心得体会,为了巩固学习成果,所以把学习心得写下来加深印象,也供需要的朋友查阅,少走弯路。
   好了,废话不多说,直接开始:
   假设我们要写一个整数除法和乘法的类,并且给他写测试用例:

1) 建立Math类

工具是eclipse3.3
Java代码 复制代码
  1. /**  
  2.  * @author bulargy.j.bai  
  3.  * @mail bulargy@gmail.com  
  4.  * @创建时间:Mar 10, 2008  
  5.  * @描述:一个整数除法和乘法的工具类  
  6.  */  
  7. public class Math {   
  8.     public static int divide(int x,int y) {   
  9.         return x/y;   
  10.     }   
  11.   
  12.     public static int multiple(int x,int y) {   
  13.         return x*y;   
  14.     }   
  15. }  


2) 建立测试用例
选中需要建立测试用例的包,选择new->other->JUnit Test Case。
有5个方法可以选择:
setUp()方法在测试方法前调用,一般用来做测试准备工作。
tearDown()方法在测试方法后调用,一般作测试的清理工作。
setUpBeforeClass()方法在整个类初始化之后调用,一般用来做测试准备工作。
tearDownAfterClass()方法在整个类结束之前调用,一般作测试的清理工作。
constructor()为是否包含构造方法。

自动生成的代码如下:
Java代码 复制代码
  1. /**  
  2.  * @author bulargy.j.bai  
  3.  * @mail bulargy@gmail.com  
  4.  * @创建时间:Mar 11, 2008  
  5.  * @描述:  
  6.  */  
  7. public class MathTest {   
  8.   
  9.     @BeforeClass  
  10.     public static void setUpBeforeClass() throws Exception {   
  11.     }   
  12.   
  13.     @AfterClass  
  14.     public static void tearDownAfterClass() throws Exception {   
  15.     }   
  16.        
  17.     @Test  
  18.     public void testDivide() {   
  19.         fail("Not yet implemented");   
  20.     }   
  21.   
  22.     @Test  
  23.     public void testMultiple() {   
  24.         fail("Not yet implemented");   
  25.     }   
  26. }  

说明:
@BeforeClass标签注释的方法用于在整个类测试过程的初始化后调用一次,@AfterClass标签注释的方法则是整个测试类结束之前调用一次。这2个标签的搭配可以避免使用@Before、@After标签组合在每个测试方法前后都调用的弊端,减少系统开销,提高系统测试速度。(不过对环境独立性要求较高的测试还是应当使用@Before、@After来完成)
@Test标签用来标注待测试的方法,按照类中声明的顺序执行。


我们在testDivide方法加入测试代码,分别测试三种情况:
a. 完全正确也没有可能出错的数据,如:9除3  结果必须等于3
b. 可能有问题的边缘数据,如:10除3 结果也必须等于3
c. 错误的数据,如:10除0 必须抛出异常

忽略testMultiple方法

代码如下:
Java代码 复制代码
  1. @Test(expected=ArithmeticException.class)   
  2. public void testDivide() {   
  3.     assertEquals(3,Math.divide(9,3));   
  4.     assertEquals(3,Math.divide(10,3));   
  5.     Math.divide(10,0); //除数不能为0,会抛出异常   
  6. }   
  7.   
  8. @Ignore("忽略乘法测试")   
  9. @Test  
  10. public void testMultiple() {   
  11. }  

说明:
Junit4为测试方法增加了判断异常的方式,避免了以前还要通过try/catch块捕捉异常再抛出的复杂方式,简单的这样声明“@Test(expected=ArithmeticException.class)”Junit4就会检查此方法是否抛出ArithmeticException异常,如果抛出则测试通过,没抛出则测试不通过(@Test标签还有一些其他参数,例如超时测试@Test(timeout=1)这样,但是由于并不能准确反应实际时间,所以应用较少,经过我测试误差太大绝对不适合拿来做超时测试的)
@Ignore标签会告诉Junit4忽略它所标注的方法,例如数据库不可用时可以用此标注标注一些测试数据库连接的方法来避免测试失败。

3) 运行测试
系统会打开JUnit透视图,如果测试全部通过,则显示颜色条为绿色;我们将assertEquals(3,Math.divide(9,3));改成assertEquals(2,Math.divide(9,3));则显示颜色条为红色,我们可以对错误或者故障的地方进行追踪。

4) 创建测试套件
  测试套件可以将多个测试用例合在一起测试,将相关的测试用例合成一个测试套件,在做一个修改后,只需要运行测试套件就可以,不需要运行每一个测试用例。
  Junit4没有采用以前的套件测试方法,同样使用annotation的方式来进行。简单在你所要构建测试套件的包里创建一个文件,一般以包名+4Suite
下面我在上面的测试包中复制一下之前的测试类并且一个改名字叫做MathTestAnother,新建一个class类叫做Uitl4Suite,代码如下:
Java代码 复制代码
  1. import org.junit.runner.RunWith;   
  2. import org.junit.runners.Suite;   
  3. import org.junit.runners.Suite.SuiteClasses;   
  4. /**  
  5.  * @author bulargy.j.bai  
  6.  * @mail bulargy@gmail.com  
  7.  * @创建时间:Mar 11, 2008  
  8.  * @描述:util包的测试套件  
  9.  */  
  10. @RunWith(Suite.class)   
  11. @SuiteClasses({MathTest.class,   
  12.            MathTestAnother.class})   
  13. public class Util4Suite {   
  14. }  

说明:
通过@RunWith和@SuiteClasses标签来注释一个空的包含无参数构造函数的类来作为套件类,将需要组成套件运行的类加到@SuiteClasses的属性中即可。

可以看到运行套件类的结果是2个测试类都进行了测试。

5) 参数测试
修改 testMultiple
Java代码 复制代码
  1. //@Ignore("忽略乘法测试")   
  2. @Test  
  3. public void testMultiple() {   
  4.     assertEquals(result,Math.multiple(faciend,multiplicator));   
  5. }  

编写参数方法:
Java代码 复制代码
  1. @Parameters  
  2.     public static Collection multipleValues() {   
  3.         return Arrays.asList(new Object[][] {   
  4.         {326 },   
  5.         {4312 },   
  6.         {215105 },   
  7.         {1122242 },   
  8.         {8972 }});   
  9.     }  

说明:
需要使用@Parameters标签注解一个静态的返回集合对象的方法

增加成员变量和构造函数:
Java代码 复制代码
  1. int faciend;   
  2. int multiplicator;   
  3. int result;   
  4.   
  5. public MathTest(int faciend, int multiplicator, int result) {   
  6.     this.faciend = faciend;   
  7.     this.multiplicator = multiplicator;   
  8.     this.result = result;   
  9. }  

最后在给测试类增加如下注释:
Java代码
  1. @RunWith(Parameterized.class)  


完整的循环测试代码如下:
Java代码
  1. import static org.junit.Assert.*;   
  2.   
  3. import java.util.Arrays;   
  4. import java.util.Collection;   
  5.   
  6. import org.junit.AfterClass;   
  7. import org.junit.BeforeClass;   
  8. import org.junit.Ignore;   
  9. import org.junit.Test;   
  10. import org.junit.runner.RunWith;   
  11. import org.junit.runners.Parameterized;   
  12. import org.junit.runners.Parameterized.Parameters;   
  13.   
  14. /**  
  15.  * @author bulargy.j.bai  
  16.  * @mail bulargy@gmail.com  
  17.  * @创建时间:Mar 11, 2008  
  18.  * @描述:  
  19.  */  
  20. @RunWith(Parameterized.class)   
  21. public class MathTest {   
  22.     int faciend;   
  23.     int multiplicator;   
  24.     int result;   
  25.   
  26.     public MathTest(int faciend, int multiplicator, int result) {   
  27.          this.faciend = faciend;   
  28.          this.multiplicator = multiplicator;   
  29.          this.result = result;   
  30.     }   
  31.   
  32.     @BeforeClass  
  33.     public static void setUpBeforeClass() throws Exception {   
  34.     }   
  35.   
  36.     @AfterClass  
  37.     public static void tearDownAfterClass() throws Exception {   
  38.     }   
  39.   
  40.     @Test(expected=ArithmeticException.class)   
  41.     public void testDivide() {   
  42.         assertEquals(3,Math.divide(9,3));   
  43.         assertEquals(3,Math.divide(10,3));   
  44.         Math.divide(10,0);//除数不能为0,会抛出异常   
  45.   
  46.     }   
  47.   
  48.     //@Ignore("忽略乘法测试")   
  49.     @Test  
  50.     public void testMultiple() {   
  51.         assertEquals(result,Math.multiple(faciend,multiplicator));   
  52.     }   
  53.        
  54.     @Parameters  
  55.     public static Collection multipleValues() {   
  56.      return Arrays.asList(new Object[][] {   
  57.         {326 },   
  58.         {4312 },   
  59.         {215105 },   
  60.         {1122242 },   
  61.         {8972 }});   
  62.     }   
  63.   
  64. }  

OK,大功告成。测试看看吧,测试类跑了5次~~。

大概就这么多体会了,总得来说JUnit4以后测试还是很方便的,顺便这个是仅仅是为了做例子,实际使用中由于JUnit4不再受命名的限制,所以应该划分更细粒度的测试来完成,一个方法的正确,异常,错误及边界数据完全可以分开来写测试方法。由于大部分情况资源只用加载和释放一次就足够,大大提高的测试的速度,再也不会有以前那样点开测试然后去泡咖啡的情况出现了~~呵呵~~

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多