六、忽略测试

Junit 4 忽略测试(Ignore test)被用来禁止执行junit测试类的某些或者全部测试方法。Junit 提供了@Ignore注解来实现 忽略测试。它可以用来跳过失败、或者抛出异常的测试方法。
  • Junit 4 Ignore Test 应用到某些测试方法上
  • Junit 4 Ignore Test 应用到整个测试类上

Junit 4 Ignore Test 应用到某些测试方法上

在需要忽略或者禁止junit 测试类中的任意测试方法上,使用@ignore注解。

样例

   Arithmetic.Java


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package in.co.javatutorials;  
  2.    
  3. public class Arithmetic {  
  4.     public int add(int i, int j) {  
  5.         return i + j;  
  6.     }  
  7.    
  8.     public int substract(int i, int j) {  
  9.         return i - j;  
  10.     }  
  11. }  

ArithmeticMethodTest.java testSubstract() 方法使用了@Ignore注解,在执行测试类时,此方法会被忽略。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package in.co.javatutorials;  
  2.    
  3. import static org.junit.Assert.assertEquals;  
  4.    
  5. import org.junit.Ignore;  
  6. import org.junit.Test;  
  7.    
  8. /** 
  9. * @author javatutorials.co.in 
  10. */  
  11. public class ArithmeticMethodTest {  
  12.    
  13.     /** 
  14.      * Example of test case success 
  15.      */  
  16.     @Test  
  17.     public void testAdd() {  
  18.         Arithmetic arithmetic = new Arithmetic();  
  19.         int actualResult = arithmetic.add(12);  
  20.         // example of test case success  
  21.         int expectedResult = 3;  
  22.         assertEquals(expectedResult, actualResult);  
  23.     }  
  24.    
  25.     /** 
  26.      * Example of @ignore annotation 
  27.      */  
  28.     @Test  
  29.     @Ignore  
  30.     public void testSubstract() {  
  31.         Arithmetic arithmetic = new Arithmetic();  
  32.         int actualResult = arithmetic.substract(42);  
  33.         int expectedResult = 2;  
  34.         // example of test case failure  
  35.         assertEquals(expectedResult, actualResult);  
  36.     }  
  37. }  

样例结果输出

样例 eclipse junit 窗口的结果输出如下:. testSubstract() 由于被@Ignore注解,测试类在执行时此方法被忽略。


Junit 4 Ignore Test 应用到整个测试类上

忽略或者禁止junit测试类上的所有方法的执行,则在测试类上添加@Ignore注解即可。

样例

Arithmetic.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package in.co.javatutorials;  
  2.    
  3. public class Arithmetic {  
  4.     public int add(int i, int j) {  
  5.         return i + j;  
  6.     }  
  7.    
  8.     public int substract(int i, int j) {  
  9.         return i - j;  
  10.     }  
  11. }  

ArithmeticClassTest.java 其所有方法都将被忽略:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package in.co.javatutorials;  
  2.    
  3. import static org.junit.Assert.assertEquals;  
  4.    
  5. import org.junit.Ignore;  
  6. import org.junit.Test;  
  7.    
  8. /** 
  9. * @author javatutorials.co.in 
  10. */  
  11. @Ignore  
  12. public class ArithmeticClassTest {  
  13.    
  14.     /** 
  15.      * Example of test case success 
  16.      */  
  17.     @Test  
  18.     public void testAdd() {  
  19.         Arithmetic arithmetic = new Arithmetic();  
  20.         int actualResult = arithmetic.add(12);  
  21.         // example of test case success  
  22.         int expectedResult = 3;  
  23.         assertEquals(expectedResult, actualResult);  
  24.     }  
  25.    
  26.     /** 
  27.      * Example of @ignore annotation 
  28.      */  
  29.     @Test  
  30.     public void testSubstract() {  
  31.         Arithmetic arithmetic = new Arithmetic();  
  32.         int actualResult = arithmetic.substract(42);  
  33.         int expectedResult = 2;  
  34.         // example of test case failure  
  35.         assertEquals(expectedResult, actualResult);  
  36.     }  
  37. }  
样例输出

如下Junit 窗口所示,junit测试类上的所有方法都被忽略:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值