Junit - 忽略测试(Ignore Test)

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

  • Junit 4 Ignore Test 应用到某些测试方法上
  • Junit 4 Ignore Test 应用到整个测试类上

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

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

样例 Arithmetic.java

package in.co.javatutorials;
 
public class Arithmetic {
    public int add(int i, int j) {
        return i + j;
    }
 
    public int substract(int i, int j) {
        return i - j;
    }
}

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

package in.co.javatutorials;
 
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
 
/**
 * @author javatutorials.co.in
 */
public class ArithmeticMethodTest {
 
    /**
     * Example of test case success
     */
    @Test
    public void testAdd() {
        Arithmetic arithmetic = new Arithmetic();
        int actualResult = arithmetic.add(1, 2);
        // example of test case success
        int expectedResult = 3;
        assertEquals(expectedResult, actualResult);
    }
 
    /**
     * Example of @ignore annotation
     */
    @Test
    @Ignore
    public void testSubstract() {
        Arithmetic arithmetic = new Arithmetic();
        int actualResult = arithmetic.substract(4, 2);
        int expectedResult = 2;
        // example of test case failure
        assertEquals(expectedResult, actualResult);
    }
}

样例输出

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

 

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

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

样例 Arithmetic.java

package in.co.javatutorials;
 
public class Arithmetic {
    public int add(int i, int j) {
        return i + j;
    }
 
    public int substract(int i, int j) {
        return i - j;
    }
}

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

package in.co.javatutorials;
 
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
 
/**
 * @author javatutorials.co.in
 */
@Ignore
public class ArithmeticClassTest {
 
    /**
     * Example of test case success
     */
    @Test
    public void testAdd() {
        Arithmetic arithmetic = new Arithmetic();
        int actualResult = arithmetic.add(1, 2);
        // example of test case success
        int expectedResult = 3;
        assertEquals(expectedResult, actualResult);
    }
 
    /**
     * Example of @ignore annotation
     */
    @Test
    public void testSubstract() {
        Arithmetic arithmetic = new Arithmetic();
        int actualResult = arithmetic.substract(4, 2);
        int expectedResult = 2;
        // example of test case failure
        assertEquals(expectedResult, actualResult);
    }
}

样例输出

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


  1. Junit - 测试框架介绍
  2. Junit - Eclipse 教程
  3. Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
  4. Junit - 断言方法(Assert Methods)
  5. Junit - 参数化测试(Parameterized Test)
  6. Junit - 套件测试(Suite Test)
  7. Junit - 忽略测试(Ignore Test)
  8. Junit - 超时测试(Timeout Test)
  9. Junit - 期望异常测试(Expected Test)
  10. Junit - 优先级测试(FixMethodOrder Test)

 

本文出处为 http://blog.csdn.net/luanlouis,转载请注明出处,谢谢!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值