为什么JUnit 5 Suite批注@SelectClasses和@IncludeClassNamePatterns无法找到未以"Tests"结尾的测试?或“测试"? [英] Why do JUnit 5 Suite annotations @SelectClasses and @IncludeClassNamePatterns fail to find tests that don't end in "Tests" or "Test"?

查看:664
本文介绍了为什么JUnit 5 Suite批注@SelectClasses和@IncludeClassNamePatterns无法找到未以"Tests"结尾的测试?或“测试"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在升级到JUnit 5(版本5.5.2)时,我对套件功能进行了一个奇怪的发现:我的套件可以找到并运行以单词"Test"结尾的测试,但是找不到无法进行测试的测试以测试"结尾(在我的情况下,它们以基准"结尾).

While upgrading to JUnit 5 (version 5.5.2), I have made a strange discovery with the suite functionality: my suites can find and run tests that end with the word "Test" but fail to find tests that don't end in "Test" (in my case, they end in "Base").

在JUnit 4中,我们使用@Suite.SuiteClasses()批注来查找这些测试,但是JUnit 5 @SelectClasses批注似乎完全错过了这些测试类.甚至使用@IncludeClassNamePatterns({"^Com.*Base.*?$"})都无法检测到我发现很奇怪的测试(我要运行的测试以"Com"开头).之后,我尝试了@Tag()批注,该批注也不起作用.

In JUnit 4, we used the @Suite.SuiteClasses() annotation to find these tests, but the JUnit 5 @SelectClasses annotation seems to miss these test classes entirely. Even using @IncludeClassNamePatterns({"^Com.*Base.*?$"}) fails to detect the tests, which I found strange (the tests I want to run start with "Com"). After this, I tried the @Tag() annotation, which didn't work either.

我认为这是因为Maven Surefire(2.22.2版)仅检测以Test开头或以Test,Tests或TestCase结尾的测试类.因此,我尝试包括我的基本测试用例:

I assumed this was because Maven Surefire (version 2.22.2) only detects test classes that start with Test, or end with Test, Tests, or TestCase. So, I tried to include my Base test case:

<includes>
        <include>**/*Base.java</include>
        <include>**/Test*.java</include>
        <include>**/*Test.java</include>
        <include>**/*Tests.java</include>
        <include>**/*TestCase.java</include>
</includes>

在我构建此项目时,Maven能够运行这些基础测试,但是测试套件仍然找不到它们.

Maven was able to run these Base tests when I built this project, but the test suites still failed to find them.

我的代码将类似于以下内容:

My code will look similar to the following:

@RunWith(JUnitPlatform.class)
@SelectClasses({
    Com_TestOne_Base.class,
    Com_TestTwo_Base.class,
    Com_TestThree_Base.class,
    Com_TestFour_Base.class,
    Com_TestFive_Base.class,
    Com_TestSix_Base.class,
})
public class Com_Base_Suite {
}

运行该套件的结果是成功,但实际上没有任何测试在运行.所有这些测试都已更新到JUnit 5,并且可以单独成功运行.

The result of running this suite is a success, but no tests actually run. All these tests have been updated to JUnit 5 and run successfully on their own.

推荐答案

将JUnit 4和5混合使用会导致结果出现问题.MavenSurefire能够在JUnit 5(即JUnit平台)之外运行测试.框-鉴于您在pom中具有正确的依赖关系.参见例如 https://github.com/junit- team/junit5-samples/tree/master/junit5-jupiter-starter-maven 以获得最小的pom.xml.

The problem you're running into results from mixing up JUnit 4 and 5. Maven Surefire is able to run JUnit 5 (aka JUnit platform) tests out of the box - given you have the right dependencies in your pom. See e.g. https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-maven for a minimal pom.xml.

JUnitPlatformSelectClasses等.允许您通过JUnit 4运行JUnit平台测试.您可能只想在构建工具或IDE不自身支持JUnit平台的情况下执行此操作.与JUnit 4的@Suite批注类似,JUnit 5当前没有对测试套件的任何明确支持.

JUnitPlatform, SelectClasses et al. allow you to run JUnit platform tests through JUnit 4. You'd probably only want to do that if your build tool or IDE do not support the JUnit platform themselves. JUnit 5 does currently not have any explicit support for test suites similar to JUnit 4's @Suite annotation.

我建议您完全放弃Com_Base_Suite并使用可以通过Maven的<includes>部分进行配置的命名约定.

I recommend you get rid of Com_Base_Suite alltogether and go with a naming convention that can be configured through Maven's <includes> section.

这篇关于为什么JUnit 5 Suite批注@SelectClasses和@IncludeClassNamePatterns无法找到未以"Tests"结尾的测试?或“测试"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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