Hi all, On the Fluid Project, we have been using Edward Hieatt's jsUnit (http://www.jsunit.net/ ) for unit testing all of our code. A number of problems--including the inability to use Firebug to debug failing tests--caused us to look elsewhere for a less intrusive JavaScript testing framework.
The jQuery testing suite is great! I've ported all of my tests over to it, and have been really happy with the results. Along with this, I've made some simple extensions to the jQuery testing suite (which I've started calling "jqUnit" for convenience) that I wanted to share with the wider community and get your feedback. First of all, I've attached a patch to testrunner.js, which moves all its functions into a closure for privacy and exposes the public test API within a namespace called "jqUnit." This will help avoid conflicts in the global namespace. Since I pass $ in as an argument, it also ensures that we don't have any problems in noConflicts mode. This patch isn't an example of beautiful refactoring, but I wanted to favour clarity--if people find this interesting, I can clean up the code and indenting. Secondly, I've written a number of adaptor functions to provide compatibility for my old jsUnit tests. These provide xUnit-style asserts, and are just wrappers around the underlying jQuery assert functions such as ok(). Perhaps these are useful for others who are porting from jsUnit? Lastly, I've added support for setUp() and tearDown() functions which get run before and after each test. I've created a simple object called TestCase. Its constructor takes three arguments: the module's name, your setUp function, and your tearDown function. Is this useful to anyone? Colin
testrunner.js.patch
Description: Binary data
var jqUnit = jqUnit || {}; (function ($) { var jsUnitCompat = { assertEquals: function (msg, expected, actual) { jqUnit.equals (actual, expected, msg); }, assertTrue: function (msg, expected) { jqUnit.ok (expected, msg); }, assertFalse: function (msg, expected) { jqUnit.ok (!expected, msg); }, assertUndefined: function (msg, expected) { jqUnit.equals ((typeof expected), 'undefined', msg); }, assertNotUndefined: function (msg, expected) { jqUnit.ok (!(typeof expected === 'undefined'), msg); }, assertNull: function (msg, expected) { jqUnit.equals (expected, null, msg); }, assertNotNull: function (msg, expected) { jqUnit.ok (!(expected === null), msg); } }; // Mix these compatibility functions into the jqUnit namespace. $.extend(jqUnit, jsUnitCompat); // TestCase object function TestCase (moduleName, setUpFn, tearDownFn) { this.moduleName = moduleName; this.setUp = setUpFn || null; this.tearDown = tearDownFn || null; jqUnit.module(this.moduleName); }; TestCase.prototype.test = function (string, testFn) { if (this.setUp) { this.setUp (); } jqUnit.test (string, testFn); if (this.tearDown) { this.tearDown (); } }; // Mix the TestCase type into the jqUnit namespace. $.extend(jqUnit, {TestCase: TestCase}); }) (jQuery);Title: Test Title Goes Here
Test Title Goes Here |
--- Colin Clark Technical Lead, Fluid Project http://fluidproject.org