Description使用组合模式描写叙述一个測试树。组合模式中全部元素都是Composite对象。

Description有成员变量private final ArrayList<Description>fChildren= newArrayList<Description>(); //无元素

保存其子结点。fChildren非空,所以不论什么子结点都是一个Composite,可是this. getChildren().size()为0的结点,其实就是叶子。

測试树

一颗測试树Description,有诸多Description构成。每个Description包括的数据:

privatefinal ArrayList<Description> fChildren= newArrayList<Description>(); //无元素

privatefinal String fDisplayName;

privatefinal Annotation[] fAnnotations;

叶子结点有:一个被測试的方法(atomic/ a single test),Description类中定义的的两个命名常量EMPTY(名字为"No Tests")和TEST_MECHANISM(名字为"Test mechanism")

一般元素/Composite,重点是fChildren的构造。一个单元測试类,其Description的子结点包含全部@test修饰的方法(不包含@Before等修饰的方法);一个成组測试类的子结点包含几个单元測试类。比如有Unit1、Unit2、Unit3,而SuiteUnit将Unit2、Unit3组成一组。

package units;
import static tool.Print.*;
import org.junit.*;//各种标注 public class Unit1{
public Unit1() { }
@Before public void setUp(){ }
@After public void tearDown(){ }
@Test public void m1(){
pln("Unit1.m1()");
}
@Test @Ignore public void m2(){
pln("Unit1.m2()");
}
@Test public void m3(){
pln("Unit1.m3()");
}
}
package units;
public class Unit2 {
@org.junit.Test
public void test2() {
System.out.println("Unit2.test2()");
}
}
package units;
public class Unit3 {
    @org.junit.Test
    public void testSth() {<span style="white-space:pre"> </span>
        System.out.println("Unit3.testSth()");
    }
}

SuiteUnit 的代码:

package units;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Unit2.class,
Unit3.class,
})
public class SuiteUnit {}

先看一个样例,打印測试 Request.classes(Unit1.class,SuiteUnit.class)时的測试树。

package demo;
import static tool.Print.*;
import units.Unit1;
import units.SuiteUnit;
import org.junit.runner.Description;
import org.junit.runner.Request;
import org.junit.runner.Runner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*測试Description的各种使用方法
* @author yqj2065
*/
public class DescriptionDemo {
public static void tree(){
Request rqst = Request.classes(Unit1.class,SuiteUnit.class);
Runner r=rqst.getRunner();
Description descr = r.getDescription();
String prefix = "";
print(descr,prefix);
pln( "the total number of atomic tests = "+descr.testCount() );//the total number of atomic tests.
}
    public static void print(Description now,String prefix){
pln(prefix+ now.getDisplayName() );
if(now.isSuite()) {
prefix+=" ";
for (Description x : now.getChildren()){
print(x,prefix);
}
}
}
public static void main(String... args) {
tree();
}
}

输出:

null

  units.Unit1

    m1(units.Unit1)

    m2(units.Unit1)

    m3(units.Unit1)

  units.SuiteUnit

    units.Unit2

      test2(units.Unit2)

    units.Unit3

      testSth(units.Unit3)

the total number of atomic tests = 5

此时的測试树有两个子结点:单元測试类units.Unit1(的Description)和成组測试类units.SuiteUnit。单元測试类的子结点都是叶子;而units.SuiteUnit的子结点为包括的单元測试类。

相关方法

String getDisplayName():返回fDisplayName。本描写叙述的用于打印的名字,普通情况下都採用类全名或JUnit的方法字符串如method(所属类的全名)

String getClassName()、String getMethodName():解析方法字符串,获得@Test修饰的方法相关的类全名和方法名;Class<?> getTestClass(),由getClassName()的返回值为參数name调用Class.forName(name);

 

ArrayList<Description> getChildren():返回fChildren。

void addChild(Description description)

isTest()(是否叶子)、isSuite()(是否组合):相互排斥的一对推断

isEmpty()

 

Collection<Annotation> getAnnotations():将fAnnotations数组形式的转换为Collection;本Description所相应元素前使用的标注

<T extends Annotation> T getAnnotation(Class<T> annotationType)。fAnnotations中是否含有annotationType。

 

@Override hashCode()、equals(Object obj)、toString();

 

组合模式中的Operation()的相应物为int testCount()。包括的叶子測试的总数。

构造器

Description有一个私有构造器。禁止客户类直接创建Description。

