跳过测试方法(测试类)(@Ignore)

本文介绍了如何在JavaJUnit中使用@Ignore注解来跳过测试方法,包括在方法级别和类级别应用的情况,以及导入必要的库。
摘要由CSDN通过智能技术生成

1.什么情况下要使用跳过测试(测试类)方法?

  • 写了一个测试方法但是不想执行
    1. 删掉该测试方法(测试类)
    2. 注释该测试方法(测试类)
    3. 使用@Ignore注解

2.示例

2.1 必要工作

  • 导入类库

import org.junit.Ignore;

2.2 使用@Ignore注解跳过测试方法

image.png

@Ignore方法写测试类上 代表该方法在执行时要跳过

  • 代码
package com.jaylan.example;
import  org.junit.Test;
import  org.junit.Ignore;
//使用@Ignore跳过测试方法
public class ExampleTest_4_Ingore {

    @Test
    public void testMethod_1() {
	System.out.println("这里是testMethod_1");
    }
    
    @Test
    @Ignore//跳过该方法
    public void testMethod_2() {
 	System.out.println("这里是testMethod_2");
     }
    
    @Test
    public void testMethod_3() {
 	System.out.println("这里是testMethod_3");
     }
   
    
}

2.3 使用@Ignore注解跳过测试类

  • 使用@Ignore跳过测试类

image.png

整个测试类被跳过 其中的一个测试方法都不执行
@Ignore写在类上

  • 代码
package com.jaylan.example;
import  org.junit.Test;
import  org.junit.Ignore;
//使用@Ignore跳过测试类
@Ignore //跳过整个测试类(该类下包含的所有方法都不执行)
public class ExampleTest_4_Ingore2 {

    @Test
    public void testMethod_1() {
	System.out.println("这里是testMethod_1");
    }
    
    @Test
    public void testMethod_2() {
 	System.out.println("这里是testMethod_2");
     }
    
    @Test
    public void testMethod_3() {
 	System.out.println("这里是testMethod_3");
     } 
}

3.总结

  • @Ignore用来跳过测试类或者测试方法
  • @Ignore的位置不同 其作用不同
    • 写在类上→跳过整个测试类
    • 写在方法上跳过该测试方法
  • 使用@Ignore之前要导入 import org.junit.Ignore;
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值