好久没画图了,看下这个序列图,还算比较清晰的:

以textui来分析:

Test

顶层接口。TestSuite和TestCase均实现此接口,在具体执行的时候面向此接口编程,弱化类型,实现各自的执行流程。

TestSuite中的run方法:

public void run(TestResult result) {
for (Enumeration e= tests(); e.hasMoreElements(); ) {
if (result.shouldStop() )
break;
Test test= (Test)e.nextElement();
runTest(test, result);
}
} public void runTest(Test test, TestResult result) {
test.run(result);
}

如果当前Test是TestSuite类型的,上面的runTest将会继续调用其上的run方法,遍历其维护的Test集合,如果遍历的是TestCase,将走常规路线,否则依旧。

这样就相当于由树到叶的过程,最终的run在叶节点。

TestRunner

测试运行类。

启动测试用例

通过调用BaseTestRunner的getTest方法来获取TestSuite。

TestSuite

测试集合类

构建用户自定义的测试集合。将一组方法整合在一起来测试,自定义组合。如果用户不定义suite方法来创建Testsuite,框架默认生成一个,包含所有的测试方法(TestCase)。

Method suiteMethod= null;
try {
suiteMethod= testClass.getMethod(SUITE_METHODNAME, new Class[0]);
} catch(Exception e) {
// try to extract a test suite automatically
clearStatus();
return new TestSuite(testClass);
}

如果我们自己来构建就是这样:

public static Test suite(){
TestSuite suite = new TestSuite();
// 自定义类集合
suite.addTestSuite(DemoTest.class);
suite.addTestSuite(DemoTwoTest.class);
return suite;
}

跟踪一下addTestSuite方法:

public void addTestSuite(Class testClass) {
addTest(new TestSuite(testClass));
} public void addTest(Test test) {
fTests.addElement(test);
}

这个过程可以让顶层的TestSuite包含两个子TestSuite,一个是关于DemoTest的,另一个是DemoTwoTest,两者又包含了各自的TestCase。

TestCase

测试用例类

为每个测试方法创建一个TestCase。包含一个最本质的run方法,仅仅持有该方法的名称,利用反射来调用测试方法。

/**
* Runs the test case and collects the results in TestResult.
*/
public void run(TestResult result) {
result.run(this);
}

TestCase的run方法会调用TestResult的run

TestResult

测试结果类

维护测试结果。最基本的测试方法最终都会在这里运行,方便统计结果。

/**
* Runs a TestCase.
*/
protected void run(final TestCase test) {
startTest(test);
Protectable p= new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p); endTest(test);
} /**
* Runs a TestCase.
*/
public void runProtected(final Test test, Protectable p) {
try {
p.protect();
}
catch (AssertionFailedError e) {
addFailure(test, e);
}
catch (ThreadDeath e) { // don't catch ThreadDeath by accident
throw e;
}
catch (Throwable e) {
addError(test, e);
}
}

TestResult的run又反过来调用TestCase的runBare

Junit3.8源码--核心类的更多相关文章

  1. MyBatis源码 核心配置解析 properties元素

    XMLConfigBuilder的parseConfiguration(XNode)方法,用于解析配置文件 XMLConfigBuilder的propertiesElement(XNode)方法,用于 ...

  2. mybatis源码核心代码

    /** * mybatis源码测试类 * @param args * @throws IOException * @see org.apache.ibatis.session.Configuratio ...

  3. zepto源码--核心方法(类数组相关)--学习笔记

    从这篇起,在没有介绍到各类插件之前,后面将陆续介绍zepto对外暴露的核心方法.即$.fn={}里面的所有方法的介绍.会配合zepto的API进行介绍. 其实前面已经介绍了几个,如width,heig ...

  4. 深入源码解析类Route

    微软官网对这个类的说明是:提供用于定义路由及获取路由相关信息的属性和方法.这个说明已经很简要的说明了这个类的作用,下面我们就从源码的角度来看看这个类的内部是如何工作的. public class Ro ...

  5. ABP源码uml类图

    陆陆续续学习ABP框架有一段时间了,阳光铭睿的入门教程和HK Zhang的源码分析文章对我的学习帮助都很大.之所以会花这么大工夫去学习ABP.看ABP的源代码,一是因为本人对于DDD也非常有兴趣,AB ...

  6. Caffe源码-Solver类

    Solver类简介 Net类中实现了网络的前向/反向计算和参数更新,而Solver类中则是对此进行进一步封装,包含可用于逐次训练网络的Step()函数,和用于求解网络的优化解的Solve()函数,同时 ...

  7. Caffe源码-SGDSolver类

    SGDSolver类简介 Solver类用于网络参数的更新,而SGDSolver类实现了优化方法中的随机梯度下降法(stochastic gradient descent),此外还具备缩放.正则化梯度 ...

  8. Caffe源码-Net类(下)

    net.cpp部分源码 // 接着上一篇博客的介绍,此部分为Net类中前向反向计算函数,以及一些与HDF5文件或proto文件相互转换的函数. template <typename Dtype& ...

  9. Caffe源码-Net类(上)

    Net类简介 Net类主要处理各个Layer之间的输入输出数据和参数数据共享等的关系.由于Net类的代码较多,本次主要介绍网络初始化部分的代码.Net类在初始化的时候将各个Layer的输出blob都统 ...

随机推荐

  1. hdu 4408 Minimum Spanning Tree

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  2. 2017 济南综合班 Day 3

    T1  黑化 题意: 求一个字符串是否可能包含另一个字符串 字符串中的?可以匹配任意字母 可能输出 God bless You! 一定不可能 输出 Game Over! 计算fail数组时,fail数 ...

  3. [洛谷P1404] 平均数

    洛谷题目链接:平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来 ...

  4. python3 mysql 时间参数错误

    一.环境 mac OS + python3.6 + pymysql 二.执行 1.语句 select count(user_id) from chezhubangapppp.yfq_user wher ...

  5. Jmeter-7-在命令行中运行Jmeter.

    jmeter -n -t D:\Jmeter_result\Script_baidu.jmx -l D:\Jmeter_result\Script_baidu.txt jmeter -n -t D:\ ...

  6. 在Linux系统里运行shutdown.sh命令关闭Tomcat时出现错误提示

    服务器:linnux 5.5 64位,已安装好 jdk: Tomcat版本:apache-tomcat-7.0.53 操作软件:Xshell 4(Free for Home / School) 刚开始 ...

  7. CentOS7安装Memcached 三步曲

    1.yum 安装 yum clean allyum -y updateyum -y install memcached 2.Memcached 运行 memcached -h //查看考号修改配置vi ...

  8. Item 3 ------单例模式的几种实现方式,及优缺点

    单例模式,是指一个类只有一个唯一的实例,一个类只会被实例化一次.实现这种效果,最佳的方式,编写包含单个元素的枚举类型. 单例模式的最佳实现方式-----创建一个包含单个元素的枚举类 public en ...

  9. dp优化-四边形不等式(模板题:合并石子)

    学习博客:https://blog.csdn.net/noiau/article/details/72514812 看了好久,这里整理一下证明 方程形式:dp(i,j)=min(dp(i,k)+dp( ...

  10. POJ 3276 Face The Right Way (尺取法)

    题目链接 Description Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are f ...