Tagging and Filtering JUnit Tests – 标记和过滤JUnit测试

最后修改: 2019年 5月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

It’s very common to execute all our JUnit tests automatically as a part of the CI build using Maven. This, however, is often time-consuming.

作为CI构建的一部分,使用Maven自动执行所有JUnit测试是非常常见的。然而,这往往很耗时。

Therefore, we often want to filter our tests and execute either unit tests or integration tests or both at various stages of the build process.

因此,我们经常希望过滤我们的测试,并在构建过程的不同阶段执行单元测试或集成测试或两者。

In this tutorial, we’ll look at a few filtering techniques for test cases with JUnit 5. In the following sections, we’ll also look at various filtering mechanisms before JUnit 5.

在本教程中,我们将了解使用JUnit 5的测试用例的一些过滤技术。在接下来的章节中,我们还将了解JUnit 5之前的各种过滤机制。

2. JUnit 5 Tags

2.JUnit 5标签

2.1. Annotating JUnit Tests with Tag

2.1.用Tag对JUnit测试进行注解

With JUnit 5 we can filter tests by tagging a subset of them under a unique tag name. For example, suppose we have both unit tests and integration tests implemented using JUnit 5. We can add tags on both sets of test cases:

使用JUnit 5,我们可以通过在一个独特的标签名称下对测试的子集进行过滤。例如,假设我们有使用JUnit 5实现的单元测试和集成测试。我们可以在这两组测试用例上添加标签。

@Test
@Tag("IntegrationTest")
public void testAddEmployeeUsingSimpelJdbcInsert() {
}

@Test
@Tag("UnitTest")
public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() {
}

Henceforth we can execute all tests under a particular tag name separately. We can also tag the class instead of methods. Thereby including all tests in a class under a tag.

因此,我们可以单独执行某个标签名下的所有测试。我们也可以对类而不是方法进行标记。从而在一个标签下包括一个类中的所有测试。

In the next few sections, we’ll see various ways of filtering and executing the tagged JUnit tests.

在接下来的几节中,我们将看到过滤和执行标记的JUnit测试的各种方法。

2.2. Filtering Tags with Test Suite

2.2.用测试套件过滤标签

JUnit 5 allows us to implement test suites through which we can execute tagged test cases:

JUnit 5允许我们实现测试套件,通过它我们可以执行标记的测试案例。

@SelectPackages("com.baeldung.tags")
@IncludeTags("UnitTest")
public class EmployeeDAOUnitTestSuite {
}

Now, if we run this suite, all JUnit tests under the tag UnitTest would be executed. Similarly, we can exclude tests with ExcludeTags annotation.

现在,如果我们运行这个套件,标签UnitTest下的所有JUnit测试都会被执行。同样,我们可以用ExcludeTags注释来排除测试。

2.3. Filtering Tags with Maven Surefire Plugin

2.3.用Maven Surefire插件过滤标签

For filtering JUnit tests within the various phases of the Maven build, we can use the Maven Surefire plugin. The Surefire plugin allows us to include or exclude the tags in the plugin configuration:

为了在Maven构建的各个阶段过滤JUnit测试,我们可以使用Maven Surefire插件。Surefire插件允许我们在插件配置中包含或排除标签

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>UnitTest</groups>
    </configuration>
</plugin>

If we now execute this plugin, it will execute all tests which are tagged as UnitTest. Similarly, we can exclude test cases under a tag name:

如果我们现在执行这个插件,它将执行所有被标记为UnitTest的测试。类似地,我们可以排除标签名称下的测试案例。

<excludedGroups>IntegrationTest</excludedGroups>

2.4. Filtering Tags with an IDE

2.4.用IDE过滤标签

IDEs now allow filtering the JUnit tests by tags. This way we can execute a specific set of tagged tests directly from our IDE.

现在IDE允许通过标签过滤JUnit测试。这样,我们可以直接从IDE中执行一组特定的标记测试。

IntelliJ allows such filtering through a custom Run/Debug Configuration:

IntelliJ允许通过自定义的运行/调试配置进行这种过滤。

JUnit5 Tags in IntelliJ

As shown in this image, we selected the Test Kind as tags and the tag to be executed in the Tag Expression.

如图所示,我们选择了Test Kind作为标签,并在标签表达式中选择了要执行的标签。

JUnit 5 allows various Tag Expressions which can be used to filter the tags. For example, to run everything but the integration tests, we could use !IntegrationTest as the Tag Expression. Or for executing both UnitTest and IntegrationTest, we can use UnitTest | IntegrationTest.

JUnit 5 允许各种标签表达式,可用于过滤标签。例如,为了运行除集成测试之外的所有内容,我们可以使用!IntegrationTest 作为标签表达式。或者为了同时执行UnitTest IntegrationTest,我们可以使用UnitTest | IntegrationTest。

Similarly, Eclipse also allows including or excluding tags in the JUnit Run/Debug configurations:

同样地,Eclipse也允许在JUnit运行/调试配置中包括或排除标签。

JUnit5 Tags in Eclipse

3. JUnit 4 Categories

JUnit 4类别

3.1. Categorizing JUnit Tests

3.1.对JUnit测试进行分类

