简体   繁体   中英

How to create JUNIT Reports using runlistener?

I have been using JUNIT for my testing. I am able to run my testcases but the problem is how do i create JUNIT reports programatically using runlistener( Without using ANT or MAVEN ). I am a bit stuck and would appreciate any guidance for the same.

Listener Implementation:-

JUnitCore junit = new JUnitCore();

junit.addListener(new MyJunitListener());

junit.run(AllEJBJunitTests.class);

How do i proceed with creation of report?

You can generate the HTML report using StringBuffer.

String filePath = "File path where you want to keep your report";
String reportFileName = "myReport.htm";

JUnitCore junit = new JUnitCore();
junit.addListener(new MyJunitListener());
Result result = junit.run(AllEJBJunitTests.class);
StringBuffer myContent = getResultContent(result, size);//Size represents number of AllEJBJunitTests class, suppose if you have 5 EJB classes then size is 5.
writeReportFile(filePath + "/" + reportFileName, myContent);

// This method will give you the HTML content.

private StringBuffer getResultContent(Result result, int numberOfTestFiles)
       {
        int numberOfTest = result.getRunCount();
        int numberOfTestFail = result.getFailureCount();
        int numberOfTestIgnore = result.getIgnoreCount();
        int numberOfTestSuccess = numberOfTest - numberOfTestFail - numberOfTestIgnore;
        int successPercent = (numberOfTest != 0) ? numberOfTestSuccess * 100 / numberOfTest : 0;
        double time = result.getRunTime();
        StringBuffer myContent = new StringBuffer("<h1>Junitee Test Report</h1><h2>Result</h2><table border=\"1\"><tr><th>Test Files</th><th>Tests</th><th>Success</th>");
         if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
           myContent.append("<th>Failure</th><th>Ignore</th>");
         }

         myContent.append("<th>Test Time (seconds)</th></tr><tr");
         if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
           myContent.append(" style=\"color:red\" ");
         }
         myContent.append("><td>");
         myContent.append(numberOfTestFiles);
         myContent.append("</td><td>");
         myContent.append(numberOfTest);
         myContent.append("</td><td>");
         myContent.append(successPercent);
         myContent.append("%</td><td>");
         if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
           myContent.append(numberOfTestFail);
           myContent.append("</td><td>");
           myContent.append(numberOfTestIgnore);
           myContent.append("</td><td>");
         }

         myContent.append(Double.valueOf(time / 1000.0D));
         myContent.append("</td></tr></table>");
         return myContent;
       }

// This will generate the report in the particular directory which you have mentioned.

private void writeReportFile(String fileName,StringBuffer reportContent){
        FileWriter myFileWriter = null;
        try {
            myFileWriter = new FileWriter(fileName);
            myFileWriter.write(reportContent.toString());
        }
        catch (IOException e) {

        }
        finally {
            if (myFileWriter!=null) {
                try {
                    myFileWriter.close();
                }
                catch (IOException e) {

                }
            }
        }
    }

Cheers !!

您可以实现自己的运行侦听器,该侦听器可以编写报告。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM