1. Technology

JavaScript Testing

Why Ad Hoc Testing May Not Be Enough


For a really simple JavaScript that only does one or two things, simply running the script to see that it does what it is supposed to may be all that is required to ensure that the script is going to actually work. As your JavaScript becomes more complicated doing ad hoc testing like this may not be sufficient to ensure that the script will work.

As your JavaScript code grows in complexity, the number of different paths that can be taken through the code are going to increase. Every additional if/else statement potentially doubles the number of separate paths through the code. Loops and switch statements may increase the number of paths even further.

Ad hoc testing relies on your remembering what testing you have already run so as to be able to run the further tests that you think you need. That is easily done when there are only two or three tests that you need to run to make sure the code all works correctly but when you have hundreds or even tens of thousands of different paths through your script there is no way that you are going to be able to even keep track of what tests you need in your head and certainly will not be able to keep track of which ones you have already run, which are yet to be run, and which need to be rerun because you have changed the code to fix an error since you last ran them.

The best that you can do with ad hoc testing is to test the most obvious paths through your code to ensure that the script actually does what it is supposed to at least for the most common alternatives.

To be able to test that your script is going to work even when the less common paths through the code are run you need to run a lot more tests than you can track properly in your head. Now obviously if there are tens of thousands of possible paths through your code you are not going to be able to test them all but instead need to test an appropriate selection that ensures that all of the code gets run at least in one of the tests and that all of the most likely paths are tested.

To be able to do this you need to plan your testing rather than just running tests ad hoc as they occur to you and hoping that you cover everything you need. Planning your testing means writing down the details of tests including what the test is for, what needs to be input to run the test, and the results that are expected when the test is run. Writing down this information for each of your proposed tests means that you have clearly defined outcomes for each test that tell you whether the test worked or not and also can easily keep track of which tests have and haven't been run. Planning your tersts may also include writing down tests that you decide not to run along with the reason why you have decided to omit that particular test.

Basically planning your tests means that you have a complete list of all of the tests that you need to run and know throughout the testing stage just what has and hasn't been tested. This enables you to be far more certain as to whether your code is going to work correctly in more situations than would be the case if you had just run tests ad hoc as they occurred to you.

  1. About.com
  2. Technology
  3. JavaScript
  4. JavaScript Testing
  5. Why Ad Hoc Testing May Not Be Enough

©2014 About.com. All rights reserved.