許久沒有來博客更新了,最近在弄ajax agile開發的東西,自然就要涉及到對javascript代碼的單元測試,tdd是agile開發之中的核心部分,那么我們如何來對javascript代碼做單元測試呢?
    JsUnit就是一個這樣的滿足我們要求的xUnit Framwork。
    首先,讓我來介紹一下JsUnit,既然是對JavaScript代碼進行測試,那么構建JsUnit的測試方面的核心代碼是由JavaScript所寫的,最核心的代碼包含在一個名為:jsUnitCore.js的文件之中。打開里面該文件查看里面的代碼,我們可以看到絕大部分是對assert的不同方面的實現。JsUnit之中的assert函數沒有junit的多。但是它已經能夠滿足我們對網頁之中的各個業務功能代碼的測試。
    既然是基于xUnit 架構之下實現的一個testing framework那么JsUnit在具體構建一個測試實例的時候有很多特征、特點是與JUnit相同的。
    首先,它里面的判斷方法要以test開頭,測試函數不能夠有輸入參數,沒有返回類型。
    其次,它的測試頁面之中也有setUp(),tearDown()之類的初始化函數。
    還有,它們都是使用具體的assert斷言函數進行,期望值與實際值比較,進而找出正確與否。
    在JsUnit里面有個對javascript開發人員最重要的功能,我們大家都知道開發js代碼是一件非常煩人的事情,沒有良好的開發集成環境,沒有很好的跟蹤措施,等等的一切,讓我們的開發人員在開發js代碼時不能夠很好的開發。而JsUnit向我們提供了類似于java代碼之中的日志功能,想必大家都用過log4j。它的日志功能讓大家調試代碼的時候非常方便,現在js因為JsUnit也具有了這樣的功能,只不過它讓我們設置的日志級別有所減少只有三種(warn,info,debug),不過能夠滿足我們一般的使用需求。
    使用JsUnit的手動測試,大家都能夠輕易學會,而使用ant集成JsUnit進行自動化測試在網上所說的文章還比較少,而且JsUnit的官方網站(www.jsunit.net)不知道什么回事,我一直登錄不了。下面就介紹一下如何使用ant來集成它。
    說到ant自然要使用到build.xml文件,下面將我所用到的build.xml代碼貼出來方便大家。
  1 <?xml version="1.0" encoding="utf-8"?>
  2 
  3 <project name="JsUnit" default="create_distribution" basedir=".">
  4 
  5     <!--
  6      The following are the properties used to configure the JsUnit server. 
You need to provide values for the mandatory properties.
  7      See the documentation at http://www.jsunit.net for more information.
  8      -->
  9 
 10     <property
 11             name="browserFileNames"
 12             value="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
 13             description="browserFileNames is the list of browsers in which to run tests when
StandaloneTest is invoked on this machine. For a JsUnit Server, this is a mandatory property.
For example: 'c:\program files\internet explorer\iexplore.exe,
c:\program files\netscape\netscape7.1\netscp.exe',D:\Program Files\Mozilla Firefox\firefox.exe"

 14             />
 15 
 16     <property
 17             id="closeBrowsersAfterTestRuns"
 18             name="closeBrowsersAfterTestRuns"
 19             value=""
 20             description="closeBrowsersAfterTestRuns determines whether to attempt to
close browsers after test runs. This is not a mandatory property.
The default is true. For example: 'true'"

 21             />
 22 
 23     <property
 24             id="description"
 25             name="description"
 26             value=""
 27             description="description is a human-readable description of a standard or farm server.
This is not a mandatory property. The default is blank.
For example: 'This is our Mac - it's only running Safari right now'"

 28             />
 29 
 30     <property
 31             id="ignoreUnresponsiveRemoteMachines"
 32             name="ignoreUnresponsiveRemoteMachines"
 33             value=""
 34             description="ignoreUnresponsiveRemoteMachines is a property used only by the JsUnit
Farm Server and the distributed_test target.
Its value is whether to ignore a remove machine that does not respond. 
If true, test runs will be green even if one or more remove machines fail to respond;
if false, an unresponsive remove machine results in a failure. 
This is not a mandatory property. 
Its default is false. For example: 'true'"

 35             />
 36 
 37     <property
 38             id="logsDirectory"
 39             name="logsDirectory"
 40             value="logs"
 41             description="logsDirectory is the directory in which the JsUnitStandardServer
