上一章中介绍了TestCase类中成员变量及成员函数,本章介绍TestResult类
先介绍下TestResult类:
成员变量:
protected List<TestFailure> fFailures;
protected List<TestFailure> fErrors;
protected List<TestListener> fListeners;
protected int fRunTests;
private boolean fStop;
fFailures:记录测试失败
fErrors:记录测试错误
fListeners:记录测试监听器
构造函数:
public TestResult()
方法:
新增、删除、拷贝监听器:
public synchronized void addListener(TestListener listener);
public synchronized void removeListener(TestListener listener);
private synchronized List<TestListener> cloneListeners();
新增测试失败、新增测试错误、测试错误个数、:
public synchronized void addError(Test test, Throwable t);
public synchronized void addFailure(Test test, AssertionFailedError t);
public synchronized int errorCount();
测试失败结果、测试错误存储为枚举类型:
public synchronized Enumeration<TestFailure> failures();
public synchronized Enumeration<TestFailure> errors();
运行测试用例:
run函数如何被调用,在前一章节TestCase类中:
TestCase类中有一部份代码如下,即运行测试用例并保存结果至TestResult:
public TestResult run()
{
TestResult result = createResult();
run(result);
return result;
}
public void run(TestResult result)
{
result.run(this);
}
以下即为result.run(this)具体实现代码:
protected void run(TestCase test)
{
startTest(test);
//开始测试,具体实现代码
**************** startTest(test)****************
public void startTest(Test test)
{
int count = test.countTestCases();
synchronized (this) {
this.fRunTests += count;
}
for (TestListener each : cloneListeners())
each.startTest(test);
}
*************** startTest(test)*****************
//以下是匿名内部类,变量P指向一个实现了 Protectable 接口的匿名类的实例
Protectable p = new Protectable() {
public void protect() throws Throwable {
test.runBare();//调用TestCase类中的方法
}
};
runProtected(test, p);
**************runProtected(test, p)具体实现代码***********
public void runProtected(Test test, Protectable p)
{ p.protect(); }
********************************************************
endTest(test);
}