Junit4 使用@Ignore注解

1 概述

在本文中,我们将学习如何在运行测试用例时忽略或禁用测试方法或类。
要忽略JUnit中的测试,可以注释一个方法或删除@Test注解,但是测试运行器不会报告此类测试。另外,您可以在@Test之前或之后添加@Ignore批注。 测试运行程序将报告忽略的测试数量,以及已运行的测试数量和失败的测试数量。 请注意,如果要记录忽略测试的原因,@ Ignore将使用可选参数(字符串)。让我们创建一个简单的maven项目,以演示Junit 4中i gnore测试的用法。

2 创建一个简单的Maven项目

让我们创建一个标准的Maven Web项目结构。
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application

3 项目包装结构

  • src/main/java :Java源代码包和类
  • src/main/resources :非Java资源,例如属性文件和Spring配置
Test
  • src/test/java :测试源代码包和类
  • src/test/resources :非Java资源,例如属性文件和Spring配置
── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── yiidian
    │   │           └── junit
    │   ├── resources
    └── test
        ├── java
        │   └── com
        │       └── yiidian
        │           └── junit
        │               └── TestCaseIgnoreTest.java
        └── resources

4 在pom.xml文件中更新JUnit依赖关系

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
</dependency>

5 创建TestCaseIgnoreTest.java类

package com.yiidian.junit;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Ignore;
import org.junit.Test;
/**
 * 一点教程网: http://www.yiidian.com
 */
public class TestCaseIgnoreTest {

    @Test
    public void test1() {
       assertThat(1 + 1, is(2));
    }
 
    @Ignore
    @Test
    public void test2() {
       assertThat(1 + 2, is(3));
    }

    @Test
    public void test3() {
       assertThat(1 + 3, is(4));
    }

    @Ignore("Test is ignored as a demonstration")
    @Test
    public void test4() {
       assertThat(1 + 4, is(5));
    }

    @Test
    public void test5() {
       assertThat(1 + 5, is(6));
    }
}

输出:

 

6 运行测试

从命令行使用maven运行测试
mvn test

7 输出

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-ignoring-tests-examples 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-ignoring-tests-examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-ignoring-tests-examples ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-ignoring-tests-examples ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.developersguide.junit.TestCaseIgnoreTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.068 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 2

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.302 s
[INFO] Finished at: 2018-06-27T22:26:31+05:30
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------

8 结论

在本指南中,我们学习了如何 通过创建Maven项目来 忽略Junit中的测试。

热门文章

优秀文章