Version 4, changed by psowden 09/10/2005. Show version history
Once you've contributed some code to Dojo it's a good idea to write some unit tests to make sure your code works. These files live in the tests/ tree. The structure in this directory mirrors that of the src/ tree. The files containing the tests are prefixed with test_, so for example, the test file for src/uri/Uri.js lives at tests/uri/test_Uri.js.
The first thing in your JavaScript file should be the relevant loadModule call, to import the module you are writing unit tests for. You can then populate the file with functions you need for the tests. The actual functions called by the testing framework should be prefixed with test_$MODULE_, so for example, the test functions used in the dojo.uri.Uri unit tests are prefixed with test_uri_. These functions are harvested by the unit testing framework and executed.
The framework provides a couple of functions which you can use for the actual testing:
The name argument is a string that is printed if the test fails. jum.debug can be used to print messages and can be used to help you figure out what's going on when constructing your unit tests.
Unit tests should be broken down into grainular groups and placed inside different functions. These constitute sets of functionality, and if any of the tests fail in a group the group fails and the remaining tests are not executed.
dojo.hostenv.loadModule("dojo.uri.Uri");
function test_uri_testBases(){
var base = 'http://a/b/c/d;p?q';
jum.debug("------------------------ URI tests");
jum.assertEquals(
"test1",
(new dojo.uri.Uri(base, 'g')).toString(),
'http://a/b/c/g');
jum.assertTrue(
"test2",
(new dojo.uri.Uri(base, 'g:h')).toString() == 'g:h');
}To run the tests you'll need to have Apache Ant installed and running. Change into the buildscripts/ directory and run ant test