1

我在一个项目中工作,其中测试类是通过 JunitCore.run(testClasses) 而不是通过 Ant 运行的,因为即使没有 ANT 框架我也必须运行该项目(因此出于同样的原因没有 Testng)。但我仍然需要创建与 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格式的标准。

4

2 回答 2

4

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) 最后我们的报告准备好了

在此处输入图像描述

我希望它对你有帮助!

于 2014-10-28T09:30:33.700 回答
0

事实上,我自己以这种方式解决了这个问题:
首先我使用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();
于 2014-10-30T00:31:18.970 回答