【问题标题】:Using a JUnit formatter when running within java application在 Java 应用程序中运行时使用 JUnit 格式化程序
【发布时间】:2012-01-09 15:50:09
【问题描述】:

我得到了以下 JUnit 东西在 ant 上运行良好,但到目前为止我还没有找到如何在我的 main() 方法中使用我的格式化程序(当不使用 ant 时)。

这是蚂蚁任务:

<junit printsummary="true" errorProperty="test.failed" failureProperty="test.failed">
    <classpath refid="test.class.path" />

    <!-- use multiple formatters -->
    <formatter type="brief" usefile="false" />
    <!-- console log -->
    <formatter type="xml" classname="x.selenium.output.SeleniumFrameworkFormatter" />
    <!-- file log -->

    <test name="x.selenium.SeleniumFramework" haltonfailure="no" outfile="result" todir="${build.temp.dir}" />
<junit>

在 Java 代码中,我有这个作为替代方案,但这会打印到控制台,而不是使用我的格式化程序来创建一个漂亮的 html 文件:

JUnitCore junit = new JUnitCore();
Result result = junit.run(classes);

有谁知道如何在 Java 代码中做类似的事情? 谢谢!

【问题讨论】:

  • 您使用的是什么版本的 JUnit?

标签: java ant selenium junit


【解决方案1】:

没有一种从纯 JUnit 生成格式化输出的好方法。获得与 ant 相同的输出的最简单方法是运行 ant 本身。您可以直接运行 ant,使用类似:

Process p = Runtme.getRuntime.exec("/path/to/ant target");

或类似的,或者您可以直接调用org.apache.tools.ant.Main 方法,但请注意这会调用System.exit()

另请参阅以下有关从 Java 运行 Ant 的答案:Run ant from JavaInvoke ant from java, then return to java after ant termination

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2013-04-25
    • 2015-03-06
    • 2015-07-16
    相关资源
    最近更新 更多