Friday, 12 June 2009

Unit testing jQuery plugins with jqunit

Whilst creating a plugin for jquery, that would provide auto formatting for a textbox for numeric values, I was looking for a way to run the tests for confirm its functionality. After getting sick of running the tests manually the penny dropped that I could unit test the plugins functionality. After a little searching I came across jqunit that seems to fit the bill perfectly.

The project is hosted over at google code. The tests them self run from a html file but the a template is provided with the jqunit library. The tests are simple to create and follow the jquery syntax style. Below is a smple test case with unit test


var numericTestCase = new jqUnit.TestCase('jQuery.numeric - decimal',function(){
/*setup*/

},function(){

/*teardown*/

});
numericTestCase.test('T1: Null Value ',function(){
var txtInput = $("");
txtInput.numeric(true);
txtInput.blur();

jqUnit.equals(txtInput.val(),"0.00");
});


Tests can quickly be written to test the functionality of your code. With the testing being able to run form a single location it provides a quick an easy way to check your plugins work across multiple browsers, that is, as long as you have written the correct tests.

1 comment: