zoukankan      html  css  js  c++  java
  • 【JUnit】JUnit 忽略测试

    环境

    • JDK 6
    • JUnit 4.13
    • Spring Tool Suite 4.6.2
    • Maven 3.6.3

    @Ignore 注解

    使用 @Ignore 注解可以忽略一个或者一组单元测试,并且可以提供忽略的说明。

    Ignore 示例

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>jiangbo.java.junit</groupId>
        <artifactId>07-java-junit-ignore</artifactId>
        <version>1.0.0</version>
        <description>JUnit Ignore 示例</description>
    
        <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.6</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
    </project>
    

    Caculator

    package jiangbo.java.junit;
    
    public class Caculator {
    
        public static int add(int number1, int number2) {
    
            return number1 + number2;
        }
    
        public static int subtract(int number1, int number2) {
    
            return number1 - number2;
        }
    
        public static int divide(int number1, int number2) {
    
            return number1 / number2;
        }
    }
    

    CaculatorTest

    package jiangbo.java.junit;
    
    import static org.junit.Assert.assertEquals;
    
    import org.junit.Ignore;
    import org.junit.Test;
    
    //@Ignore("这里可以忽略当前类中的所有单元测试")
    public class CaculatorTest {
    
        @Test
        public void testAdd() {
    
            System.out.println("testAdd");
            int number = Caculator.add(1, 1);
            assertEquals(2, number);
        }
    
        @Test
    //    @Ignore
        @Ignore("忽略除法测试,因为会抛异常")
        public void testDivide() {
    
            System.out.println("testDivide");
            Caculator.divide(1, 0);
        }
    }
    

    运行

    通过运行 CaculatorTest 测试类,控制台输出:

    testAdd
    

    并且在 IDE 上可以看到被忽略的测试,和其它的颜色不一样。
    忽略测试

  • 相关阅读:
    数组名与指向数组的指针
    如何实现带可变长参数的函数
    assert()的使用
    参数入栈的顺序以及栈/堆的生长顺序
    指向函数的指针
    各变量入栈顺序
    数组与指针
    C中空指针、NULL与0
    C中为什么不能用==比较字符串?
    在命令行窗口中输入EOF
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13122277.html
Copyright © 2011-2022 走看看