private Description(final String displayName, Annotation... annotations) {

fDisplayName= displayName;

fAnnotations= annotations;

}

然而,Description的构造,它提供了静态方法获得Description对象。

这些静态方法构造本Description的基本信息,不加入子结点。因而4个静态方法都是调用私有构造器,

public static Description createSuiteDescription(String name, Annotation... annotations)

public static Description createSuiteDescription(Class<?> testClass) 

public static Description createTestDescription(Class<?> clazz, String name, Annotation... annotations) {

return new Description(String.format("%s(%s)", name, clazz.getName()), annotations);

}

public static Description createTestDescription(Class<?

> clazz, String name) {

return createTestDescription(clazz, name, new Annotation[0]);

}

当中String str = String.format("%s(%s)", "m1", "Class1");

str为m1(Class1),JUnit的方法字符串如method(所属类的全名)

DescriptionDemo中測试树是怎样构建的呢?ParentRunner<T>的代码

	@Override
public Description getDescription() {
Description description= Description.createSuiteDescription(getName(),
getRunnerAnnotations());
for (T child : getFilteredChildren())
description.addChild(describeChild(child));
return description;
}

能够通过“调试文件”,跟踪查看。

【JUnit4.10源码分析】3.4 Description与測试树的更多相关文章

  1. 【JUnit4.10源码分析】6.1 排序和过滤

    abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...

  2. 【JUnit4.10源码分析】5 Statement

    假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...

  3. 【JUnit4.10源码分析】5.2 Rule

    标注@Rule TestRule是一个工厂方法模式中的Creator角色--声明工厂方法. package org.junit.rules; import org.junit.runner.Descr ...

  4. JUnit4.12 源码分析之TestClass

    1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...

  5. 10.源码分析---SOFARPC内置链路追踪SOFATRACER是怎么做的?

    SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...

  6. JUnit4.12 源码分析之Statement

    1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...

  7. JUnit4.12 源码分析(二)之TestRule

    1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...

  8. 11.源码分析---SOFARPC数据透传是实现的?

    先把栗子放上,让大家方便测试用: Service端 public static void main(String[] args) { ServerConfig serverConfig = new S ...

  9. 12.源码分析—如何为SOFARPC写一个序列化?

    SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...

随机推荐

  1. jquery选择器用法笔记(第二部分)

    今天继续讲讲jquery选择器的更多用法,希望能给大家带来帮助. 9.$("ul li:eq(3)")  --  列表中的第四个元素(index 从 0 开始) :eq() 选择器 ...

  2. LA 4728 Square ,旋转卡壳法求多边形的直径

    给出一些正方形.让你求这些正方形顶点之间的最大距离的平方. //返回点集直径的平方 int diameter2(vector<Point> & points) { vector&l ...

  3. 如何在Linux上安装服务器管理软件Cockpit

    Cockpit 是一个自由开源的服务器管理软件,使得我们可以通过它好看的 Web 前端界面轻松地管理我们的 GNU/Linux 服务器,非常轻量级,Web 界面也非常简单易用. Cockpit 使得 ...

  4. 【Window OS】”对于目标文件系统,文件XXXXX过大“导致无法进行文件操作的解决方法

    问题原因:这是目标文件系统不支持这么大的文件的操作问题.例如:目标文件系统的格式是FAT32,FAT32最大支持4G,如果你要进行发送或粘贴4G以上的文件就会出现这个问题. 解决办法:把目标文件系统的 ...

  5. mysql存储引擎简析

    一.常见存储引擎特性 Innodb 具有提交.回滚和崩溃恢复能力的事务安全.支持外键.使用mvcc以及行锁来提供事务支持,因此支持高并发.适用于写频繁,并发率高的应用. Myisam 不支持事务和灾难 ...

  6. ifconf和ifreq

    http://blog.csdn.net/jasenwan88/article/details/7763689 用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人 ...

  7. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  8. Linux安装 微信开发者工具(deepin linux ubt)

    一.环境:: deepin linux15.4.1 二.安装过程: 2.1 安装wine sudo apt-get install wine 2.2 安装nwjs-sdk 2.2.1 下载linux版 ...

  9. Hibernate(十一)检索

    一.Hibernate检索策略 二.检索方法 三.get和load比较 get和load的区别:  get不支持延迟加载,而load支持.  当查询特定的数据库中不存在的数据时,get会返回null, ...

  10. html5开放资料

    http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html KineticJS教程(12) 摘要: KineticJS教程(12) 作者 ...