跳过测试方法(测试类)(@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;
相关推荐
realhuizhu16 小时前
周五下午5点改完的代码,你敢直接发布上线吗?
自动化测试·单元测试·ai编程·测试驱动开发·代码质量
我一定会有钱1 天前
pytest测试框架基础
python·单元测试·自动化·pytest
宋情写1 天前
单元测试、覆盖率测试-Springboot
spring boot·单元测试·测试覆盖率
柒.梧.2 天前
Maven构建工具全解析
junit·maven
kjl5365662 天前
幂等性的6类核心解决方案
junit
卓码软件测评3 天前
第三方APP软件测试机构:【Gatling如何测试移动应用后端API移动网络特性和用户行为模拟】
网络·测试工具·单元测试·测试用例
晚烛3 天前
Flutter + OpenHarmony 质量保障体系:从单元测试到真机巡检的全链路可靠性工程
flutter·单元测试
旺仔Sec3 天前
2025年安徽省职业院校技能大赛(高职组)软件测试赛项规程及竞赛样题(附评分标准)
功能测试·单元测试·压力测试
記億揺晃着的那天5 天前
MyBatis-Plus 单元测试中 Lambda Mock 的坑与解决
单元测试·log4j·mybatis
CeshirenTester6 天前
Playwright元素定位详解:8种定位策略实战指南
人工智能·功能测试·程序人生·单元测试·自动化