简体   繁体   中英

Junit create report without ant/maven

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()));
}

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) {
    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM