跳过测试方法(测试类)(@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;
相关推荐
寒月霜华8 小时前
JavaWeb后端-JDBC、MyBatis
spring boot·junit·mybatis
2501_9387742919 小时前
Copilot 与测试工具协同?Mastering 课程中单元测试生成与结对编程的结合
测试工具·单元测试·copilot
千里镜宵烛1 天前
Lua-function的常见表现形式
开发语言·junit·lua
Hello World......1 天前
互联网大厂Java面试实战:以Spring Boot与微服务为核心的技术场景剖析
java·spring boot·redis·微服务·junit·kafka·spring security
安冬的码畜日常1 天前
【JUnit实战3_14】第八章:mock 对象模拟技术在细粒度测试中的应用(中):为便于模拟重构原逻辑的两种策略
测试工具·junit·重构·单元测试·多态·junit5·mock 模拟
l1t1 天前
用Lua访问DuckDB数据库
数据库·junit·lua·duckdb
l1t2 天前
Lua与LuaJIT的安装与使用
算法·junit·单元测试·lua·luajit
安冬的码畜日常2 天前
【JUnit实战3_10】第六章:关于测试的质量(上)
测试工具·junit·单元测试·测试覆盖率·1024程序员节·junit5
千里镜宵烛2 天前
Lua-迭代器
开发语言·junit·lua
安冬的码畜日常2 天前
【JUnit实战3_11】第六章:关于测试的质量(下)
junit·单元测试·tdd·1024程序员节·bdd·变异测试