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

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

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

2.示例

2.1 必要工作

  • 导入类库

import org.junit.Ignore;

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

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

  • 代码
javascript 复制代码
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跳过测试类

整个测试类被跳过 其中的一个测试方法都不执行

@Ignore写在类上

  • 代码
javascript 复制代码
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;
相关推荐
Glitch40427 分钟前
LLM 数据管线缺陷实录:两个单元测试无法覆盖的运行时问题
单元测试
钱栈up37 分钟前
全量 21 个失败、单跑全绿:泄漏进连接池的 SQL 变量
sql·单元测试·测试
冬奇Lab42 分钟前
LLM 自动化测试系列(03):单元测试生成——TestGen-LLM 与 Qodo Cover
人工智能·单元测试·测试
慧都小项6 天前
Python 跨文件重构怎么验证?PyCharm 用法查找、重构预览与测试流程
python·重构·pycharm·单元测试
2302_805771079 天前
Day13Jmeter数据驱动
功能测试·单元测试·测试用例·需求分析·测试
2302_805771079 天前
Day14Jmeter+数据库的操作
功能测试·单元测试·测试用例·需求分析
清风~徐~来9 天前
【软件测试】allure 测试报告模块
单元测试
2302_8057710710 天前
Day18在接口自动化测试中引入pytest用例管理框架
功能测试·单元测试·测试用例·需求分析·测试
CAE虚拟与现实12 天前
什么是浏览器冒烟测试
单元测试·测试·冒烟测试·回归测试
写后端的胖头鱼13 天前
一文学会单元测试之----Junit
junit·单元测试