跳过测试方法(测试类)(@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;
相关推荐
阳光普照世界和平2 小时前
2025年单元测试与软件质量工程领域研究现状与进展深度分析报告
单元测试
fzm52987 小时前
嵌入式软件单元测试中AI自动化与人工检查的协同机制研究:基于专业工具的实证分析
c语言·测试工具·单元测试·自动化
中间件XL1 天前
自动测试(base junit)组件
junit·自动测试
Cherry的跨界思维2 天前
【AI测试全栈:质量模型】4、新AI测试金字塔:从单元到社会的四层测试策略落地指南
人工智能·单元测试·集成测试·ai测试·全栈ai·全栈ai测试·社会测试
布列瑟农的星空2 天前
Playwright使用体验
前端·单元测试
2301_780943842 天前
gmock 和 gtest 的完整示例
单元测试
以己之3 天前
初识测试(详细篇)
单元测试·压力测试·测试
你有麻烦我有钱赚3 天前
[Tessy]函数内子函数被调用了数次,需要返回不同值
单元测试·tessy
卓码软件测评4 天前
第三方软件测试机构【Gatling源码的本地编译构建方法】
测试工具·性能优化·单元测试·测试用例
哈哈~haha4 天前
UI5_Walkthrough_Step 27: Unit Test with QUnit 单元测试QUnit
单元测试·qunit