在清單 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