Monday, 27 February 2012

Run test via code

You can run your JUnit and JWebUnit tests from your own code. The class org.junit.runner.JUnitCore provides the method runClasses() which allows you to run one or several tests classes. As a return parameter you receive an object of the type org.junit.runner.Result. This object can be used to retrieve information about the tests.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

class RunTests {

    public static void main(String args[]) {
        Result result = JUnitCore.runClasses(TestClass.class);
        for (Failure failure : result.getFailures()) {
            System.out.println("FAIL!: " + failure.toString());
        }
    }
}

http://www.vogella.de/articles/JUnit/article.html

No comments:

Post a Comment