Home > Uncategorized > Generating JUnit Test Results as HTML Report

Generating JUnit Test Results as HTML Report

Junit is one the most popular unit testing framework around . I am assuming you would have gone through the first tutorial Getting Started with Junit

This tutorial is going to introduce you to a new aspect of junit not known to many of you , Generating Reports. If you use build tools like confluence etc, they generate the html report for you but if want to see the out put as beautiful html output, keep reading below.

This Junit tutorial is structured as follows

  • Introduction
    • About Unit testing tools
    • About JUnit
    • Generating HTML reports in JUnit
  • Three ways of generating HTML reports
    • Way 1 – Simple Redirect
    • Way 2 – Simple Output Stream Redirect
      • Sample JUnit Test Results HTML Report
    • Way 3 – Advanced Output Stream Redirect
  • JUnit Links and References
  • Conclusion

Introduction:

  • About Unit testing
    • Process of software life cycle
    • Should be done while coding
    • Assert the correctness of code by Expected result vs. the runtime result using the unit testing tools.Unit testing tools are used to test the Method/Stored procedure we have written. We know thereturn result of the particular method we wrote and expect the same results from the method.Then we have to assert (method result and expected result) which will help you to match our code and our expectation/requirement.
  • About JUnit
    • It is an instance of the xUnit architecture for unit testing frameworks
    • Plug-in for unit testing in java.
    • Executed in both GUI and Text mode.
    • Easy to generate JUnit test cases and JUnit test suite.
  • Generating reports in JUnit
    • Redirect the console output to the html file (file format will be of date/month ex: 22032010.html).
    • Design report template using Microsoft Office InfoPath 2003, redirect the console output to theXML file, and view the xml file using the designed template.

Three ways of generating HTML reports:

1 Simple Redirect: When running the JUnit java files, redirect the output to the HTML File

Ex:: Java -classpath…… filename > TestResult.html

2 Simple Output Stream Redirect: In JUnit java code redirect the output stream to the html file

Sample code for generating the JUnit test results as HTML reports

//main() method

