java - How to create JUNIT Reports using runlistener? -
i have been using junit
testing. able run testcases problem how create junit reports programatically using runlistener(without using ant or maven). bit stuck , appreciate guidance same.
listener implementation:-
junitcore junit = new junitcore(); junit.addlistener(new myjunitlistener()); junit.run(allejbjunittests.class);
how proceed creation of report?
you can generate html study using stringbuffer.
string filepath = "file path want maintain 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 have 5 ejb classes size 5. writereportfile(filepath + "/" + reportfilename, mycontent);
// method give 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>"); homecoming mycontent; }
// generate study in particular directory have mentioned.
private void writereportfile(string filename,stringbuffer reportcontent){ filewriter myfilewriter = null; seek { myfilewriter = new filewriter(filename); myfilewriter.write(reportcontent.tostring()); } grab (ioexception e) { } { if (myfilewriter!=null) { seek { myfilewriter.close(); } grab (ioexception e) { } } } }
cheers !!
java junit junit4 junit3
No comments:
Post a Comment