Ignoring a Test

如果我们不想让某个测试失败,我们仅仅想要忽略它,那么我们可以暂时的disable它。

有三种方法来忽略一个测试:

  • 把方法注释掉
  • 删除 @Test 注释
  • 增加 @Ignore注释: @Ignore([ignore reason])

方法一和方法二会导致测试结果不包括该测试。而使用方法三的话,执行完测试之后,我们不仅会知道跑多少测试,失败多少测试,还会知道有多少测试被忽略了。通过在@Ignore内添加字符串参数我们还可以记录该测试被忽略的原因。

import static org.junit.Assert.assertArrayEquals;  
import org.junit.Ignore;  
import org.junit.Test;  
  
public class IgnoreTest {  
  
    @Ignore("testAssertArrayEquals ignore")  
    @Test  
    public void testAssertArrayEquals(){  
        byte[] expected="trial".getBytes();  
        byte[] actual="trial".getBytes();  
        assertArrayEquals("failure-byte arrays not same",expected, actual);  
    }  
}  

测试结果:

Runs:1/1(1 skipped)           Errors:0           Failures:0

原文地址:https://www.cnblogs.com/miniren/p/4638505.html