aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Test automation for Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Test automation for Javascript" Watch "Test automation for Javascript" New topic
Author

Test automation for Javascript

Rupa Lahiri
Greenhorn

Joined: Aug 05, 2009
Posts: 5
How can testing of Javascript be automated?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

There is a JSUnit library which follows the same XUnit conventions as JUnit does for Java. This allows you to write tests in JavaScript that you can run repeatedly against your JavaScript code to tell it is still working.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
There are heaps of JavaScript testing frameworks out there. The one I am currently recommending is JsTestDriver (http://code.google.com/p/js-test-driver/) which lets you automate many browsers very easily, supports most CI tools and so on. Give it a spin. There is also an introduction on my site: http://cjohansen.no/en/javascript/test_driven_javascript_done_right

For stubbing and mocking, there is also http://cjohansen.no/sinon/ which is test framework agnostic, and works very well with JsTestDriver.
Filipi Zimermann
Greenhorn

Joined: Mar 26, 2008
Posts: 21
Guys, and what about qUnit (http://docs.jquery.com/Qunit) ?
Have you already used it? Recommend? Don't recommend?

I use Jquey extensively. Should this influence in my tool choice?
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
Whether you use jQuery or not (or some other library for that matter) shouldn't matter to your testing tool preference. As long as jQuery is already a part of your project, you are still free to use it in your tests no matter what framework you use.

With that said, QUnit is a decent framework, but I personally find it to be slightly lacking in features. I really like high-level assertions, as I feel they make tests clearer, and failures easier to track down. QUnit only has a few assertions. But, it boils down to preference. QUnit is easy to use and has a nice browser runner, and you can even run the tests on multiple browsers simultaneously via JsTestDriver (which has a QUnit bridge).
Filipi Zimermann
Greenhorn

Joined: Mar 26, 2008
Posts: 21
Nice. Thanks for the answer.

I didn't know JSTestDriver.

I would ask about IDE integration but I'm seing at the JSTestDriver website that it integrates with Eclipse.
I use Aptana, probably will easily integrate there too (I had some problems to integrate some eclipse plugins in Aptana Studio once).

I'll take a deep immersion on JSTestDriver now.
Thanks again!
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
No problem! While I haven't used it in Aptana myself, I know that other people have made it work. Good luck
Gordon Bockus
Greenhorn

Joined: May 13, 2009
Posts: 2
Great info. Is there a way to put JSTestDriver into command line mode where it could run in a non-browser evn? This is a concern for me when builds run on a mix of systems...some with displays available and some headless. Thanks
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
Gordon: Unfortunately, no, at the moment this is not possible. But I'm working on something like this, so hopefully that will be possible sometime soon. I haven't tried, but you might get this working with Rhino and env.js, which allows JavaScript on the command line to act as a browser environment, but I'm not sure if it's flexible enough to actually run this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Test automation for Javascript