【问题标题】:Failures are not collected in dynamic created test suite动态创建的测试套件中不收集故障
【发布时间】:2023-04-07 00:52:01
【问题描述】:

我有一个 jar 文件,我从中反映了测试类(junit 测试)。

我创建了 TestSuite 实例并向其中添加了测试。

为了检查我的代码,我尝试只向测试套件添加一个示例测试类。

TestSuite suite = new TestSuite();
suite.addTest(new JUnit4TestAdapter(ExampleTest.class));

然后我调用了 suite.run() 来运行测试:

TestResult result = new TestResult();
suite.run(result);

问题是当测试完成后,结果中的失败列表应该是空的。

Enumeration<TestFailure> failures = result.failures(); // = empty list

如何使用 testSuite 解决故障?如果我使用 JUnitCore.runclasses ,我会得到失败列表,但它不能与 TestSuite 的实例一起使用,因为我将测试类作为输入,所以我必须使用它。

Edit-1:

如果可以创建一个扩展 TestSuite 的类并将类动态添加到套件中,那将非常适合我的需求。
任何建议都会很棒。

Edit-2:

通过网络上的更多搜索,我发现failuretestFailure 之间存在差异。如何使用 testFailure 而不是失败的测试失败?

【问题讨论】:

    标签: java unit-testing junit test-suite test-runner


    【解决方案1】:

    经过更多调试,我发现 TestResult 对象确实捕获了失败,但将它们报告为错误(断言错误)。
    我还发现 TestResult.errors() 返回了一个 TestFailure 的枚举器。每个 TestFailure 都包含有关抛出异常的信息,因此我现在可以通过检查哪些错误是断言错误而哪些不是,来区分错误和失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2018-03-29
      • 2020-03-16
      相关资源
      最近更新 更多