JsTestdriver Getting Started
The hello world test project on the JsTestdriver page is no longer where it used to be. Instead, I cloned
git clone http://tddjs.com/code/03-tools-of-the-trade.git
It is the companion website to Christian Johansen's book Javascript Test-Driven JavaScript Development. I found this book very helpful to get me started.
After checkout, you will find a folder jstestdriver in the directory where you have executed git. The conf file in that folder sets up JsTestdriver to use port 4224 and specifies the js files and tests to load:
server: http://localhost:4224
load:
- src/*.js
- test/*.js
Copy the JsTestdriver jar from the JsTestdriver downloads page to the jstestdriver folder. Open a commandline to that folder and start the JsTestdriver process using
java -jar JsTestDriver-1.3.4.b.jar --port 4224
The next step is to attach the browser to JsTestdriver, a process called capturing. Open your browser to
http://localhost:4224
Next, hit one of the capture browser links. The browser shows a page which displays some information about the last test execution and the current status in general.
The capturing process can be automated by passing your browser's location. Instead of firefoxpath and chromepath, provide the paths to the browser executables on your system:
java -jar JsTestDriver.jar --port 4224 --browser firefoxpath,chromepath
Once the browser is attached, you can run the tests using
java -jar JsTestDriver-1.3.4.b.jar --tests all
We will now cause a failing test. Edit the file
jstestdriver\test\strftime_test.js
TestCase("strftimeTest", {
setUp: function () {
this.date = new Date(2009, 9, 2, 22, 14, 45);
},
setUp: function () {
this.date = new Date(2009, 9, 2, 22, 14, 45);
},
// ...
"test %Y should return full year": function () {
var year = Date.formats.Y(this.date);
assertNumber(year);
assertEquals(2009, year);
}
var year = Date.formats.Y(this.date);
assertNumber(year);
assertEquals(2009, year);
}
}
so that it asserts a different year instead of 2009. For this, change the line.
assertEquals(2009)
to a different year, maybe 2010. The code under test is:
Date.formats = {
// ... Formatting methods ...
Y: function (date) {
return date.getFullYear();
}
The resulting output shows the failing test:
Total 5 tests (Passed: 4; Fails: 1; Errors: 0) (2,00 ms)
Firefox 14.0.1 Windows: Run 5 tests (Passed: 4; Fails: 1; Errors 0) (2,00 ms)
strftimeTest.test %Y should return full year failed (0,00 ms): AssertError:
expected 2010 but was 2009
@http://localhost:4224/test/test/strftime_test.js:16
The reason for the failure is that the date has been initialized during the test setup to contain some day in the year 2009, but the test expects 2010, now that we have manipulated the file test\strftime_test.js.