许久没有来博客更新了,最近在弄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.blogjava.net/Files/stme/JsUnitAnt.rar
这个文件是我构建的JsUnit测试环境,大家要加入一些jar包,一般的lib包放入到lib文件夹下,测试所用到的jar放到testlib之中,基本上就可以运行了,build.xml之中的一些参数大家应该都看得懂的。好吧,大家有什么问题可以和我多多交流:)