一.需求(小组内接口自动化由junit4+重试[用例]+HttpClient[调接口,获取数据]+多线程[执行用例]+数据库连接池[测试账号]组成,由ant生成junit报告)
最近,小组内有人提出了,是否能单独运行某些失败的cases,因为在eclipse中只能运行一个类的全部cases或某个cases; 不能指定跑单个类某些方法或多个类的某些方法。juint4中有@RunWith(Suite.class) 和 @Suite.SuiteClasses({A.class, B.class}) 聚合类A和B;但是不能指定某些方法。
在Ant执行Junit中有个formatter(结果的类型type:xml
, plain
, brief
or failure
),其中type=failure, collects all failing testXXX()
methods and creates a new TestCase
which delegates only these failing methods. 我尝试了,执行ant ***.xml报错为substring空指针,查看源码FailureRecorder.java; 该文件就是用来创建新的测试类(聚合失败cases),但是用Junit3(继承TestCase),并有bug,不能用:
public static class TestInfos implements Comparable {
private final String className;
private final String methodName;
public TestInfos(Test test) {
this.className = test.getClass().getName();
String _methodName = test.toString();
this.methodName = _methodName.substring(0, _methodName.indexOf(40));
}
...
}
TestInfos带参构造函数TestInfos(Test test)中
_methodName.substring(0, _methodName.indexOf(40)) // ( 的 ASCII码:40
_methodName并没有‘(’导致空指针。
二.方案
使用注解
@SuiteClassesMethods;指定
Class<?>[] className 类名
int[] methodsNumPerClass(); 每个类对应的方法数量
String[] methodsName() 方法名
实现代码如下:
SuiteClassesMethods.java
package com.weibo.failmethods;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotati