【问题标题】:JUnit: is it possible to create a Test Suite that executes all test of classes that share a naming convention?JUnit:是否可以创建一个测试套件来执行共享命名约定的类的所有测试?
【发布时间】:2019-01-26 08:49:52
【问题描述】:

我知道我可以创建一个 TestSuite 来枚举我想要的所有类,例如:

@RunWith(Suite.class)
@SuiteClasses({SQLServerTests1.class, SQLServerTest2.class, ... })
public class AllSQLServerTests {}

但是,我有将近 100 多个课程,我不想记住在 @SuiteClasses 注释中包含任何新课程。

由于我的类有一个命名约定(例如以“SQLServer”开头),我正在寻找一种方法来做这样的事情:

@RunWith(Suite.class)
@SuiteClasses(prefix="SQLServer")
public class AllSQLServerTests {}

普通的 JUnit 可以吗?使用 spring 或任何其他框架?

【问题讨论】:

  • 是的,您可以通过自定义 Runner 来做到这一点。但是你需要在类路径中找到所有的测试类。

标签: java testing junit frameworks


【解决方案1】:

标记他们

您可以为每个测试或测试类添加许多标签:

@Test
@Tag("red")
@Tag("production")
public void testWithColour() {...} 



@RunWith(JUnitPlatform.class)
@IncludeTags("red & !production")
public class JUnit5Example {
   //...
}

您也可以使用@ExcludeTags,但它不能与@IncludeTags共存

在测试包中全部运行

@RunWith(JUnitPlatform.class)
@SelectPackages("com.acme.megaproduct.slowtests")
public class JUnit5Example {
   //...
}

编写自定义测试运行器

也许以上都不能满足您的需求,在这种情况下,您可以通过编写自己的跑步者来添加自定义过滤。

here for step by step how to do it。 然后你就像这样使用它:

@RunWith(MyCustomRunner.class)
public class CustomTestSuite {
   //...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多