本文介绍了创建 Java 报告项目 -- 想使用 JUnit 和 Ant 但不确定如何使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣使用 JUnit 为公司中的某个(非软件)工程师组生成报告.与典型的 JUnit 测试不同,您要确保对象、方法等没有错误,我想使用 JUnit 来确认大型日志文件中的结果是否在范围内并提供相关报告.

I'm interested in generating reports for a certain group of (non-software) engineers in my company using JUnit. Unlike typical JUnit testing where you are ensuring objects, methods, etc are bug free, I want to use JUnit to confirm if results in large log files are within range and give reports on it.

我的背景:有 Java 经验.我熟悉 JUnit 4,我花了最后一两个小时浏览 ANT 文档,我想我对它的工作原理有一个深刻的了解(我只是不知道所有的 XML 命令),并且我虔诚地使用 Eclipse.特别是 Eclipse June(最新).

My background: Experienced with Java. I am familiar with JUnit 4, I have spent the last hour or 2 looking through ANT documentation and I think I have a strong idea how it works (I just don't know all the XML commands), and I use Eclipse religiously. Specifically Eclipse June (latest).

我的目标是创建大量测试用例,以断言日志中的值是否在正确的范围内.然后可以在每个日志文件上一遍又一遍地运行测试类.每次运行后,结果将添加到 HTML 文件中.

My goal is to create a slew of testcases that assert if the values in the logs are within the right range. The test class can then be run over and over on every single log file. After each run, the results will be added to an HTML file.

我知道 Ant 中的 JUnit 报告功能,但我在将拼图的所有部分正确组合在一起时遇到了很多麻烦.我知道我可以很轻松地完成这个项目,但我想以正确的方式做到这一点.

I am aware of the JUnit Report features in Ant but I am having a lot of trouble putting all the pieces of the puzzle together properly. I know I can hack my way through this project quite easily, but I want to do this the right way.

任何人都可以指出我如何做的正确方向:

Can any point me in the right direction in how to:

  • 制作正确的 build.xml 文件
  • 设置 JUnit 类/套件/运行程序,以便它为给定的日志列表一遍又一遍地运行
  • 设置 ANT 使其输出 HTML/XML 以及如何将其指向正确的样式表以便在 IE/Firefox 中打开它
  • 如何制作输出文件
  • 覆盖功能以便自定义报告.
  • 后处理要覆盖的内容.(所以我可以将输出的 HTML 转换为 PDF 等)
  • 如何创建一个独立的可执行文件来自动完成所有这些工作 - 如果用户不手动提供输入,也许可以结合一些 Swing.
  • 如何正确地循环通过许多测试(目前我只是在制作一个主要功能并执行:

代码:

public static void main(String[] args) {
    JUnitCore junit = new JUnitCore();
    RunListener listener = new RunListener();
    junit.addListener(listener);
    Result result = junit.run(GearAndBrakeFaultLogReports.class);
    for (Failure f : result.getFailures()) {
        System.out.println(f.toString());
        System.out.println(f.getDescription());
        System.out.println(f.getTestHeader());
        System.out.println(f.getException());
        System.out.println(f.getMessage());
    }
    System.out.println("Done.");
}

  • 我觉得这是一种hacky"方式,如果我知道如何正确插入 Ant,它就会运行我的测试,生成 XML/HTML,我就会得到所需的输出.
  • 我想随着这个项目的发展我会有更多的问题,但我认为克服最初的困境对我来说非常好!我知道有很多问题,所以任何帮助,我很乐意指出凹凸:) 我希望熟悉这些工具的人可以通过将我指向网站、函数或示例项目的问题来回答所有这些问题做类似的事情.

    I imagine I will have more questions as this project develops, but I think getting over the initial hump will be very good for me! I know there are a lot of questions, so any help and I'll gladly point bump :) My hope is, someone familiar with these tools can answer all these with a matter of pointing me to web sites, functions, or an example project that does a similar thing.

    推荐答案

    用于从 JUnit 测试结果创建 PDF:http://junitpdfreport.sourceforge.net/managedcontent/

    For creating a PDF from JUnit test results:http://junitpdfreport.sourceforge.net/managedcontent/

    作为单元测试框架,JUnit 报告非常特定于软件工程.尽管您可能能够为非单元测试场景定制它,但这需要大量定制.与其他测试框架(例如 TestNG)不同,JUnit 不提供制作您自己的报告生成器的能力.然而,TestNG 可用于运行 JUnit 测试,甚至可以生成 JUnit 报告以及它自己的报告.TestNG 可以运行您的 JUnit 测试,然后通过提供 org.testng.IReporter 接口的自定义实现来创建自定义报告.

    As a unit testing frameword, JUnit reports are very specific to software engineering. Although, you might be able to customize it for a non-unit testing scenario, it will take a lot of customizing. Unlike other testing frameworks (such as TestNG) JUnit doesn't provide the ability to make your own report generator. TestNG, however, can be used to run JUnit tests and even produces a JUnit report in addition to its own reports. TestNG can run your JUnit tests and then create custom reports by providing a custom implementation of the org.testng.IReporter interface.

    http://testng.org/doc/documentation-main.html

    使用 Java 编写报告已有多年,我强烈建议您尝试使用 JasperReports 和 iReport 工具等报告工具.Jasper Reports 可以处理自定义数据源(例如您的日志文件)并生成多种格式的报告,包括 XML、HTML 和 PDF.

    Having written reports in Java for many years now, I would strongly recommend trying out a reporting tool such as JasperReports and the iReport tool. Jasper Reports can process custom data sources such as your log files and produce reports in many formats, including XML, HTML, and PDF.

    http://community.jaspersoft.com/project/ireport-designer

    这篇关于创建 Java 报告项目 -- 想使用 JUnit 和 Ant 但不确定如何使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:34