Pages

Monday, June 4, 2012

Writing Unit Tests for Your ArcGIS API for JavaScript Apps

Whether you’re working with an existing application, or you’re planning a new app, unit testing provides an efficient, dependable way to verify that your code functions as expected and ensure that your app’s features work. In a nutshell, unit testing provides an automated way to target, test and validate specific components of an application.
As with most things JavaScript, Dojo has you covered. Specifically, the Dojo Objective Harness (DOH), which ships with Dojo, provides a framework for writing and running unit tests. The ArcGIS API for JavaScript team uses DOH to run unit tests throughout the development process to check that changes made to the API do not break existing functionality, and that new features accomplish their intended purpose. For more background on DOH and to get up to speed on its capabilities, please head over to the Dojo reference guide page for DOH. Please read through that page to get a better understanding of what DOH can do for you.
Now that you’ve had a brief introduction to DOH, let’s talk about how to use it with your custom modules as well as with classes shipped as part of the ArcGIS API for JavaScript. We’ve put up a page showing some simple unit tests that test functionality in a custom module as well as test some functionality in the ArcGIS API for JavaScript. The two tests for the esri.geometry.Extent object were pulled from the JavaScript API’s test suite of over 2,000 tests and are provided as an example of how to write a test that references esri modules. You can also download all of the required files from ArcGIS.com to run the tests locally.
One key change to make when using DOH to test code that references classes from the ArcGIS API for JavaScript is to change the dojoUrl variable to point to the Esri CDN-hosted version of ArcGIS API for JavaScript instead of a locally-hosted or other CDN-hosted version of dojo. Inside a script tag in runner.html, specify window.dojoUrl as the URL to the ArcGIS API for JavaScript:
window.dojoUrl = "http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.8";
By default, test modules are specified via a query string parameter called testModule. In the example linked above, the tests.all module is specified. That module rolls up tests from a couple of other modules that contain unit tests. By using modules, DOH allows you to run any number of tests, in any number of modules easily. For instance, to run only the tests in tests.TestModule1, specify that module as the testModule query string parameter. To see the specifics of how the various unit tests are implemented, please download the code from the link above.
Is this more work? Yes. Is it worth the trouble? Absolutely. While writing tests adds to the work required for app development, seeing tests run and pass instills confidence in your application that would otherwise be missing. By writing unit tests for your code, you can be sure that your app’s features and functionality work as expected. When tests are run throughout the development process, you know exactly how changes affect existing code and when things break. Unit tests do not completely replace manual, in-browser testing but they can go a long way to eliminating the majority of that work.
Source ESRI Blog

No comments:

Post a Comment