stores the XML logs produced from tests run.It can be specified relative to the working directory.
This is not a mandatory property. If not specified, the directory called 'logs' inside resourceBase is assumed.
For example: 'c:\jsunit\java\logs'"

 42             />
 43 
 44     <property
 45             id="port"
 46             name="port"
 47             value=""
 48             description="port is the port on which the JsUnitStandardServer runs.
This is not a mandatory property. If not specified, 8080 is assumed. For exapmle: '8080'"

 49             />
 50 
 51     <property
 52             id="remoteMachineURLs"
 53             name="remoteMachineURLs"
 54             value=""
 55             description="remoteMachineURLs is a property used only by the JsUnit Farm Server
and the distributed_test target. Its value is the list of URLs of remove machines to
which a request to run tests will be sent.
For example: 'http://machine1.company.com:8080,http://localhost:8080,http://192.168.1.200:9090'"

 56             />
 57 
 58     <property
 59             id="resourceBase"
 60             name="resourceBase"
 61             value=""
 62             description="resourceBase is the directory that the JsUnitStandardServer
considers to be its document root. It can be specified relative to the working directory.
This is not a mandatory property. If not specified, the working directory is assumed.
For example: 'c:\jsunit'"

 63             />
 64 
 65     <property
 66             id="timeoutSeconds"
 67             name="timeoutSeconds"
 68             value="20"
 69             description="timeoutSeconds is the number of seconds to wait before timing out a browser during a test run.
This is not a mandatory property. If not specified, 60 is assumed. For example: '60'"

 70             />
 71 
 72     <property
 73             id="url"
 74             name="url"
 75             value="file:///D:/eclipse/workspace/JsUnitAnt/tests/testRunner.html?
testPage=D:/eclipse/workspace/JsUnitAnt/tests/jsUnitTestSuite.html"

 76             description="url is the URL (HTTP or file protocol) to open in the browser.
For a JsUnit Server, this is a mandatory property for a test run if the server is not passed the 'url' parameter.
For example: 'file:///c:/jsunit/testRunner.html?testPage=c:/jsunit/tests/jsUnitTestSuite.html'"

 77             />
 78 
 79     <!--
 80      The remainder of this build file is not intended to be modified by the end user.
 81      Those targets whose name begins with an underscore are not intended to be run directly by the end user.
 82      -->
 83 
 84     <property name="source_core" location="src"/>
 85     <property name="source_server" location="src"/>
 86     <property name="tests_core" location="test"/>
 87     <property name="tests_server" location="test"/>
 88     <property name="bin" location="bin"/>
 89     <property name="lib" location="lib"/>
 90     <property name="testlib" location="testlib"/>
 91     <property name="config" location="config"/>
 92     
 93 
 94     <path id="classpath">
 95         <fileset dir="${lib}">
 96             <include name="*.jar"/>
 97         </fileset>
 98         <fileset dir="${bin}">
 99             <include name="jsunit.jar"/>
100         </fileset>
101         <dirset dir="${config}"/>
102     </path>
103 
104     <target name="JsUnitTest" description="Runs tests on the local machine">
105         <junit showoutput="true" haltonerror="true" haltonfailure="true">
106             <formatter type="xml" usefile="true"/>
107             <classpath refid="classpath"/>
108             <sysproperty key="browserFileNames" value="${browserFileNames}"/>
109             <sysproperty key="description" value="${description}"/>
110             <sysproperty key="closeBrowsersAfterTestRuns" value="${closeBrowsersAfterTestRuns}"/>
111             <sysproperty key="logsDirectory" value="${logsDirectory}"/>
112             <sysproperty key="port" value="${port}"/>
113             <sysproperty key="resourceBase" value="${resourceBase}"/>
114             <sysproperty key="timeoutSeconds" value="${timeoutSeconds}"/>
115             <sysproperty key="url" value="${url}"/>
116             <test name="net.jsunit.StandaloneTest" outfile="${logsDirectory}\log"/>
117         </junit>
118     </target>
119 
120 </project>
121 
大家可以自己研究一下這個文件,http://www.aygfsteel.com/Files/stme/JsUnitAnt.rar
這個文件是我構建的JsUnit測試環境,大家要加入一些jar包,一般的lib包放入到lib文件夾下,測試所用到的jar放到testlib之中,基本上就可以運行了,build.xml之中的一些參數大家應該都看得懂的。好吧,大家有什么問題可以和我多多交流:)