Click here to Skip to main content
Click here to Skip to main content

Unit Testing JavaScript Functions

By , 13 Feb 2011
 

Introduction

This article is all about unit testing JavaScript functions using the unittest.js library provided by Script.aculo.us.

We always use UI and test against, and that is what dev and QA folks do. But there is some thing which can be done by developers apart from that. We can unit test JavaScript functions without the UI getting ready. Which means we can completely test JavaScript functions at a very early stage.

All you have to do is follow the three steps mentioned in the article, and you will surely enjoy unit testing.

Using the code

Following are the steps for downloading Script.aculo.us.

  1. Go to http://script.aculo.us/downloads to grab the latest version in a convenient package.
  2. Unpack the downloaded file and you will find the following:
    • lib: This folder has the prototype.js file.
    • src: This folder has these eight files:
      1. builder.js
      2. controls.js
      3. dragdrop.js
      4. effects.js
      5. scriptaculous.js
      6. slider.js
      7. sound.js
      8. unittest.js
    • test (folder): has files for testing purpose.

How to use?

All we require is unittest.js and the stylesheet test.css available in the downloaded folders.

  1. Create a JavaScript file which contains the functions which you wish to unit test.
  2. var Validator = {
       // Returns the string, or null if not a valid email
       email: function (str) {
         str = str.squash();
         var result = (/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i).exec(str);
         return (result) ? result[0] : null;
       }
    };
  3. Create a unit test JavaScript class:
  4. new Test.Unit.Runner({
    
       testValidator: function () { 
         this.assertNull(Validator.email(' ranjancse@gmail.com '));
         this.assertNull(Validator.email(' roshancse@gmail.com  '));
     });

    The Test.Unit.Runner object receives our test object and executes all the methods. In our case, there is only one method for testing. Each of the test methods performs a series of checks called assertions to ensure that our code works correctly.

    There exists a good number of assert methods available in the JavaScript library unittest.js, just try to explore and use them.

  5. Create a sample HTML page and include the following scripts:
    • unittest.js - The framework library provided by Script.aculo.us.
    • Experniment.js - This file contains the JavaScript functions.
    • test.js - This file contains the unit test methods. We can add as many unit tests as we want.

    We create a DIV with id="testlog" which will be used by the unit test library to fill the test results. We include the stylesheet test.css, which contains the styles to show the test results in a more colorful way.

Unit Test Results

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ranjan.D
Web Developer
United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberhasan.rounak18-Feb-11 5:44 
GeneralMy vote of 4membercorol123414-Feb-11 10:03 
GeneralMy vote of 5memberramakrishna.pal114-Feb-11 7:21 
GeneralMy vote of 5membersraoh14-Feb-11 6:23 
GeneralRe: My vote of 5memberRanjan.D14-Feb-11 7:06 
Yes true. Even I am also new to this one , got to know this about 2 days back.. We both are almost on same boat.
Ranjan.D

Generalthanks for sharing - have 5memberPranay Rana14-Feb-11 2:46 
GeneralRe: thanks for sharing - have 5memberRanjan.D14-Feb-11 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 13 Feb 2011
Article Copyright 2011 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid