【问题标题】:JUnit 5 and Test SuitesJUnit 5 和测试套件
【发布时间】:2021-09-21 13:22:29
【问题描述】:

我们有一个纯 JUnit-5 项目,我们想要对我们的单元测试进行分类。

为此,我的理解是我们需要包含 JUnit-4 @RunWith(JUnitPlatform.class)

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectPackages("my.project.domain")
@IncludeTags("long-running-test") 
public interface LongRunningTestSuite {}

在 Intellij-Idea 中,我可以毫无问题地运行所有或仅测试套件,但使用 maven 执行时出现错误:

  • 在 Intellij-Idea 中某些测试用例失败
  • 在控制台上我有一个无限循环

这没有帮助:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skip>false</skip>
        <excludes>
            <exclude>**/*TestSuite.java</exclude>
        </excludes>
        <includes>
            <include>**/*Test.java, **/*IT.java</include>
        </includes>
    </configuration>
</plugin>

如果我只是删除所有 XyzTestSuite.java 类,则 maven 构建成功。我也试过&lt;exclude&gt;my.project.domain.testsuite&lt;/exclude&gt;

排除接缝不起作用。为什么?

【问题讨论】:

    标签: java junit junit5 maven-surefire-plugin


    【解决方案1】:

    JUnitPlatform 已被弃用。

    对应的新特性是JUnit Platform Suite Engine:https://junit.org/junit5/docs/current/user-guide/index.html#junit-platform-suite-engine

    但是,如果只是对测试进行分类,那么简单得多的标签可能就足够了:https://junit.org/junit5/docs/current/user-guide/index.html#writing-tests-tagging-and-filtering

    【讨论】:

    • 我更改了这些链接中提到的依赖项,但仍然无法正常工作:mvn test -Dtest=my.project.domain.MyTestSuite。它说没有执行任何测试。我正在使用标签对我的测试进行分组。
    • 您是否也添加了 Suite 注释?
    • 是的:import org.junit.platform.suite.api.IncludeClassNamePatterns; import org.junit.platform.suite.api.SelectPackages; import org.junit.platform.suite.api.Suite; import org.junit.platform.suite.api.SuiteDisplayName; @Suite @SuiteDisplayName("Integration Tests") @SelectPackages("my.domain") @IncludeClassNamePatterns(".*ITTest") public interface IntegrationTestSuite {}
    • 好的,我不得不将 TestSuits 从接口更改为在 Intellij-Idea 中一切正常的类,但它仍然不适用于 maven。
    • 我意识到这只是一个本地问题。在 Jenkins 上不会出现此错误。
    猜你喜欢
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2018-12-26
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多