2016.6.15笔记(2)-测试框架

本文介绍了一种基于扩展的Junit3测试框架,该框架采用XML作为驱动测试的手段。文章详细阐述了框架结构、源码组织方式、测试用例配置及运行模式等内容,并提供了如何添加测试用例的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.框架结构

这个项目是基于扩展的Junit3,以xml作为驱动测试的测试框架。可以在xml里面,按照配置要求,写一个单元测试或者功能测试的测试用例,测试框架会解析这个测试用例,然后调用对用的方法,去跑这个测试

2.Source Code Structure

包名实现的功能
common.base这个包里面含有一些基本的测试类,最常用的测试类就是basetestcase.java,这个类可以提供许多常用的验证方法verification methods
common.framwork测试框架的核心类文件,包里面的类文件是用来解析你test suite中的测试配置文件,因为测试用例是xml驱动,里面的elements,attribute的配置都是在test suite configuration file定义的
common.framwork.logic测试框架的核心类文件,包里面的类文件是用来解析你test suite中的测试配置文件,因为测试用例是xml驱动,里面的elements,attribute的配置都是在test suite configuration file定义的
common.framwork.session测试框架的核心类文件,包里面的类文件是用来解析你test suite中的测试配置文件,因为测试用例是xml驱动,里面的elements,attribute的配置都是在test suite configuration file定义的
common.profiling一些特别的测试类,(specific testclasss)
common.sample例子

3.测试类 test classses

可以看到,测试框架利用类base TestCase(在base 的包里面)来集成所有的常用的测试用例。

3.1 支持3种跑case 的模式

  1. serial模式
    应该是一个接一个的跑,跑一个的时候,会block其他的
    (run each test case block one after another)
  2. parallel模式
    可以同时跑跑很多case
  3. randomorder模式
    随机的跑每一个测试用例

3.2 配置test suite configuration file

从上面的可以看到,测试框架提供了一个很强的工作流去控制和比较这些方法,但是如何用他们?(这翻译的我自己都醉了,framework provides strong workflow control and comparison methods)
我们首先应该先写一个test suite configuration file,是一个xml格式的文件。

3.2.1 test suite schema

xml可以定义一个非常宽松的格式,所以需要利用xsd(xml schemas definition)来定义xml中的格式。

  1. 下面来介绍test suite configuration file中的主要元素 major elements
    root element: 这是test suite configuration file的根节点,主要包括以下元素和属性。
    - Atrributes
    - name:定义这个test suite configuration file的名字。
    - haltonerr:布尔型属性,如果是true的话,当这个test suite 跑挂了之后,会中止整个的测试流程,如果为false的话,那么就跳过skip这个错误failure继续跑下面一个test suite。
    loglevel:这是设置debug信息的等级,和log4j一样。
    - children elements
    - testcase:这个元素主要告诉测试框架,在这个test suite 中,有哪些测试用例和方法需要跑的。
    example:

3.3 怎么添加一个test case

  1. 创建case(如果需要)
    比如有时候,Base TestCase里面的东西不能满足你的要求,比如你需要一个flexible 的test case去cover不同的场景(取决于test suite中传递进来的参数) 你就可以写一个自己的test class 去扩展Base Testcase。
package xxx
public class YourTestCase extends BaseTestCase {
  public YourTestCase (String testName) {
    super(testName);
  }

  public void testMethod1() { /* do unit test logic here */ }
  public void testMethod2() {…}
}

然后要到test suite file 里面去配置这个case 的信息:

<testcase name="xxx"
                    testclass="com.xxx.yourmodule.YourTestCase"
                    testmethod="testMethod1"
                    verifyoutput="true"
                    haltonerr="true"
                    retries="0"
                    secondsbetweenretries="10">
             </testcase>
  1. 编译test case 的类
<property name="src.dir" value="${basedir}/src" />
<target name="compile" depends="prepare">
        <javac debug="true" destdir="${build.classes.dir}" classpathref="build.classpath">
            <src path="${src.dir}" />
            <compilerarg line="-version" />
        </javac>
    </target>
  1. 创建一个test suite configuration file
    就是在testsuites目录下,创建对应的xml文件。里面配置好对应的elements的定义。

3.4 整合到build 脚本

添加一个ant 目标。。。后面一堆脚本,暂时看不懂,ant这块也不熟悉。。。尴尬

3.5 跑case咯

  1. 先去对应的项目文件下
  2. 召唤 setEnv.cmd。
  3. Run 起来
    C:\xxx\xxx>ant -f c:\xxx\xxx\modules\admin\test\buil
    d.xml testMetrics-0001 -xxx.config.home=c:\xxx\xxx\configuration\KC_Target-D
    omainConfig -DDEBUG=true

其中,xxx.config.home:这个参数表示,告诉框架,要用哪个silo之类的。
debug:告诉框架,测试结果不要保存在testlink里面。

4. 怎么debug case

  1. 如果是UI的test case, eclipse先打开~
    配置一些VM argument。

  2. 如果是后台的case
    eclipse 里面的run configurations去配置Junit的argument ,classpath。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值