【问题标题】:How to get junit report when testcase is executed from java class?从java类执行测试用例时如何获取junit报告?
【发布时间】:2014-05-13 12:57:51
【问题描述】:

我正在开发一个关键字驱动的测试框架。

我想获取在 java 类中调用的 junit 测试用例的 html 报告。 当在 ant 任务中调用测试用例本身时,我能够获取 html 报告:build、testcase_model、junit。

但我想要这个;构建,DriveTest(调用 testcase_model 的 java 类),junit。不幸的是,这不会生成报告。

我这样称呼我的测试用例:

JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
Result result=junit.run(TestCase_Model.class);

非常感谢大家。 让

/!\ UPDATE /!----------------------------------------- --------

你好。现在我把“@RunWith(SomeRunner.class)”的方式放在一边。在我的 DriveTest 课程中,我这样称呼我的 TestCase_Model : JUnitCore junit = new JUnitCore(); RunListener listener = new RunListener(); junit.addListener(listener); Result result = junit.run(TestCase_Model.class);

我的 DriveTest 目标如下所示: <target name="DriveTest (5)"> <mkdir dir="${junit.output.dir}" /> <junit fork="yes" printsummary="withOutAndErr"> <formatter type="xml" /> <test name="tech.DriveTest" todir="${junit.output.dir}" /> <classpath refid="RWD_Testing.classpath" /> </junit> </target>

我终于得到了报告,但我只有一个全局结果而不是详细结果(testRun1 -> 成功,testrun2 -> 成功)。你觉得哪里不对劲?

【问题讨论】:

  • 您是否有理由不让 DriveTest 成为 JUnit 套件?
  • 感谢您回答 MrPotes。好吧,我试图让 DriveTest 成为一个 junit 测试用例(公共类 DriveTest 实现 junit.framework.Test),但它没有改变任何东西。所以我想测试套件会是一样的吗?
  • 也许您可以提供更多关于 TestCase_Model 实际作用的细节?您使用的是 JUnit 3 还是 4?
  • 当然!我的 TestCase_Model 有一个使用参数迭代的 @Parameters data() 方法。还有一个 @Test run() 方法,它遍历一个方法数组。这些方法是我的关键词,这些方法可以做出断言等。我正在使用 Junit4。
  • 我认为您可能想要定义一个运行器,然后生成测试 - 请参阅下面的答案。

标签: java junit report automated-tests keyword


【解决方案1】:

大多数允许将运行包装在 JUnit 执行中的测试框架使用 JUnit Runner,然后可以使用 @RunWith 注释将其应用于类。

例如,请参阅 Cucumber-JVM 的 Cucumber 运行程序,它运行声明如下的测试中的 .feature 文件:

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
private class RunCukesTest {
}

查找和执行测试的所有逻辑都在运行程序中完成,但因为它仍然是一个运行的 JUnit 测试,所以所有 JUnit 报告生成都按预期工作。

【讨论】:

  • 感谢您非常宝贵的回答 MrPotes。我将尝试使用测试运行器课程,并为您提供最新信息。
  • 实际上我没有得到我应该在我的跑步者课程中放的东西(你的例子中的 Cucumber.class,对吧?)?我的 DriveTest 课程应该使用带有 @RunWith(MyOwnRunnerclass.class) 的跑步者?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2017-05-19
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多