我参与了一个项目,其中测试类通过JunitCore.run(testClasses)而不是通过Ant运行,因为我必须运行项目,即使没有ANT框架(因此没有出于同样的原因)。但我仍然需要创建与JUNIT / ANT相同的html和xml报告。如何在我的情况下生成它们?
现在我发现https://github.com/barrypitman/JUnitXmlFormatter/blob/master/src/main/java/barrypitman/junitXmlFormatter/AntXmlRunListener.java可用于生成xml报告。如何生成类似于junit-noframes.html的html?是否有现有的方法将TESTS-TestSuites.xml转换为junit-noframes.html以及如何?如果没有,如何生成HTML?我甚至没有找到html格式的标准。
答案 0 :(得分:2)
1)编写测试类
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MyTest{
@Test
public void test(){
int i=5;
int j=5;
assertTrue(i==j);
}
@Test
public void test2(){
int i=5;
int j=15;
assertTrue(i!=j);
}
}
2)创建一个扩展RunListner的类:
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
public class MyRunListner extends RunListener{
private int numberOfTestClass;
private int testExecuted;
private int testFailed;
private long begin;
public MyRunListner(int numberOfTestClass){
this.setBegin(System.currentTimeMillis());
this.numberOfTestClass = numberOfTestClass;
}
public void testStarted(Description description) throws Exception{
this.testExecuted += 1;
}
public void testFailure(Failure failure) throws Exception{
this.testFailed += 1;
}
/**
* @return the numberOfTestClass
*/
public int getNumberOfTestClass(){
return numberOfTestClass;
}
/**
* @param numberOfTestClass the numberOfTestClass to set
*/
public void setNumberOfTestClass(int numberOfTestClass){
this.numberOfTestClass = numberOfTestClass;
}
/**
* @return the testExecuted
*/
public int getTestExecuted(){
return testExecuted;
}
/**
* @param testExecuted the testExecuted to set
*/
public void setTestExecuted(int testExecuted){
this.testExecuted = testExecuted;
}
/**
* @return the testFailed
*/
public int getTestFailed(){
return testFailed;
}
/**
* @param testFailed the testFailed to set
*/
public void setTestFailed(int testFailed){
this.testFailed = testFailed;
}
/**
* @return the begin
*/
public long getBegin(){
return begin;
}
/**
* @param begin the begin to set
*/
public void setBegin(long begin){
this.begin = begin;
}
}
3)生成报告。
import java.io.FileWriter;
import java.io.IOException;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
public class JUnitTest{
public static void main(String[] args){
JUnitTest junit = new JUnitTest();
junit.runTest();
}
public void runTest(){
try {
String filePath = "C:/temp";
String reportFileName = "myReport.htm";
Class[] myTestToRunTab = {MyTest.class};
int size = myTestToRunTab.length;
JUnitCore jUnitCore = new JUnitCore();
jUnitCore.addListener(new MyRunListner(myTestToRunTab.length));
Result result = jUnitCore.run(myTestToRunTab);
StringBuffer myContent = getResultContent(result,size);
writeReportFile(filePath+"/"+reportFileName,myContent);
}
catch (Exception e) {
}
}
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>Junit 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;
}
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) {
}
}
}
}
}
4)最后我们的报告准备好了
我希望它可以帮到你!
答案 1 :(得分:1)
事实上我这样就解决了这个问题:
首先我使用https://code.google.com/p/reporting-junit-runner/source/browse/trunk/src/junitrunner/XmlWritingListener.java?spec=svn2&r=2
创建TESTS - * .xml
然后我自己编写以下代码来创建TEST-SUITE.xml和junit-noframes.html。我们的想法是利用ANT的API来创建报告,而无需真正运行测试。到目前为止,解决方案对我有用。
Project project = new Project();
//a fake project feeding to ANT API so that latter not complain
project.setName("dummy");
project.init();
FileSet fs = new FileSet();
fs.setDir(new File(reportToDir));
fs.createInclude().setName("TEST-*.xml");
XMLResultAggregator aggregator = new XMLResultAggregator();
aggregator.setProject(project);
aggregator.addFileSet(fs);
aggregator.setTodir(new File(reportToDir));
//create TESTS-TestSuites.xml
AggregateTransformer transformer = aggregator.createReport();
transformer.setTodir(new File(reportToDir));
Format format = new Format();
format.setValue(AggregateTransformer.NOFRAMES);
transformer.setFormat(format);
//create junit-noframe.html
aggregator.execute();