Tuesday, 15 February 2011

JavaScript Headless Unit Testing

This is going to be a slightly technical post as it revolves around our CI (Continuous Integration) solution. For the record we use Mercurial - Hudson - JsTestDriver.

JsTestDriver is a project by Google which aims to allow Headless CI by abstracting the test away from the browser and in this respect it works quite well. The process revolves around a server which many browsers can connect to which poll for tests to run. JavaScript is then injected into the browser and the results collated.

This part of JSTD works very well, it is slightly less convenient than a regular JavsScript testing framework (such as QUnit, JSUnit et al) but has the two major advantages of being scriptable and outputting JUnit compliant results. This make it an excellent choice for integrating into central builds and Hudson supports JUnit natively.

Testing locally is usually done against Firefox but the exact same tests are run by Hudson against the major browsers, which are running an several virtual machines on the server. JsTestDriver takes care of the browsers behind the scenes, which is ideal.

JsTestDriver suffered from some major problems though, which have been resolved in the latets release 1.3.0 which was made live 14/02/2011. The major win for me was the abaility to load static test data directly through Jetty (the underlying server that JSTD is built upon). This allowed me to stub my AJAX calls with static JSON, in a regular file, and run tests against the data reliably, which until this release had not been possible. For those of you that are interested my project and config look like this.

Project-dir
  • jsTestDriver.conf
  • data
    • info.json
  • src
    • ajax.js
  • srctest
    • testajax.js
The JsTestDriver Conf file is as follows: -

server: http://localhost:9876
load:
  - src/*.js
test:
 - srctest/*.js
serve:
 - data/*.json


When JsTestDriver is starter the JSON static files are exposed under /test/*, so to access them you would use something like http://localhost:9876/test/data/info.json. This causes me a little pain in that I have to detect if the application is being run by JSTD or not and alter the path to the static test data, but I have a common and consistent approach to Unit Testing this functionality.

JsTestDriver can be obtained here http://code.google.com/p/js-test-driver/ and is well worth a look.

Regards

Andy

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. You're saying about right things. Many thanks you for helpful tips. Every time we need to be wise in planning, so use safeway auto insurance or you may combine another type of auto insurance and compare auto insurance.

    ReplyDelete