让开发过程自动化:做好持续测试(3)

发表于:2013-03-04来源:IBM作者:Paul Duvall点击数: 标签:持续测试
在清单 5 中,我定义了一个 JUnit 测试,该测试使用 JUnitPef 来验证 BeerServicePerformanceTest 测试类中的 testLongRunningMethod 测试的执行时间。如果执行该 测试方法

  在清单 5 中,我定义了一个 JUnit 测试,该测试使用 JUnitPef 来验证 BeerServicePerformanceTest 测试类中的 testLongRunningMethod 测试的执行时间。如果执行该测试方法所花的时间多于 1000 毫秒,则测试失败。

  清单 5. 使用 JUnitPerf 的基于性能的测试

package com.beer.business.service;
import com.clarkware.junitperf.*;
import junit.framework.Test;

public class ExampleTimedTest {
  public static Test suite() {        
    long maxElapsedTime = 1000;        
    Test testCase = new BeerServicePerformanceTest("testLongRunningMethod");
    Test timedTest = new TimedTest(testCase, maxElapsedTime);        
    return timedTest;
  }
	
  public static void main(String[] args) {
    junit.textui.TestRunner.run(suite());
  }

}

  使用精确计时作为方法执行时间的标准时要小心;测试的建立和销毁时间包含在整个执行时间中。此外,在早期的性能测试中,精确测定执行速度在更大程度上是一门艺术而不是科学。

  回页首

  使用 Selenium 进行功能测试

  可随意编写所有需要的单元测试和组件测试,但如果要编写一个提供某种类型的用户界面的应用程序(例如 Web 应用程序),则需要测试表示层。以 Web 应用程序为例,需要验证用户场景的导航,另外还要验证场景的功能是正常的。尽管如此,直到最近,这类测试都常被证明是一个负担,需要购买工具来促进开发周期晚期的测试。此外,这些工具几乎不能适合构建过程,即使测试构建得足够早也是如此。

  深入 Selenium

  但近几年来,一些着眼于功能测试的开放源码工具脱颖而出;而且,能轻易地在开发生命周期的早期使用这些工具。工具如 Selenium 和 Watir 都是开放源码的;另外,它们构建时考虑到了开发人员。除了用各种语言(例如 Java 编程和 Python)编程定义 Selenium 测试之外,Selenium 也提供了一种易于学习的表格驱动格式,此格式也能被非技术类型使用。

  Selenium 框架使用 JavaScript 来执行基于 Web 的接受测试,该测试打开一个浏览器并运行表格驱动测试。例如,清单 6 展示了一个表示简单的 Selenium 测试的 HTML 表。该测试的多个步骤打开一个 Web 应用程序,然后使用有效的用户名和密码执行登录。测试结果生成到一个 HTML 表中,在 Selenium 运行完所有的测试后,能查看该表。

  清单 6. 使用 Selenium 的功能测试

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MyTest</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
</thead><tbody>
<tr>
  <td>open</td>
  <td>/beer/</td>
  <td></td>
</tr>
<tr>
  <td>type</td>
  <td>username</td>
  <td>admin</td>
</tr>
<tr>
  <td>type</td>
  <td>password</td>
  <td>password</td>
</tr>
<tr>
  <td>clickAndWait</td>
  <td>//input[@value='Login']</td>
  <td></td>
</tr>
<tr>
  <td>verifyTextPresent</td>
  <td>Logged in as admin</td>
  <td></td>
</tr>
</tbody></table>
</body>
</html>

  使用清单 6 中基于表格的格式,可以定义多个接受测试。也可以将测试分组成套,一次执行一整套测试。

  使用 Ant 驱动 Selenium

  Selenium 的伟大之处在于它是在考虑了 CI 的基础上从头创建的,因为你能在像 Ant 那样的构建工具中运行 Selenium。此外,由于框架设计者的高瞻远瞩,如果任何 Selenium 接受测试失败,您也可以让整个构建失败。例如,清单 7 展示了一个 Ant 任务,该任务使用 Selenium 远程控制服务器在一个 Web 应用程序中执行一系列表格驱动测试:

  清单 7. 使用 Ant 运行 Selenium

<?xml version="1.0" encoding="iso-8859-1"?>
<project name="functional-tests" default="run-selenium-tests" basedir=".">
  <property file="${basedir}/selenium.properties"/>
  <import file="${basedir}/common-environment.xml"/>
  <property name="acceptance.test.lib.dir"
                value="${functional.test.dir}" />
  <property name="firefox" value="*firefox" />
  <property name="base.url"
                value="http://${web.host.name}:${web.port}" />
  <property name="acceptance.test.list.dir"
                value="${functional.test.dir}" />
  <property name="acceptance.test.report.dir"
                value="${functional.test.dir}" />
  <target name="run-selenium-tests">
    <mkdir dir="${reports.dir}" />
    <java jar="${acceptance.test.lib.dir}/selenium-server.jar"
            fork="true">
      <arg line="-htmlSuite "${firefox}""/>
      <arg line=""${base.url}""/>
      <arg line=""${acceptance.test.list.dir}/${test.suite}""/>
      <arg line=""${reports.dir}/index.html""/>
      <arg line="-timeout ${timeout}"/>
    </java>
  </target>
  <target name="stop-server">
    <get taskname="selenium-shutdown" 
	      src="http://${web.host.name}:
		    ${selenium.rc.port}/selenium-server/driver/?cmd=shutDown"
      dest="result.txt" ignoreerrors="true" />
     <echo taskname="selenium-shutdown"
              message="Errors during shutdown are expected" />
  </target>
</project>

原文转自:http://www.ibm.com/developerworks/cn/java/j-ap03137/index.html