Content with Style

Web Technique

Unit Tests in JavaScript

by Pascal Opitz on May 31 2007, 17:52

I don’t know how many of you guys out there are seriously testing their javascript applications, and how you do approach tesing them.
I, until now, have been doing the good old step by step alert thing …

But my recent reading and the changes in my PHP development, where I’m using SimpleTest more and more, made me shop around for finding something similar for JavaScript.

The one I want to start using from now on to debug my scripts and trace their output, is called jsassertunit

A port of the xUnit framework (know from JUnit or PHPUnit) is JsUnit

Browsing around on the general subject of unit testing, I even came accross a test framework for XSLT. That left me quite surprised, and I wonder how many people really do use things like this. But then again, XSLT is a turing complete, so you can do potentially anything computable with it.

If anyone has suggestions or helpful examples on using unit tests in javascript, please drop a comment.
Also, I’d like to ask you, dear reader, to comment about your general testing practice and approach to debugging, not only JavaScript but everything you code.

Comments

  • Hi,

    Have you heard of Selenium? It’s a functional test suite for javascript which runs in many different browsers.

    http://www.openqa.org/selenium/

    I’ve only had a quick look at it, but it looks like it could be a powerful tool.

    Erin.

    by erin on May 31 2007, 16:28 #

  • Hey Erin, Thanks for that great great hint. Selenium looks like areally really powerful testing tool. Judging by the video you can even to some test by just browsing around and then later execute exactly the same actions again, because they can be called from a test file … very neat!

    by Pascal Opitz on May 31 2007, 17:47 #

  • I’ve been using Selenium recently with some success. I’ve got CruiseControl.rb (running on Linux) setup to run my selenium tests (in IE and Firefox on Windows) whenever we check in too. This is especially handy because when you get lots of selenium tests they can take a while to run. It’s all quite easy to setup, though you’d be forgiven for thinking that from the amount of reading you need to do. If I get round to it, I’ll blog about how to do it.

    With a javascript unit tester don’t you worry that you can only test some of the functionality? How do you test that the right behaviour has been bound to the events you intended?

    xsltunit is great by the way, I used it 4 or 5 years ago when it was at version 0.1 to develop a pretty large xslt project test first, and it made the entire project feasible. It was easier to use once we’d written a stylesheet to convert the XML output into something similar to your typical command line xUnit output (i.e. dots).

    by Graham on June 28 2007, 03:47 #

  • Graham: In fact I played around with jsassertunit for a while, but found a couple of issues with it that just made me step away.

    I think it’s worth adding that the console object of firebug provides an assert function … and also there’s a tool called firetest. Haven’t got the time to try it pout so far, but if I’ll have some JS work coming, then I’ll give that one a try for sure.

    by Pascal Opitz on June 28 2007, 11:22 #