【JUnit4.10源码分析】3.4 Description与測试树
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与測试树的更多相关文章
- 【JUnit4.10源码分析】6.1 排序和过滤
abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...
- 【JUnit4.10源码分析】5 Statement
假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...
- 【JUnit4.10源码分析】5.2 Rule
标注@Rule TestRule是一个工厂方法模式中的Creator角色--声明工厂方法. package org.junit.rules; import org.junit.runner.Descr ...
- JUnit4.12 源码分析之TestClass
1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...
- 10.源码分析---SOFARPC内置链路追踪SOFATRACER是怎么做的?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
- JUnit4.12 源码分析之Statement
1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...
- JUnit4.12 源码分析(二)之TestRule
1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...
- 11.源码分析---SOFARPC数据透传是实现的?
先把栗子放上,让大家方便测试用: Service端 public static void main(String[] args) { ServerConfig serverConfig = new S ...
- 12.源码分析—如何为SOFARPC写一个序列化?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
随机推荐
- Interpreter Expression 解释器模式 MD
解释器模式 简介 Interpreter模式也叫解释器模式,是行为模式之一,它是一种特殊的设计模式,它建立一个解释器,对于特定的计算机程序设计语言,用来解释预先定义的文法. 应用环境: 如果一种特定类 ...
- [asp.net入门]利用ADO.NET处理数据的简单之处
由于项目需要,要往数据库中导入一些历史数据,而这些历史数据都是线下人工记录的,所以有很多不规范的地方,比如:同一个公司的名称在不同的记录中可能相差那么几个字,而且每条数据不是每个字段都是完整的,等等诸 ...
- (转)Overview : Writing Scripts in C# 使用C#书写脚本
Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are: 除 ...
- libFM 简介
原文:https://eachcloudcn.blob.core.chinacloudapi.cn/clips/XOI1W.htm libFM全称为Factorization Machine Libr ...
- [Functional Programming] Build a Linear congruential generator
What we are going to do in this post, is to build a random number generator. As you might know that ...
- Android MVP 构架封装
上一篇我们简单实现了一个MVP的构架,下面我们来做一个简单的封装使其使用更简单方便 源码地址RxMVP分支Tag03 最终实现目录结构如下 BasePresenter 如果每一个Activity都需要 ...
- 图像处理MATLAB源代码
图像反转 I=imread('nickyboom.jpg'); J=double(I); J=-J+(256-1); %图像反转线性变换 H=uint8(J); sub ...
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- vue 项目的开发流程
1.$ node -v (检测node版本,node版本需要在 V4 以上) 2.全局安装vue $ npm install -g vue 3.安装脚手架 $ npm install -g vue-c ...
- red5源代码编译并打包公布
编译环境:ubuntu14.04/JDK7 步骤: 1.svn检出源代码(两种方式) svn co --depth empty https://github.com/Red5/red5-server ...