简体   繁体   English

为什么 JUnit 5 Suite 注释 @SelectClasses 和 @IncludeClassNamePatterns 找不到不以“Tests”或“Test”结尾的测试?

[英]Why do JUnit 5 Suite annotations @SelectClasses and @IncludeClassNamePatterns fail to find tests that don't end in “Tests” or “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 5(版本 5.5.2)时,我对套件功能有了一个奇怪的发现:我的套件可以找到并运行以“测试”一词结尾的测试,但找不到不以“测试”结尾的测试测试”(在我的例子中,它们以“Base”结尾)。

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.在 JUnit 4 中,我们使用@Suite.SuiteClasses()注释来查找这些测试,但 JUnit 5 @SelectClasses注释似乎完全错过了这些测试类。 Even using @IncludeClassNamePatterns({"^Com.*Base.*?$"}) fails to detect the tests, which I found strange (the tests I want to run start with "Com").即使使用@IncludeClassNamePatterns({"^Com.*Base.*?$"})也无法检测到测试,我觉得这很奇怪(我想运行的测试以“Com”开头)。 After this, I tried the @Tag() annotation, which didn't work either.在此之后,我尝试了@Tag()注释,它也不起作用。

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.我认为这是因为 Maven Surefire(版本 2.22.2)仅检测以 Test 开头或以 Test、Tests 或 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 was able to run these Base tests when I built this project, but the test suites still failed to find them. Maven 在我构建这个项目时能够运行这些基本测试,但测试套件仍然找不到它们。

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 {
}

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 5 并自行成功运行。

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. 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 eg https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-maven for a minimal pom.xml.有关最小的 pom.xml,请参见例如https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-maven

JUnitPlatform , SelectClasses et al. JUnitPlatformSelectClasses等。 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. 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. JUnit 5 目前对类似于 JUnit 4 的@Suite注释的测试套件没有任何明确支持。

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.我建议您完全摆脱Com_Base_Suite和 go 的命名约定,可以通过 Maven 的<includes>部分进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM