NST JavaScript Unit Testing
Overview
Over the years, we have accumulated numerous JavaScript functions and classes which we use as building blocks when creating the NST website (http://networksecuritytoolkit.org) and the web based user interface (NST WUI) for the Network Security Toolkit distribution.
As we rely on our core set of JavaScript building blocks to perform their tasks in a reliable manner, it became desirable to have a method which would allow us to quickly verify that these building blocks behaved in a consistent manner.
After being exposed to the power of the JUnit (http://junit.org/) unit testing framework for Java based code, we decided to create a unit testing framework for our JavaScript code.
The framework we came up with permits us (actually anyone) to quickly verify that various JavaScript functions and classes are behaving as expected simply by loading the following page into a web browser:
This Wiki page describes the conventions used in our JavaScript unit testing framework. This framework was designed such that it can be used outside of the NST development area (anyone with JavaScript code they would like to apply unit tests to is free to follow these conventions as well).
Goals of JavaScript Unit Tests
- Verify expected behaviors of simple JavaScript functions and classes.
- Verify that expected behaviors are supported by all supported browser environments.
- Be simple to run (simply loading: TestSuite.html into your browser runs all of the unit tests for the NST project).
What JavaScript Unit Tests Don't Do
- Test service based behaviors (AJAX based features are difficult to unit test).
- Test modifications to the active document (DOM changes are difficult to unit test).
- Test visual renderings or appearance of rendered pages.
Requirements
Skills
Before proceeding with unit testing, it is recommended that you are familiar with:
- Have a basic understanding of what unit testing means.
- Using a web browser to load HTML pages directly from your file system.
- Using a text editor to create and modify JavaScript code.
- Have a basic understanding of JavaScript objects (creating classes) and the JavaScript prototype keyword.
Files/Directories
To add unit testing capabilities to a existing project, create a sub-directory named test and install the following files into it:
- TestSuiteTemplate.html
- This file can be saved as TestSuite.html. It will become the file you load into your web browser when you want to run your unit tests.