JUnit4.8.2源代码分析-3.1 Description-测试树

重新把org.junit.runner.Description的源代码读了一下,结合成组测试(Suite)了解Description所表示的测试树

Description使用组合模式描述一个测试的信息。所有元素都是Composite对象

例如myTest.units包中有Unit1、Unit2、Unit3,而SuiteUnit将Unit2、Unit3和myTest.param.ParametTestUnit组成一组。

    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);
            }
        }
    }
    

运行tree()的输出为:

null

myTest.units.Unit1

m1(myTest.units.Unit1)

m2(myTest.units.Unit1)

myTest.units.SuiteUnit

myTest.units.Unit2

test2(myTest.units.Unit2)

myTest.units.Unit3

testSth(myTest.units.Unit3)

myTest.param.ParametTestUnit

[0]

testOdd[0](myTest.param.ParametTestUnit)

[1]

testOdd[1](myTest.param.ParametTestUnit)

[2]

testOdd[2](myTest.param.ParametTestUnit)

[3]

testOdd[3](myTest.param.ParametTestUnit)

[4]

testOdd[4](myTest.param.ParametTestUnit)

[5]

testOdd[5](myTest.param.ParametTestUnit)

the total number of atomic tests = 10

测试树的叶子,是this. getChildren().size()为0的结点。isTest()(是否叶子)返回true。Description的两个命名常量EMPTY(名字为"NoTests")和TEST_MECHANISM(名字为"Test mechanism")是特殊的叶子。但它通常表示一个被测试的方法(atomic/ a single test),或一个@test。包含的信息有:

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

privatefinal String fDisplayName;

privatefinal Annotation[] fAnnotations;

测试树的一般元素/Composite,用fChildren保存其子结点。如一个单元测试类的Description,子结点包括所有@test修饰的方法;而一个Suite的子结点包括几个单元测试类的Description,而单元测试类和成组测试类又可以构成更大的Composite。

注意:单元测试类Description的子结点不包括@Before等修饰的方法。

相关方法:

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> TgetAnnotation(Class<T> annotationType)。fAnnotations中是否含有。

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

组合模式中的Operation()的对应物为int testCount(),包含的叶子测试的总数。

Description有一个私有构造器,而提供了静态方法获得Description对象。

Description childlessCopy(),不知道其意图。

时间: 2024-07-30 13:47:43

JUnit4.8.2源代码分析-3.1 Description-测试树的相关文章

JUnit4.8.2源代码分析-2 org.junit.runner.Request和Description

JUnit4.8.2源代码中,最令yqj2065感兴趣的是org.junit.runner.Request,它是几个意思呢? ①封装JUnit的输入 JUnit4作为信息处理单元,它的输入是单元测试类--布满各种JUnit4的RUNTIME标注的类,但由于使用反射机制,JUnit4的输入严格地说是一个或多个(组)单元测试类的Class对象.早期版本的JUnit主要处理一个测试或测试构成的树,在增添了对过滤/ filtering和排序/ sorting支持后,JUnit4加入了这个概念.毕竟按照1

JUnit4.8.2源代码分析-4 RunNotifier与RunListener

JUnit4执行过程中,org.junit.runner.notification. RunListener和RunNotifier运用了观察者模式. 1.观察者 观察者Observer/Listener主要作用是分析各种事件并定义相应的回调接口.例如JDK中MouseListener处理鼠标键相关的5个动作:鼠标键被按下/pressed.释放/released.单击/clicked.光标进入或离开某组件/enters or exits.java.awt.event .MouseListener

JUnit4.8.2源代码分析-1说明

由于yqj2065下载使用的BlueJ集成的是JUnit4.8.2,所以就分析一下JUnit4.8.2的源代码. JUnit是由GOF 之一的Erich Gamma 和 Kent Beck 编写的一个开源的单元测试框架,分析JUnit源代码的主要目的是学习其中对设计模式的运用.JUnit也是一个研究如何应对版本升级和接口变化的案例. 阅读源代码,首先需要知道该框架的设计需求.如果使用过JUnit,单元测试的需求应该比较熟悉.从简单的例子入手,有应用程序(待测试的类)HelloWorld,为了使用

JUnit4.8.2源代码分析-5 Statement

org.junit.runners.model.Statement/语句是对运行JUnit测试组过程中的一个或多个动作的封装.如果说Runner.run()表示运行JUnit测试组的整个过程,则Statement表示其中或大或小的步骤.针对方法的标注如@Test .@Before.@After.@BeforeClass.@AfterClass具有某些执行的顺序,Statement是整个过程的一个步骤结点,而诸多Statement构成的链式结构表达了整个过程.在各种Runner中广泛使用它们. o

JUnit4.8.2源代码分析-3 TestClass 和RunnerBuilder

吃柿子专挑软的捏.JUnit4的核心是org.junit.runner.Runner,它涉及的类型太多,今天看几个简单的类型.看完了而且不准备回头再看的类型,yqj2065会在BlueJ中将它删除.删除如NullBuilder时,将import org.junit.internal.builders.NullBuilder加到本包的它的客户类中(其他包使用的,是BlueJ库中引入的包文件中的类),以保证整个项目可以编译和生成JavaDoc. org.junit.runners.model.Tes

JUnit4.8.2源代码分析-3.2 Computer

本系列文章,记录yqj2065阅读JUnit源代码的过程,很多时候在阅读过程中有许多不理解的地方,例如某某类是干什么的,为什么需要它,为什么不这样设计--等等. org.junit.runner是JUnit最核心的包,其中的Computer/计算机(是这样翻译么),它是个什么意思呢?刚开始读JUnitCore的时候,我就很不明白. package org.junit.runner; import org.junit.runners.Suite; import org.junit.runners.

JUnit4.8.2源代码分析-6.1 排序和过滤

Runner.sort.Request.sortWith和Sorter.apply yqj2065都快被它们搞死了. Sorter.apply().Request.sortWith()和Sortable.sort()三者做一件事情?为什么呢? java.util.Comparator接口是一个策略类,定义了int compare(T o1, T o2)方法.org.junit.runner.manipulation.Sorter implements Comparator<Description

JUnit4.8.2源代码分析-5.1 Statement之复合命令

抽象类Statement作为命令模式的Command,只有一个方法public abstractvoid evaluate() throws Throwable; 作为命令模式的Invoker的各种Runner,将发出各种Statement并以它们表示运行JUnit测试组的整个过程.针对方法的标注如@Test .@Before.@After.@BeforeClass.@AfterClass和各种测试场景,JUnit在org.junit.internal.runners.statements包中定

【JUnit4.10源代码分析】0导航

JUnit是由GOF 之一的Erich Gamma和 Kent Beck 编写的一个开源的单元测试框架,yqj2065分析JUnit源代码的主要目的是 学习其中对设计模式的运用. JUnit也是一个学习Java编程. 学习框架设计 和研究如何应对版本升级和接口变化的案例. NetBeans IDE 7.4 (Build 201310111528) 的测试库为JUnit4.10,因而在前面对JUnit4.8.2源代码分析的基础上,yqj2065将采用较正规的方式介绍JUnit4.10源代码. 10