【问题标题】:How can I run a (JUnit) TestSuite created by a factory method?如何运行由工厂方法创建的(JUnit)TestSuite?
【发布时间】:2013-09-08 07:10:19
【问题描述】:

我正在寻找自定义实现集合的验证测试,并偶然发现:http://www.gamlor.info/wordpress/2012/09/google-guava-collection-test-suite/

我以前没有使用过 junit(所以我对 junit 完全是个菜鸟)。我将 junit4 添加到我的测试项目中,然后...... 陷入了如何实际运行由 Google Guava Collection Test Suite 创建的测试套件。我从我的测试类运行带注释的测试就好了,但不是来自 guava 的测试套件。

junit 文档说套件是通过使用 Suite 注释对套件类进行注释来创建的,列出了它们应该包含的案例,但显然我不能以这种方式列出动态生成的类。我很乐意创建一个简单的测试并将整个套件作为单个测试运行,只是......我如何让 junit 运行套件实例?

【问题讨论】:

  • 你看过stackoverflow.com/questions/3257080/…吗?我认为您应该能够遵循这些模式来结束对工厂方法的调用以完成您想要的。
  • 具体来说,this answer。
  • @tuckermi 您应该将其发布为答案(可能带有摘要/引用)。
  • 当然。我现在就写点东西。

标签: java junit


【解决方案1】:

如果你有一个返回Test对象的工厂方法(我们称它为TestMaker.foo(),那么你可以编写如下内容来获取Test并将其添加到你自己的TestSuite类中:

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.AllTests;

@RunWith(AllTests.class)
public class TestRunner {
    public static TestSuite suite() {
        TestSuite ts = new TestSuite();

        ts.addTest(TestMaker.foo());

        return ts;
    }
}

现在,当您运行TestRunner 测试时,它还将运行TestMaker.foo() 返回的Test 以及可能直接在TestRunner 中定义的任何其他测试。

有关相关讨论(例如,如何引用测试类而不是传递测试对象),请查看:How do I Dynamically create a Test Suite in JUnit 4?

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2017-08-17
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多