Sunday, February 14, 2010

JavaScript Unit Testing and Continuous Integration with JsTestDriver

I am currently reworking the JavaScript layer of an application at work. Among several initiatives to improve the maintainability of the app, I suggested to adopt JavaScript unit testing. There are several JavaScript unit testing framework out there. The one that I used before was QUnit, a simple unit test framework developed by the jQuery team and I intended to use it again.

The final goal was to have our tests ran within our continuous integration process, meaning the JS tests would run when a build was scheduled.

My initial approach was to write:

  1. QUnit tests and called the script from a single file.
  2. Generate an automated test with Selenium to read the result section of the QUnit result page
  3. Depending on what was read, fail or pass the test like any other JUnit tests.

Simple enough until a coworker briefly mentioned to me JsTestDriver.

What is JsTestDriver?

JsTestDriver is a JavaScript testing framework supported by Google. It is more complete than a JavaScript Unit Testing library and can provide you with the following benefits:

  • provides JavaScript Testing API similar to JUnit. You can also write tests against the DOM very easily.
  • can be run via the command line: this means it can be integrated to your Continuous Integration process.
  • can output test results in the same XML format as JUnit results tests: this will allow you to produce reports on the last build and run of tests suites.
  • can provide code coverage.
  • has an eclipse plug-in to run the tests. The plugin is very enjoyable to use because you can run your tests against any browser you specify without leaving leaving Eclipse. With QUnit, you would have to open each browser and refresh the page every time you make a code change for each browser you test against.
  • last but not least, it exists adapters for other JS testing framework so that you can run your tests using JsTestDriver even if you used another testing library such as QUnit.

As you can see, JsTestDriver and its plugins offer a much more complete set of tools to write and automate JS testing.

I was somewhat reluctant at switching from QUnit (which I thought was good enough) to JsTestDriver. However I realized QUnit was only a part (but essential nonetheless) of the whole JS testing process.

Where to I get started?

I will not write today a tutorial on using JsTestDriver, maybe later. I will just point out the resources to get a better feel for it.

1. A presentation video: http://www.youtube.com/watch?v=aDKGGZv-T4M

2. The google code website: http://code.google.com/p/js-test-driver/

To break down the jsTestDriver site and I am linking each of the above points (in the what is JsTestDriver section) to its corresponding page for more info.

There are a few other topics such as how to debug (which is the same as you would debug a JS script with Firebug) and the config file (very simple).

I hope this gave you an idea of JsTestDriver. Hopefully I will post an example soon.