views:

3519

answers:

13

What's the best way to go about running unit tests for Javascript? I've been playing around with Selenium IDE, but it's Firefox-specific. Selenium RC looks... difficult - but is it the best way to run tests in, say, IE6?

Also, is it worth doing? I haven't seen much about automated tests for Javascript applications, but it seems to me that as the language running most of the user experience it should be tested. Do you test your Javascript, just back end code, or neither?

A: 

look at Watin if you use visual studio

Joel Martinez
It won't help you with unit-testing. It's a browser automation tool
Alexander Abramov
+12  A: 

JsUnit is a port of JUnit that allows you to run Javascript tests on multiple browsers. I do a lot more back end testing than front end, but I like to automate any test that I can.

Update: In response to leeand00's comment, there are several alternatives to JsUnit.

Just to name a few. There are more alternatives for different development environments as well.

Bill the Lizard
I like JSUnit too, but it doesn't seem to work with the latest version of Firefox 3. Is there an alternative?
leeand00
@Bill the Lizard Thanks for the alternatives! I did find a way to sort-of make it work with the exception of some browser cache problems...http://stackoverflow.com/questions/619170/getting-firefox-3-to-work-with-jsunit
leeand00
+21  A: 

in the last couple of days i've also been looking for unit testing franeworks for jscript. still haven't decided on one. this one looks interesting

also it was recently launched a firebug extension called FireUnit: FireUnit provides a simple JavaScript API for doing simple test logging and viewing within a new tab of Firebug

vitorsilva
+1  A: 

Selenium RC is mostly used for testing the webpage layout and DOM, although it does provide a getEval() function that you can use to explicitly test javascript expressions.

From the Selenium IDE in Firefox you can export the recorded testcase to the code to the language of your choice, and I think the Selenium doco and tutorial should be enough to get you started with the RC server.

Its an extremely useful tool if you want to also test your page layout and simulate user interactions.

However, if you're purely testing javascript, then JsUnit (as suggested by Bill) is probably a better option.

brass-kazoo
+10  A: 

I already have tried both JsUnit and QUnit and I found QUnit is more easy to use. Some good articles about QUnit:

Kind Regards

marcospereira
A: 

Not quite unit testing, but you may also want to check out lint.

Till
+3  A: 

I've recently had success with Crosscheck http://www.thefrontside.net/crosscheck a JS unit testing framework implemented in Java (on Mozilla Rhino engine).

On the plus side:

  • Crosscheck simulates different Browser DOM implementations (IE, Firefox)
  • Implemented in Java, runs tests in Java, does not require a browser to run tests
  • You can truly do JavaScript TDD, fast feedback loop
  • Maven plugin available on Google Group page http://groups.google.com/group/crosscheck, so you can integrate your tests into automated build
  • works with JS libraries (I used jQuery, Prototype)
  • project owners are pretty responsive on the Google group forum

On the minus side:

  • Crosscheck offers only a simulation of different browsers (but that gets you a long way)
  • Crosscheck is still pre-1.0
  • I had to tweak and rebuild the Maven plugin, it was not binary-compatible with the latest crosscheck release
  • Maven plugin not available in any public repository at this time
Scott Bale
+1  A: 

JSUnit is difficult if you have anything asynchronous going on.

Yahoo's YUI test is easier in that case: http://developer.yahoo.com/yui/yuitest/

+1  A: 

JSSpec is an alternative to JSUnit with a behavior driven development bent to it. It looks a bit more javascript-y to my eye.

slashnick
+7  A: 

Google's jsTestDriver is also nice, especially for testing cross-browser.

In addition to a JUnit-like framework for writing the tests using assert*() methods, it provides an automatic test runner for quickly testing multiple browsers at the same time with just a "run" click from your IDE (Eclipse and IntelliJ are supported).

And there is an adapter for QUnit.

Alexander Klimetschek
A: 

jsunit rocks! it's kinda old-fashined, but stable.

alshur
A: 

I have just been assessing a few, and so far YUI Test been using Screw.Unit have come on top. Screw.Unit is nice because there is a maven plugin for it, and is based on Behavior Driven Development rather than jUnit style assertions. On the minus sites, the maven plugin uses env.js, which is quite buggy (it won't run with Prototype.js for example). YUI Test is a very nice library, but automation seems to be in its early stages - although there seem to be some Selenium integration with YUI Test (there is a link to it but SO won't let me post it)

+4  A: 

If you like Screw.Unit, I recommend you switch over to Jasmine. Jasmine is a BDD framework as well, but removes the dependency on jQuery that allows your tests to be run in a lot of other environments - Rails, PHP, Node.js, Palm webOS, and even iPhone (say PhoneGap).

Also, sadly, Screw.Unit is not being actively maintained - though there are some active forks of the project.

We wrote Jasmine for these (and a few other reasons). Check it out - we'd love your feedback.

dwfrank