public static void main(String[] args) {

PrintStream oldoutps = System.out; //get the current output stream

try {

FileOutputStream outfos = new FileOutputStream(“Test Output.htm”); //create //new output stream

PrintStream newoutps = new PrintStream(outfos); //create new output //stream

System.setOut(newoutps); //set the output stream

System.out.println(“Output of Junit Testing”);
System.out.println(“”);
System.out.println(”

");
junit.textui.TestRunner.run(HelloWorldTest.suite());
System.out.println("test finished");
System.out.println("

“);
System.setOut(oldoutps); //for resetting the output stream

} catch (Exception e) {

System.out.println(“some error”);

}

}

//suite method

private static TestSuite suite() {

TestSuite suite = new TestSuite(“Test for Engine. HelloWorldTest.java”);

//$JUnit-BEGIN$

suite.addTestSuite(HelloWorldTest.class);

//$JUnit-END$

return suite;

}

Sample JUnit Test Results HTML Report: test Output.htm

3 Advanced Output Stream Redirect: Design your report templates and generate good-looking and easy analyzing reports.

Steps to generate the repots:

Step1: Design your JUnit Test Report Template using Microsoft Office InfoPath 2003

Step2: Output redirection to form a XML file

Step3: Open XML file using the designed template and Export to Web(.mht)/Excel file

Step4: Open the .mht file and save as html file.

Short note on Microsoft Office InfoPath 2003

Microsoft Office InfoPath 2003 is a new Office System application that streamlines the process of gathering, sharing, and using information by enabling teams and organizations

to easily create and work with rich, dynamic forms. The information gathered can be easily reused throughout organizations and across multiple business processes because

the native file format for InfoPath forms is industry-standard Extensible Markup Language (XML).  XML makes it easier for information to be reused across different documents or systems

for various purposes. For example, the information gathered in an InfoPath form can be stored directly in a database, on a Web server or file share, or on a server running

Windows Share Point Services. From there, the information can be analyzed or easily reused, eliminating the need to retype it.

For more information, see Microsoft Office InfoPath 2003 Help. Microsoft Office InfoPath 2003 will be available in Microsoft Office2003

Design your JUnit Test Report Template using Microsoft Office InfoPath 2003:

File -> Design a form -> Open a New Blank form and start designing your template.

JUnit Test Report

Sample screen shows the way to design the template for the report using controls, layout

JUnit Test Results

Publish/Save the file in .xsn format. Open the .xsn file and save it in xml format.

xml file is the template for the report.

Sample XML Template file

TestReport Template.xml

Output redirection to form a XML file

Sample code to redirect the JUnit output to generate the xml file.

Generate the xml file as output from the JUnit java code.

//main method

public static void main(String[] args) {

PrintStream oldoutps = System.out; //get the current output //stream

try {

FileOutputStream outfos = new FileOutputStream(“TestResults.xml”); //create new //output stream

PrintStream newoutps = new PrintStream(outfos); //create new output stream

System.setOut(newoutps); //set the output stream

System.out.println(“”);

System.out.println(“Name”);

System.out.println(“Allocation”);

System.out.println(“write@kpom.com”);

System.out.println(“12345″);

System.out.println(“”);

junit.textui.TestRunner.run(HelloWorldTest.suite());

System.out.println(“”);

System.out.println(“HelloWorldTest.class”);

System.out.println(“com.wsgc.alloc.junittest”);

System.out.println(“2010-03-22″);

System.out.println(“”);

System.setOut(oldoutps); //for resetting the output stream

} catch (Exception e) {

System.out.println(“some error”);

}

}

//suite method

private static TestSuite suite() {

TestSuite suite = new TestSuite(“Test for Engine. HelloWorldTest.java”);

//$JUnit-BEGIN$

suite.addTestSuite(HelloWorldTest.class);

//$JUnit-END$

return suite;

}

Sample XML file  TestResults.xml

Export to Web or Excel File

Open XML file using the designed template and Export to Web(.mht)/Excel file.

When you open the xml file, make sure that the saved path of the xsn file is not changed(because in the xml file the path of the xsn file will be stored).

JUnit Export To Web

Open the .mht file and save as html file.

JUnit Template View

Exporting to web will save the file in .mht format and we need to again open the mht file

and save it as html file.

Conclusion:

Generating the JUnit test outputs as HTML Reports is useful for recording test results, future reference  and presentation of data.

Categories: Uncategorized Tags:
  1. olive
    October 10th, 2013 at 01:16 | #1

    hi
    i have few doubts, could you please clari
    Create Test Suite Class : – how can i get the details of junit test classes executed( pass/fail, test case execution start time, end time etc..) from this suite class ?
    Create Test Runner class : How Can i call/execute this class from Ant Build File or Jenkins server ?
    I am trying to generate a custom html report file for the Junit Test Suite, so for that i want to know above details.

  2. October 6th, 2010 at 13:10 | #2

    Yes Nico. That is another option to get html reports

  3. nico
    October 6th, 2010 at 10:55 | #3

    Why bother so much when you can have JUnit test results in HTML format for free? E.g. if you use maven to run tests?

  4. sehjad
    April 21st, 2010 at 06:53 | #4

    HTML report will be generated…run it as java application…u might have run it as junit testcase

  5. fanand
    April 7th, 2010 at 21:37 | #5

    Thanks for trying the sample and for your comment Manish.

    Below code generates the output in the html, which is helpful to view the output in browser locally and remotely. For generating the code as a html report you need to use the next code in the technical article. You need to try the 3rd step – Advanced Output Stream Redirect.

    Please let me know if you need more information.

  6. April 7th, 2010 at 08:01 | #6

    Manish
    We will request the guru to look the code. Thanks for feedback.

  7. Manish K Chauhan
    April 7th, 2010 at 01:49 | #7

    Following code below is not generating the html report

    package com.samsung.unittest.testsuite;

    import java.io.FileOutputStream;
    import java.io.PrintStream;

    import com.samsung.unittest.MyClassTest;

    import junit.framework.Test;
    import junit.framework.TestSuite;

    import junit.framework.Test;
    import junit.framework.TestSuite;

    public class AllTests {

    public static void main(String a[])
    {
    PrintStream oldoutps = System.out; //get the current output stream
    try {

    FileOutputStream outfos = new FileOutputStream(“Output.htm”); //creat new output stream

    PrintStream newoutps = new PrintStream(outfos); //create new output //stream

    System.setOut(newoutps); //set the output stream

    System.out.println(“Output of Junit Testing”);
    junit.textui.TestRunner.run(AllTests.suite());
    System.out.println(“test finished”);
    System.setOut(oldoutps); //for resetting the output stream

    } catch (Exception e) {

    System.out.println(e);

    }
    }

    public static Test suite() {
    TestSuite suite = new TestSuite(“Test for JunitTestSuite”);
    //$JUnit-BEGIN$
    suite.addTestSuite(MyClassTest.class );
    //$JUnit-END$
    return suite;
    }

    }

    what is wrong here when only junit test case report not generated…

    any idea please

  1. March 26th, 2010 at 22:04 | #1
  2. March 27th, 2010 at 07:58 | #2
  3. October 10th, 2010 at 17:36 | #3
  4. February 22nd, 2013 at 17:17 | #4