1. Technology

Testing JavaScript

What is Testing?

By

Testing is the process that you use to ensure that the script you have written actually does what it is supposed to do.Without testing you have no way to tell whether the script actually works.If it were possible to write scripts perfectly so that they always did exactly what they are supposed to do first time and every time then there'd be no need to test. Exen the experts can't write perfect code all the time though and so we need to test all our scripts in order to locate and fix any errors.

From the viewpoint of testing a script, an error is any circumstance where the script does not do what it is supposed to do. An error may or may not cause the script to actually crash. Simply producing a wrong answer (eg. 1.9999999 instead of 2) or tying up so much of the computer's resources so that it can't calculate the required result in the necessary timeframe are errors.

Not all errors necessarily need to be fixed although generally all of those that you encounter in connection with JavaScript will need to be fixed in order for your script to work. One example of an error that could occur while your JavaScript is running that is not JavaScript related and which you don't need to fix is where someone trips over the computer power cord and cuts off power to the computer. As the entire computer crashes at that point there is nothing that can be done within the JavaScript to cater for that error. Since the JavaScript in the web page can always be rerun once the computer is restarted there is no need to attempt to fix that particular error in connection with the JavaScript at all. If there are other things running on the computer where such an error would be a major issue that needs to be fixed then a possible way to handle that particular error would be to install a UPS so that when the power supply to the computer goes off for any reason that the computer is automatically switched over to battery power.

Many of the possible errors that can affect your computer have nothing to do with the running of some JavaScript in your web page and so can be disregarded in so far as testing your JavaScript is concerned. Where a large company has a specific team dedicated to testing it is their job to try to uncover as many errors as they can and to then categorise them into those that need to be fixed and those that don't. Small companies and individuals do not have the resources to deal with identifying the many errors that do not need to be fixed and so in general those not part of a dedicated testing team concentrate primarily on that part of testing known as debugging - locating and fixing the errors that are critical to the operation of the particular script.

Another thing to note with respect to errors in your script is that having your script produce an error message to display to the person running the script is not an error in the script. Producing an error message may be exactly what you expect the script to do in response to particular inputs from the user. It is only an error in so far as testing the script is concerned where the results that are produced are not what should be produced in that situation (such as producing an error message with respect to perfectly valid user input).When we talk about errors with respect to testing we are always referring to a situation where the results of the test are different for whatever reason from what the test says that the results should be. Validation of user inputs and the production of error messages where incorrect values are entered is a part of our script itself and we may have tests to ensure that the right error messages are produced when invalid input is entered.

There are several approaches to testing and which approach is the most appropriate one to use is very dependent on just how complicated that your script is.With just short fragments of code that perform simple tasks an ad hoc approach to testing can be used that consists of little more than debugging of the code to ensure that it appears to do what it is supposed to do. More complex scripts require a more formal test plan which to be most effective ought to be written prior to writing the script that is to be tested.

The biggest advantage of a formal test plan is that it documents a series of tests that can be rerun at any time. Since each formal test will specify both the specific inputs that are required to run the test as well as the expected results, rerunning all the tests after any changes are made to the code will confirm whether those changes have accidental side effects on the rest of the script.

Even with ad hoc testing, you can't test without having a prior expectation of what the script is supposed to be doing. If any result is to be considered as valid then any script will do for producing that result and so there'd be no reason for testing. The main difference between ad hoc and more formal testing is that with ad hoc testing you simply 'know' what the result is that you want and if the script produces that result when you test then your test is successful. The biggest disadvantages of ad hoc testing are that there may be situations you overlooked where the script does not produce the desired results and because you have no written test plan there is no way for someone else to easily rerun the testing to verify that the same results as before are produced if changes have been made to the script.

One important thing to note is that any testing of your script is better than no testing and for many simple scripts a small amount of ad hoc testing will be quite sufficient to confirm that the script actually works. Things are entirely different when you have an extremely complex script (eg. a library such as Jquery). With a really complex script that can be used in lots of different ways you not only need a formal test plan that outlines a large number of different tests that will be needed in order to demonstrate that the code actually works but you will also need some form of automated test suite to make rerunning those hundreds of tests easier.

As you progress from writing really simple JavaScripts to writing more complex ones the importance of testing your scripts will grow as will the need for a more structured and formal approach to testing your scripts. For those with a background in writing using other programming languages, applying their knowledge of testing in those languages to JavaScript will be an obvious progression. For those for whom JavaScript is their first language or who have never formally learnt how to test programs before, it will be essential that you learn more about testing as your scripts become more complex as ah hoc testing of your scripts will only work to a point and you will eventually start producing scripts that are too complex to test properly that way.

  1. About.com
  2. Technology
  3. JavaScript
  4. JavaScript Testing
  5. What is Testing?

©2014 About.com. All rights reserved.