Let's JAM!

QUnit And You

As a young developer, I haven't gone through all of the different development fads like many people have. What do I mean by fad? I mean development techniques which have come and gone somewhat quickly. These include agile development, iterative development, and other similar methods. I have, however, been told recently to start testing the code which I create. Most of the work I do is in Javascript. At the time of the discussion (which had several other developers participating as well), I was curious how to do testing in Javascript. Was it even possible? I decided to do a bit of research on my options.

 

After looking around at the different options, I decided I wanted a solution which was written in Javascript, allowing me to test quickly and on a variety of platforms and browsers easily. I also wanted a small amount of set up and for the framework to be simple. Good documentation and current development is a huge bonus as well. After looking at the different options which I had, namely JsUnit, Crosscheck, RhinoUnit, and QUnit, I settled on QUnit. QUnit was made by the folks at jQuery. QUnit is used to test jQuery. It is a very simple, easy to set up and use, and is powerful. To make QUnit functional, all one has to do is have a bit of HTML on a page and include a CSS and Javascript file. Once that happens, the tests are dead simple to create. A simple test may look like:

test("Name of Test!",function(){

   equals(10,5*2);

});

You can see this test in action here.

 

The example sets up a test with a name and then tests to see if 10 and 5 * 2 are equal to each other. If they are, the test passes and the following is displayed:

If it doesn't pass, it will puke and cause red things to happen:

Overall, QUnit has been very nice to use. I even found a bug in my code which I didn't know about thanks to QUnit! I will be incorperating QUnit into many of my projects, testing the Javascript which I write. I highly recommend it.