命令如下:
java -cp D:\AutoRun\lib\coretestlib\common\junit-4.3.jar;. org.junit.runner.AliJUnitCore com.TestAA(测试Class名) TestCase1(测试方法名)
攻略如下
由于事先junit 支持 测试粒度到测试类级别,命令如下
java -cp D:\AutoRun\lib\coretestlib\common\junit-4.3.jar;. org.junit.runner.JUnitCore com.TestAA
但是它支持界面模式下的单方法执行,那么肯定有相应的测试类支持,为此找到了TestMethodRunner的类,同时想到改造org.junit.runner.JUnitCore的main的方法,让它去调用TestMethodRuner改造的方法代码分别如下
public Result runMain(String... args) {
System.out.println("JUnit version " + Version.id());
Class<?> cls = null ;
Method mes = null;
List<Failure> missingClasses= new ArrayList<Failure>();
try {
cls = Class.forName(args[0]);
for(int j=0; j< cls.getMethods().length; j++)
{
if(cls.getMethods()[j].getName().endsWith(args[1]))
mes = cls.getMethods()[j];
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("Could not find class: " + args[0]);
Description description= Description.createSuiteDescription(args[0]);
Failure failure= new Failure(description, e1);
missingClasses.add(failure);
}
RunListener listener= new TextListener();
addListener(listener);
Result result= run(cls,mes);
for (Failure each : missingClasses)
result.getFailures().add(each);
return result;
}
public Result run(Class<?> cls, Method mes ) {
Description description;
description = new Description("开始");
TestMethodRunner tmr = null;
try
{
tmr = new TestMethodRunner(cls.newInstance(),
mes, fNotifier, description);
}
catch (Exception e1) {
System.out.println(e1.getMessage());
}
return run(tmr,description);
}
/**
*基本保持原来的函数不变,只是把Runner改成TestMethodRunner
*/
public Result run(TestMethodRunner runner,Description description) {
Result result= new Result();
RunListener listener= result.createListener();
addFirstListener(listener);
try {
fNotifier.fireTestRunStarted(description);
runner.run();
fNotifier.fireTestRunFinished(result);
} finally {
removeListener(listener);
}
return result;
}
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/283241,如需转载请自行联系原作者