【问题标题】:jUnit: How to filter tests found in multiple suitesjUnit:如何过滤在多个套件中找到的测试
【发布时间】:2023-04-06 19:46:01
【问题描述】:

当 jUnit 运行包含多个“子套件”的测试套件时,是否可以过滤在不同套件中找到的测试以使它们仅运行一次?我正在开发一个包含许多数据库集成测试的项目,因此最好只运行一次以使其更快。

考虑一下这个“主要套件”:

@RunWith(Suite.class)
@SuiteClasses
({
    ModuleASuite.class,
    ModuleBSuite.class,
    // More suites...
})
public class MainSuite
{}

还有这些“子套件”:

@RunWith(Suite.class)
@SuiteClasses
({
    TestA1.class,
    TestA2.class,
    //... More tests only related to ModuleA

    SomeTestUsedByManyModules.class
})
public class ModuleASuite
{}

@RunWith(Suite.class)
@SuiteClasses
({
    TestB1.class,
    TestB2.class,
    //... More tests only related to ModuleB

    SomeTestUsedByManyModules.class
})
public class ModuleBSuite
{}

当前,当使用 jUnit 4.4(使用 Eclipse 或 Ant)运行 MainSuite 时,它会运行 SomeTestUsedByManyModules 两次。我怎样才能让它只运行一次?我想过自己制作Runner,但也许有更简单的解决方案?

【问题讨论】:

  • 我以前从未听说过 JUnit 会跟踪它是否运行测试方法——毕竟,也许这就是您的意图?编写自己的跑步者可能是唯一的方法。 (好吧,您可以向每个测试方法添加代码,以将自己注册到某个共享散列中,以便它可以决定是否运行。非常丑陋。)编写测试运行器诚然看起来令人生畏,但看起来比实际困难得多。跨度>

标签: java junit junit4


【解决方案1】:

我不知道如何使用@RunWith(Suite) 来实现,但是另一种方法呢? ClassPathSuite 允许您提供测试名称模式以避免手动将它们全部列出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    相关资源
    最近更新 更多