For JavaScript, there are number of unit test framework, but I chosed JsTestDriver because it can be integrated in build process. Though found number of issues during the journey.
First, the JsTestDriver can be downloaded from js-test-driver at google code. At first tried 1.3.5 but later changed to 1.3.3d as 1.3.5 has bug on relateive path. Refer stackoverflow discussion
JsTestDriver needs a running process in background behaves like a server. In Windows, I wrote a batch script that can be double-clicked to launch the process.
@echo off
set path=%path%;C:\Program Files (x86)\Java\jre7\bin;
set SRC_BUILD=%~dp0
SET JSTESTDRIVER_HOME=%SRC_BUILD%\..\3rdParty\JsTestDriver
cmd /k java.exe -jar %JSTESTDRIVER_HOME%/JsTestDriver-1.3.5.jar --port 4224 --browser "C:\Program Files (x86)\Legpat\Application\chrome.exe"
When double-clicked, it will shows command window and chrome will have a dedicated page. These two should be kept on to function correctly. Though annoying it is, better to find a way to run command line without window, and run the page in background page of chrome. Let me know if you have an idea. Then jsTestDriver.conf like below. It should be located at a base directory which contains UnitTest folder and the folder should have js files to test.
server: http://localhost:4224
load:
- UnitTest/*.js
Then unit test files can be executed as part of MSBUILD process like below.
<?xml version="1.0" encoding="utf-8"?>
<Project
ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTarget="FullBuild">
...
<Target Name="CoreBuild">
<Exec
Command="java -jar %JSTESTDRIVER_HOME%\JsTestDriver-1.3.5.jar --tests all --reset --verbose"
WorkingDirectory="$(SRC_ROOT)\Diagram"
CustomErrorRegularExpression="\[FAILED\]"
/>
</Target>
...
Make sure you put '--reset' option otherwise the you see an error magically disappear but comes right back when source file get saved.
Here is a example of running above build project as a batch script.
When looking closely above window, you can see that there are js-test-driver logging like below. Note that it loaded '/test/UnitTest/Sample.js'. I didn't use 'test' folder but it somehow magically prefixed it. It gave me a lot of confusion at the first time. With combination of '--reset', it dragged me down quite long.
Chrome: Reset
Chrome 50.25.2661.78 Windows loaded /test/UnitTest/Sample.js
...
No comments:
Post a Comment