首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

一个智能的 Web 界面测试系统 -4 TestNG 在配置自动测试脚本中的作用

一个智能的 Web 界面测试系统 -4 TestNG 在配置自动测试脚本中的作用

TestNG 在配置自动测试脚本中的作用本测试系统还使用 TestNG 工具来辅助配置自动测试。TestNG 是测试 Java 应用程序的框架之一。它通过一些语义注释来传递测试的参数,定义测试脚本的顺序并配置运行时的性能。用户可以通过配置来生成各式测试报告,十分方便。
TestNG 要求将所有要运行的测试用例都记录在一个叫 testng.xml 的文件中(参见以下代码)。然后根据该文件中的测试用例顺序依次执行测试。用户还可以根据需要在测试用例的具体方法中标识 @BeforeClass,@AfterClass 等语义,更加具体得定义测试顺序。
为方便配置测试 Web 应用程序,我们将所使用的浏览器和应用程序所部署的 IP 地址作为参数定义在 testng.xml 中。这些参数由 parameter 定义,通过 @Parameters 传递到函数中(参见上文)。具体的测试脚本由 Classes 元素中的 Class 来定义。
清单 8.testng.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE suite SYSTEM "http://beust.com/TestNG/TestNG-1.0.dtd" >
<suite name="My test suite">
<test name="SVCGUI unit test">
<parameter name="selen-svr-addr" value="localhost"></parameter>
<parameter name="aut-addr" value="http://9.9.9.9:9080"></parameter>
<parameter name="brwsr-path" value="*firefox"></parameter>
<parameter name="cluster-ip" value="1.1.1.1"></parameter>
<parameter name="cluster-username" value="admin"></parameter>
<parameter name="cluster-pwd" value="abc"></parameter>
<classes>
<class name="src.cluster.TestClusterAddPage"/>
<class name="src.cluster.TestClusterDeletePage"/>
<class name="src.user.TestUserListPage"/>
</classes>
</test>
</suite>




  在上文的事例中,定义了一个叫 My test suite 的测试组,其中包括三个测试脚本。用户可以根据需要在同一个文件中定义其他测试组(suite),或者在一个测试组中定义更多的测试脚本集(classes)。
  系统的 Web 管理站点会罗列出该文件中的 class 元素的 name 属性值(参见图 8)。然后让用户自由选择需要的测试用例,保存成一份新的 testng.xml 文件。接着,用户可以选择马上执行自动测试或者保存到下一次系统运行下载流程时执行。
图 8. 测试实例管理界面TestNG 还能够为系统自动生成一份漂亮的测试报告。我们可以在 build.xml 中给出报告的存放地址和形式(包括 html/text 等,参见如下代码)
清单 9.build.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<target name="compile" description="compile the examples" depends="prepare">
<javac   debug="true"
fork="true"
source="1.5"
classpathref="cp"
srcdir="${basedir}/src"
destdir="${basedir}/build/classes"
/>
</target>
<target name="testjar"
description="make a test jar with the property file and the classes we need in it">
<jar destfile="${basedir}/example.jar">
<fileset dir="${basedir}/build/classes" includes="**/*.class"/>
<fileset file="${basedir}/TestNG.xml"/>
</jar>
</target>
<target name="run-task-jar" depends="compile, testjar"
description="run examples using TestNG task with just xml">
<testng classpathref="cp" outputdir="${test.output}" testjar="${basedir}/example.jar"/>
</target>




在以上事例中有三个任务,compile/testjar/run-task-jar。这三个任务中 run-task-jar 是主要的任务,它调用了 TestNG 的语义 <testng>,并给出测试报告的生成目录,以及测试脚本的所组成 jar 包名。
返回列表