Junit create report without ant/maven

落花浮王杯 提交于 2019-12-06 14:21:37

问题


Is it possible to create a report from JUnit without Ant or Maven? Because I call the tests with velocitycode, and the velocitycodes calls a method. And that method calls all the tests. So I can get a response from it, the failures/errors/runs etc. But I want to create a report with it.. Or do I need to create html stuff by myself?

I created the methods and testmethods in Java, so I will do everything in Java, except the call, thats in Velocity code.

Velocitycode:

${custom.test}

Java code:

public void getTest(){
junit.textui.TestRunner runner = new junit.textui.TestRunner();
TestResult testresult = Junit.textui.TestRunner.run(runner.getTest(MyTestClass.class.getName()));
}


回答1:


You will need the ant library. But with this code you can create an XML report and use it in other pograms. Such as Jenkins.

public static void getTest(){
    String pathToReports = "C:\\path\\to\\the\\Reports";
    Project project = new Project();

    try {
        new File(pathToReports).mkdir();
        JUnitTask task = new JUnitTask();

        project.setProperty("java.io.tmpdir",pathToReports);
        task.setProject(project);

        FormatterElement.TypeAttribute type = new FormatterElement.TypeAttribute();
        type.setValue("xml");

        FormatterElement formater = new FormatterElement();   
        formater.setType(type);
        task.addFormatter(formater);

        JUnitTest test = new JUnitTest(YOURTEST.class.getName());
        test.setTodir(new File(pathToReports));

        task.addTest(test);         
        task.execute(); 
    } catch (Exception e) {
    }

}



回答2:


Don't think so. But you might be able to use ant as a library instead of a tool, and use the same code that the tool uses to generate these reports.



来源:https://stackoverflow.com/questions/9890530/junit-create-report-without-ant-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!