19

我刚刚升级了我的解决方案以使用 JUnit5。现在尝试为我的测试创建具有两个标签的标签:@Fast@Slow. 首先,我使用下面的 Maven 条目来配置使用我的默认构建运行哪个测试。这意味着当我执行时,mvn test只会执行我的快速测试。我假设我可以使用命令行覆盖它。但我不知道我会输入什么来运行我的慢速测试......

我假设像....mvn test -Dmaven.IncludeTags=fast,slow这样的东西不起作用

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <properties>
            <includeTags>fast</includeTags>
            <excludeTags>slow</excludeTags>
        </properties>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>
4

3 回答 3

21

你可以这样使用:

<properties>
    <tests>fast</tests>
</properties>

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <tests>fast,slow</tests>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <groups>${tests}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

这样您就可以从mvn -PallTests test所有测试(甚至是mvn -Dtests=fast,slow test)开始。

于 2017-02-23T18:42:44.877 回答
19

使用配置文件是可能的,但它不是强制性的,因为groupsmaven surefire 插件excludedGroups中定义的用户属性分别包含和排除任何 JUnit 5 标记(它也适用于 JUnit 4 和 TestNG 测试过滤机制)。因此,要执行带有or 标记的测试,您可以运行:
slowfast

mvn test -Dgroups=fast,slow

如果要在 Maven 配置文件中定义排除和/或包含标签,则无需声明新属性来传达它们并在 maven surefire 插件中建立它们的关联。只需使用groups和或excludedGroups由 maven surefire 插件定义和期望:

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <groups>fast,slow</groups>
        </properties>
    </profile>
</profiles>
于 2018-08-12T15:26:40.597 回答
2

您可以省略配置文件而只使用属性,这是一种更有弹性的方式。

<properties>
    <tests>fast</tests>
</properties>

<build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <groups>${tests}</groups>
                </configuration>
            </plugin>
        </plugins>
    </build>

然后你可以通过打字来进行快速测试,通过打字来进行mvn test所有测试,mvn test -Dtests=fast | slow或者只通过打字来进行慢速测试mvn test -Dtests=slow。当您有更多测试标签时,您还可以通过键入运行除所选类型之外的所有测试标签mvn test -Dtests="! contract"

于 2021-09-06T07:54:30.707 回答