Does anyone else have a better tool to suggest? What has worked for you?
The visual interface of the testrunner page is nice, allowing you to drill down and see each assert in every test method. Writing tests is fairly easy, and you can run the test code directly on the testRunner page . This allows for easy and visible DOM testing.
QUnit: MIT or GPL (choose) jQUnit: MIT License
Pros - Asynchronous support - Good for DOM testing - Tests always run sequentially in the order they are added to a suite - Debug on test page using firebug - Syntax is similar to JUnit if using JQUnit, but simple to learn if using QUnit Cons - Automation would be difficult to implement
For example, with JsMockito, you have to make mocks and then pass those mocks into your software-under-test (SUT). The SUT has to directly call the mocks. Therefore, you're forced to code the SUT as a constructor or function that takes in all its dependencies as parameters. (Sometimes, that's a fine way to implement it, but not in every case. The tail is wagging the dog if your mocking framework's design forces your implementation approach).
I'm not sure why no one has mentioned JsTestDriver! It has to be the one of the only JS Testing tools that actually work like you'd expect them to if you've used unit testing tools in other languages.
Running tests can be done without touching a browser, you can integrate it with IDE's, you can integrate it with Continuous integration systems... Oh, and it's fast, and can run tests in multiple browsers at the same time.
You can also use other testing frameworks like YUITest with it, making it even better.
YUI Test is the test framework for Yahoo’s User Interface library. It is used by Yahoo to test its own library, and has syntax similar to jUnit.
Like jsUnit, YUI Test comes with its own logging console that can output info, warnings and errors in addition to the results of each test.
YUI also provides the ability to send reports on the results in either JSON or XML format.
YUI Test is BSD Licensed.
Pros - Really good documentation - Active community - Regular releases - Syntax is similar to jUnit (test suites, asserts and setup/teardown) - Asynchronous support - Good for DOM testing - Tests always run sequentially in the order they are added to a suite
Cons - Automation not trivial to implement,but less difficult than other frameworks
Also check out http://sinonjs.org/
Test spies Test stubs Mocks Fake timers Fake XHR Fake server Sandboxing Assertions
It does work along with qUnit and that has been a plus so far.
Oh, and to automate testing as part of the build we used a simple selenium script to navigate through our testsuite (one testing page per JS file), and 'listened' for a pass or fail CSS class (added by QUnit). This works headless as well for IE/FF2 AFAIK
For mozilla development, I fall in love with UXU, based on MozUnit but still active. Has nice features like mock server or sleep / yeld methods.
I use the Screw Unit test framework and I've written my own mocking library called jsMocha which has been in heavy use in the company I work at for over 6 months.
CrossCheck seemed extremely powerful when I looked at it, but we've not incorporated it into our build process at this time. It has the advantage of being browserless, and thus should work well in an automated build-and-test scenario.
The advantage of HtmlUnit is that it isn't driving a browser so it is FAST.
The downside is that it isn't driving a browser so there are some JS things that won't work. But offsetting that they can run the JQuery tests so the JS support might be good enough for what you need.
JsUnit is run from either the browser, through its Eclipse plug-in, or automatically through an ANT task. You create an HTML page with a bunch of test functions, which must be named with the prefix ‘test’, include the JS file you are testing. When any assert within a function fails, the entire function fails and stops executing. There is no guaranteed order in which these tests are run. You can create setup() and teardown() functions.
License: GPL, GLPL, MPL
- Automation is relatively easy to implement
- A lot of functionality
- Syntax is similar to JUnit
- Not great for DOM testing since it runs tests inside an iFrame.
- No guarantee that tests will be run in the order they are written.
- Can’t use Firebug on testrunner page. Need to have another tab open with the actual test code.