Recently I needed to create a custom JUnit runner for executing a single method using the command line. This is how I implemented that.
package testpackage;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
public class SingleJUnitTestRunner {
/**
* Custom built junit runner for running single method.
* Usage:
* java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod */ public static void main(String... args) throws ClassNotFoundException { if (args.length == 0){ System.out.println("Usage:"); System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
System.exit(-1);
} String[] classAndMethod = args[0].split("#");
if (classAndMethod .length != 0){ System.out.println("Usage:"); System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
System.exit(-1);
}
System.out.println("======================================"); System.out.println("Test Execution started for " + classAndMethod[1] + " method in class " + classAndMethod[0] + "."); System.out.println("=====================================");
Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]); JUnitCore core = new JUnitCore();
// Use the text listener to get the output from test cases to console
RunListener listener= new TextListener(System.out);
core.addListener(listener);
Result result = core.run(request);
System.out.println("RunCount: " + result.getRunCount());
System.out.println("FailureCount: " + result.getFailureCount());
System.out.println("IgnoreCount: " + result.getIgnoreCount());
System.exit(result.wasSuccessful() ? 0 : -1);
}
}
package testpackage;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
public class SingleJUnitTestRunner {
/**
* Custom built junit runner for running single method.
* Usage:
* java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod */ public static void main(String... args) throws ClassNotFoundException { if (args.length == 0){ System.out.println("Usage:"); System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
System.exit(-1);
} String[] classAndMethod = args[0].split("#");
if (classAndMethod .length != 0){ System.out.println("Usage:"); System.out.println("java -cp ...... testpackage.SingleJUnitTestRunner testpackage.TestClass#testMethod");
System.exit(-1);
}
System.out.println("======================================"); System.out.println("Test Execution started for " + classAndMethod[1] + " method in class " + classAndMethod[0] + "."); System.out.println("=====================================");
Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]); JUnitCore core = new JUnitCore();
// Use the text listener to get the output from test cases to console
RunListener listener= new TextListener(System.out);
core.addListener(listener);
Result result = core.run(request);
System.out.println("RunCount: " + result.getRunCount());
System.out.println("FailureCount: " + result.getFailureCount());
System.out.println("IgnoreCount: " + result.getIgnoreCount());
System.exit(result.wasSuccessful() ? 0 : -1);
}
}
No comments:
Post a Comment