如何在带有@IncludeTags的套件中执行以@Tag注释的JUnit测试? [英] How to get JUnit tests annotated with @Tag to be executed in suite that has @IncludeTags?

查看:79
本文介绍了如何在带有@IncludeTags的套件中执行以@Tag注释的JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

测试类:

@Tag("foo")
class SomeIT
{

   @Test
   public void testSomeStuff()
   {
       ...
   }

}

套房课程:

@RunWith(JUnitPlatform.class)
@IncludeTags({"foo"})
//@SelectPackages("org.foo")
public class SomeITSuite
{
}

我的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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <version.slf4j>1.7.30</version.slf4j>
        <version.junit>5.7.0</version.junit>
        <version.junit.platform.runner>1.7.0</version.junit.platform.runner>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- for testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${version.junit}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${version.junit.platform.runner}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>integration-tests</id>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <configuration>
                            <includes>
                                <include>**/*ITSuite.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>verify</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

这将在Idea和控制台中使用0个测试执行套件(使用mvn clean install -Pintegration-tests).

This executes the suite with 0 tests in both Idea and a console (using mvn clean install -Pintegration-tests).

如果我恢复注释掉的@SelectPackages("org.foo"),它将运行所有测试,无论是否标记了它们.我在这里想念什么?这是一个错误吗?

If I restore the commented out @SelectPackages("org.foo"), it will run all the tests, regardless of whether they're tagged, or not. What am I missing here? Is this a bug?

推荐答案

显然,当JUnit扫描类时,它不能标识以IT结尾的类作为集成测试.我不得不将它们重命名为*ITTest,事情开始起作用.

Apparently, when JUnit scans the classes, it doesn't identify such that end with IT as integration tests. I had to rename them to *ITTest and things started working.

如果有人对更多详细信息感兴趣,可以查看此提示请求.

If anyone's interested in more details, you can have a look at this pull request.

这篇关于如何在带有@IncludeTags的套件中执行以@Tag注释的JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