Automating Javascript Unit Tests / Specs - Part 1

I’m building an Adobe Air application at the moment, which basically means loads of javascript development.

We’re building it pure test-first, and have kicked off using jsUnit to get us started with something simple, flipping to the browser when we make a change and hitting the ‘run’ button in the jsTest testrunner HTML page.

I’m starting to find this quite unsatisfactory, however.

  • Debugging when a test fails seems to be pretty much impossible using the testrunner page. There is a workaround where you put a line to call the failing test function into the fixture html file, then browse to that file, but that all feels like quite a rigmarole when you’re cooking on TDD gas.
  • All that alt-tabbing is making my thumb sore! I’m only writing scripts, so I should be able to get rapid feedback like you get with ruby / rails’ autotest plug-in. Shouldn’t I?
  • Ultimately, I want to run these tests as part of an automated build process.

It seems I’m not alone in having these goals.

Now jsUnit does have a java server which is built as an Ant task so you can automate the running of tests, so that could solve my automation goal, except we’re a .net shop using NAnt, and I can’t be bothered to get my head around configuring Ant just to run this one task right now.

I also notice from the sourceforge pages that jsUnit hasn’t been touched for a couple of years - version 2.2alpha, the latest release, was uploaded in March 2006.

There are a couple of alternative testing frameworks for javascript that could replace jsTest and make it all feel a bit more sexy.

I has a look at JSSpec tonight, and knocked together a quick ruby script which uses the selenium rubygem to run a JSSpec test:

require ‘rubygems’ require ’selenium’ require ‘webrick’

include WEBrick

def go pathtotests = ‘/Users/matt/Documents/projects/js-autotest/jsspec’ port = 8001 testsurlbase = “http://localhost:#{port}/”

server = createwebserverat(pathtotests, port) manager = startseleniumservice browser = startseleniumbrowserat(testsurlbase)

browser.open(testsurlbase + ‘demo.html’) puts “page title is #{browser.get_title}”

TODO - check that the specs passed!

puts ’stopping browser’ browser.stop

puts ’stopping selenium service’ manager.stop

puts ’stopping web server’ server.stop end

def startseleniumbrowserat(baseurl) puts ’starting selenium browser’ browser = Selenium::SeleniumDriver.new(”localhost”, 4444, “*firefox”, base_url, 15000) browser.start browser end

def startseleniumservice puts ’starting selenium service’ manager = Selenium::ServerManager.new(Selenium::SeleniumServer.new) manager.start manager end

def createwebserverat(path, port) puts “starting web server on port #{port} at pathto_tests #{path}” server = HTTPServer.new( :Port => port, :DocumentRoot => path ) trap(”INT”){ server.shutdown } t = Thread.new { server.start } server end

go

It seems ridiculous to have to write a script that starts a web server, calls a framework that opens a browser that calls the web server that returns a page that runs the harness that runs my tests - surely there must be some shortcuts in there somewhere?!

I’ve considered trying to directly call one of the JavaScript engines from Mozilla, Safari, or the .NET framework but it’s not feeling right, and in any case, most of these test harnesses need use a whole HTML page, not just run javascripts.

I think my next step is to take another look at what Thomas Fuchs has been up to, and also this rails-based effort from Dr Nic. We’re using the prototype.js framework anyway, so this seem like it would be a good fit, and his documentation suggests we should be able to automate it using rake, which should be fun.

Stay tuned.

Last Modified: Saturday, May 10th, 2008 @ 11:30

This entry was posted on Sunday, April 6th, 2008 at 3:39 am and is filed under Agile / Lean Software Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Automating Javascript Unit Tests / Specs - Part 1”

  1. Hey Matt,

    A couple months ago we struggled with the same pain you describe.

    We’ve been working on a JavaScript testing library that solves many of these problems. It’s not officially released yet, but here is a web page describing what its for and why its different.

    http://javascriptmvc.com/learningcenter/test/index.html

    I’d love to hear what you think.

    • Brian

Leave a Reply