Junit create report without ant/maven

2019-04-16 04:39发布

问题:

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.