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 的模式
- serial模式
应该是一个接一个的跑,跑一个的时候,会block其他的
(run each test case block one after another) - parallel模式
可以同时跑跑很多case - 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中的格式。
- 下面来介绍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
- 创建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>
- 编译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>
- 创建一个test suite configuration file
就是在testsuites目录下,创建对应的xml文件。里面配置好对应的elements的定义。
3.4 整合到build 脚本
添加一个ant 目标。。。后面一堆脚本,暂时看不懂,ant这块也不熟悉。。。尴尬
3.5 跑case咯
- 先去对应的项目文件下
- 召唤 setEnv.cmd。
- 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
如果是UI的test case, eclipse先打开~
配置一些VM argument。如果是后台的case
eclipse 里面的run configurations去配置Junit的argument ,classpath。