JUnit 4 allows us to execute a subset of JUnit tests by adding them into different categories. As a result, we can execute the test cases in a particular category while excluding other categories.

JUnit 4允许我们通过将JUnit测试添加到不同的类别中来执行一个子集。因此,我们可以执行某个特定类别的测试用例,而排除其他类别。

We can create as many categories by implementing marker interfaces where the name of the marker interface represents the name of the category. For our example, we’ll implement two categories, UnitTest:

我们可以通过实现标记接口来创建尽可能多的类别,其中标记接口的名称代表类别的名称。对于我们的例子,我们将实现两个类别,UnitTest:

public interface UnitTest {
}

and IntegrationTest:

IntegrationTest:

public interface IntegrationTest {
}

Now, we can categorize our JUnit by annotating it with Category annotation:

现在,我们可以通过用Category注解对我们的JUnit进行分类。

@Test
@Category(IntegrationTest.class)
public void testAddEmployeeUsingSimpelJdbcInsert() {
}

@Test
@Category(UnitTest.class)
public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() {
}

In our example, we put the Category annotation on the test methods. Similarly, we can also add this annotation on the test class, thus adding all tests into one category.

在我们的例子中,我们把Category注解放在测试方法上。同样地,我们也可以在测试类上添加这个注解,从而将所有的测试加入到一个类别中。

3.2. Categories Runner

3.2.类别运行器

In order to execute JUnit tests in a category, we need to implement a test suite class:

为了在一个类别中执行JUnit测试,我们需要实现一个测试套件类。

@RunWith(Categories.class)
@IncludeCategory(UnitTest.class)
@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class)
public class EmployeeDAOUnitTestSuite {
}

This test suite can be executed from an IDE and would execute all JUnit tests under the UnitTest category. Similarly, we can also exclude a category of tests in the suite:

这个测试套件可以从IDE中执行,并且会执行UnitTest类别下的所有JUnit测试。同样地,我们也可以在套件中排除某一类测试。

@RunWith(Categories.class)
@ExcludeCategory(IntegrationTest.class)
@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class)
public class EmployeeDAOUnitTestSuite {
}

3.3. Excluding or Including Categories in Maven

3.3.在Maven中排除或纳入类别

Finally, we can also include or exclude the categories of JUnit tests from the Maven build. Thus, we can execute different categories of JUnit tests in different Maven profiles.

最后,我们还可以在Maven构建中包括或排除JUnit测试的类别。因此,我们可以在不同的Maven配置文件中执行不同类别的JUnit测试。

We’ll use the Maven Surefire plugin for this:

我们将使用Maven Surefire插件来实现这一目标。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>com.baeldung.categories.UnitTest</groups>
    </configuration>
</plugin>

And similarly we can exclude a category from the Maven build:

同样,我们也可以从Maven构建中排除一个类别。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <excludedGroups>com.baeldung.categories.IntegrationTest</excludedGroups>
    </configuration>
</plugin>

This is similar to the example we discussed in the previous section. The only difference is that we replaced the tag name with the fully qualified name of the Category implementation.

这与我们在上一节讨论的例子类似。唯一的区别是,我们用Category 实现的完全合格的名称替换了标签名称。

4. Filtering JUnit Tests with Maven Surefire Plugin

4.用Maven Surefire插件过滤JUnit测试

Both of the approaches we’ve discussed have been implemented with the JUnit library. An implementation agnostic way of filtering test cases is by following a naming convention. For our example, we’ll use UnitTest suffix for unit tests and IntegrationTest for integration tests.

我们讨论的两种方法都是通过JUnit库实现的。筛选测试用例的一个不可知的方法是遵循一个命名惯例。对于我们的例子,我们将对单元测试使用UnitTest后缀,对集成测试使用IntegrationTest。

Now we’ll use the Maven Surefire Plugin for executing either the unit tests or the integrations tests:

现在我们将使用Maven Surefire Plugin来执行单元测试或集成测试。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <excludes>
            **/*IntegrationTest.java
        </excludes>
    </configuration>
</plugin>

The excludes tag here filters all integration tests and executes only the unit tests. Such a configuration would save a considerable amount of build time.

这里的excludestag过滤所有的集成测试,只执行单元测试。这样的配置将节省大量的构建时间。

Furthermore, we can execute the Surefire plugin within various Maven profiles with different exclusions or inclusions.

此外,我们可以在不同的Maven配置文件中执行Surefire插件,并设置不同的排除项或包容项。

Although Surefire works well for filtering, it is recommended to use the Failsafe Plugin for executing integration tests in Maven.

尽管Surefire在过滤方面效果不错,但建议使用Failsafe插件在Maven中执行集成测试。

5. Conclusion

5.总结

In this article, we saw a way to tag and filter test cases with JUnit 5. We used the Tag annotation and also saw various ways for filtering the JUnit tests with a specific tag through the IDE or in the build process using Maven.

在这篇文章中,我们看到了用JUnit 5标记和过滤测试用例的方法。我们使用了Tag 注解,还看到了通过IDE或使用Maven在构建过程中过滤带有特定标签的JUnit测试的各种方法。

We also discussed some of the filtering mechanisms before JUnit 5.

我们还讨论了JUnit 5之前的一些过滤机制。

All examples are available at Github.

所有的例子都可以在Github获得。