Creating a Java reporting project — would like to

2019-09-09 02:08发布

问题:

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.

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).

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.

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:

  • Make the right build.xml files
  • Set up JUnit classes/suites/runners so that it runs over and over for a given list of logs
  • Set up ANT so it does output HTML/XML and how to point it to the right style-sheet so it can be opened in IE/Firefox
  • How to make the output file
  • Override features so the reports are custom.
  • What to override for post-processing. (So I can convert the output HTML into a PDF, etc)
  • How to create a standalone executable that will do all this automatically - Perhaps incorporating a little Swing if the user doesn't supply an input manually.
  • How to properly loop through many tests (currently I am just making a main function and doing:

code:

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.");
}
  • I have a feeling that is the "hacky" way of doing it and if I knew how to plug into Ant properly, it will run my tests, generate the XML/HTML and I would have the output desired.

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.

回答1:

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

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

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