- 1 Overview
- 2 Requirements
- 3 Walk Through
- 3.3 Adding Unit Test Files
- 3.4 Updating The TestSuite.html File
- 3.5 Running The Unit Tests and Reviewing The Results
- 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).
- 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.
Before proceeding with unit testing, it is recommended that:
- You have a basic understanding of what unit testing means.
- You know how to use a web browser to load HTML pages directly from your file system.
To add unit testing capabilities to a existing project, create a sub-directory named test and install the following files into it:
- This file provides some style information for display the results of running the unit tests within a HTML page.
- 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.
- This file provides an example set of unit tests (for a function in the UnitTest.js file). You won't need this file in the long run, but it is loaded and run by the default TestSuiteTemplate.html template file.
Our example basename implementation can be downloaded from:
Our example dirname implementation can be downloaded from:
- Create the test sub directory.
- Download and install test/UnitTest.js into the test sub directory.
- Download and install test/UnitTest.css into the test sub directory.
- Download and install test/TestUnitTest.js into the test sub directory.
- Download and install test/TestSuiteTemplate.html into the test sub directory.
- Rename test/TestSuiteTemplate.html to test/TestSuite.html.
Adding Unit Test Files
Updating The TestSuite.html File
You will need to edit the TestSuite.html file you downloaded in the following manner:
- Find "STEP 1" in TestSuite.html and update it to include your two new unit test classes. Change:
var unitTests = [ new TestUnitTest() ];
var unitTests = [ new TestBaseName(), new TestDirName() ];
- You may optionally remove the TestUnitTest.js file as it should no longer be referenced after the above updates.
Running The Unit Tests and Reviewing The Results
The following is what you should see if you've completed all of the steps in this walk through:
Notice that there is a failure in one of the unit tests. You can click on the links next to the failure to help drill down to the details of the failure.
When you drill down, you'll notice that the test failed because it expected the basename of "/var/log/" to be "log" and that the actual implementation produced "".
This error was left in the example to demonstrate the power of using unit tests to help eliminate ambiguity from your code. If you look at the source for this unit test, you notice a comment that indicates that the unit test wasn't really sure what the results should be when passing "/var/log/" to the basename function. At this point we can proceed to reconcile the discrepency by either updating the basename function to return "log" in this case, or updating the unit test to expect "". We are forced into thinking about what the correct behavior should be in this case.