AJAX and Unit Testing - it's time to mingle

February 13, 2006

I've decided to write a little two part introduction into unit testing your AJAX applications with JSUnit. AJAX applications now are adding a new complexity into our development lives. Introducing business logic into our presentation tier. It is now not enough to write some adhoc javascript form validation functions that work most of the time. You now need to take accountability for your javascript code as it can affect your business logic on the server side.

As briefly as I can describe it unit testing gives a developer freedom. "Freedom?? but I have to write more code how the hell is that free?". Unit testing can free you as a developer by making sure your code is accounted for with automated tests. If you're in a multi-developer environment this is crucial to the success and bug management of your project. Imagine you spend weeks writing this great class library, all the code is pretty, well documented, works great! Now Kevin comes in a decides he wants to change the functionality of one of your methods to match something he's working on. His code now works but that change just broke 10 other areas where it was being used. F'ing Kevin! You will never catch this unless you're monitoring every change list that developers submit, or you have an automated testing harness that will run the code nightly (at least) and report failures. If you had a properly tested library, this would have been caught after Kevin ran the unit test suite and realized that code he change did have a purpose and he could look at the test and now see what the purpose was. That my friend is the power of the unit test.

Ok so with that out of the way let's talk about how to harness your new great AJAX code!


First thing you're going to need is a unit test suite for javascript. The current gold standard is JSUnit (http://www.edwardh.com/jsunit/). JSUnit allows you to write html pages with javascript assertions built in. Download the latest JSUnit package to your hard-drive and you'll notice in there it will unzip itself to a jsunit/ directory. The main thing you will be concerned is the jsUnitCore.js file in the jsunit/app directory. That's the file that makes the magic happen. You will need to include this in your testing scripts. The other file of note is jsunit/testRunner.html file. This will be the main test runner page that you will use to view the status of your tests. Mmmmm the Green bar!

Lets take a look at this image to give us an overview of how we want our testing structure setup to start with.


What this relationship shows is that we have our main testRunner.html page which we'll access through the browser. If you take a look at this UI shot of the testRunner interface, you'll notice a UI text box to put in a test script.


This test script can be a single page or a "suite" of pages. The nice part about the suite is you can have suites of suites. What this means is let's say you have an inventory module you are working on. You also have an order entry module you just finished. The inventory modules has 2 javascript classes in 2 files, so you created two test scripts so far. You can roll those into what's called a "suite" so you only have to run one page to actually run both sets of tests. You could also have a "TOTAL PACKAGE" suite that runs all your modules. Sometimes though you might just want to test a module you're working on for speed's sake.

Lets jump into our first script just to make sure we're up and running. This will just cover getting us up and running. I will post another write up on how to actually test your ajax application.

Environment setup:
Let's assume we downloaded jsunit to our root directory so our web structure looks like this:

Htdocs/jsunit/

First things first. Let's make sure everything unpackaged correctly and go to: http://localhost/jsunit/testRunner.html

(if you installed to a different location, point your browser to the appropriate place. Unix servers must also take note of the capital R in testRunner.html)

You should now be looking at the same UI screen that I've shown at the beginning of this page.

Now let's write our first script in the TDD (test driven development) fashion where tests come before code. We just want to make sure we're working properly so here we go.

Step 1:
Create a file called testOfJSUnit.htm

  1.  
  2. <html>
  3. <head>
  4. <title>Test Page for Inventory Module</title>
  5. <script language="javascript" src="http://localhost/jsunit/app/jsUnitCore.js"></script>
  6. </head>
  7. <body>
  8. <script language="javascript">
  9.  
  10. function testSetupWorks() {
  11.  
  12. assertEquals("Should have equaled true for setup", 1, 1);
  13. }
  14. </script>
  15. </body>
  16. </html>


So all we're trying to do is assert that 1 does in fact equal 1 which will give us a passing score. We just want to make sure we're all setup properly. So now we have a bonafide JSUnit test script. Here's what we did:

You'll notice at the top this line
  1.  
  2. <script language="javascript" src="http://localhost/jsunit/app/jsUnitCore.js"></script>


That's the meat and potatoes. We want to include the core library for JSUnit so we have access to all the assert methods.

  1.  
  2. function testSetupWorks() {
  3.  
  4. assertEquals("Should have equaled true for setup", 1, 1);
  5. }


The function above is a test function. Anything that begins with "test" gets run in JSUnit. We're using the function assertEquals which takes 3 arguments. The args are: a comment to show if the test fails, the condition to match and a condition to test. So we're testing that 1 is equal to 1.

Now go back to your browser and make sure you're at http://localhost/jsunit/testRunner.html

You'll see that text input where it says: "Enter the filename of the Test Page to be run:" In there type in the path to the file you created, so for me I would type in:
"localhost/Projects/test/testOfJSUnit.htm". Notice they already add the http:// for you. Click the run button and you should see a green bar for a passing test like this below:



If you see the green bar congrats! You have successfully installed JSUnit. This week I'll be doing a write up on how to test some common ajax functionality as well as setting up a test suite to run all our tests. If you didn’t get JSUnit installed correctly visit their mailing list here: http://groups.yahoo.com/group/jsunit/

Comments

RSS feed for comments on this post.

  1. srini says:
    February 15, 2006 @ 01:26 — Reply

    Your article is amazing..I wanna work on this topic i.e unit testing Ajax Please help me and give me enough cooperation

  2. dave says:
    February 16, 2006 @ 13:51 — Reply

    Ankit

  3. asdasd says:
    July 30, 2006 @ 22:09 — Reply

    Intresting post! Thanx!...

  4. Luisd says:
    April 14, 2006 @ 03:36 — Reply

    Jim, when are you planning to publish the second part of your tutorial?

  5. Ad says:
    August 14, 2006 @ 10:49 — Reply

    Thank you very much for writting this tutorial it has really helped me get this Unit testing off the ground!!

  6. N says:
    August 16, 2006 @ 22:12 — Reply

    Thank you for the info, very helpfull for getting a start on JsUnit :-D.

  7. ramu says:
    October 6, 2006 @ 07:48 — Reply

    hi whether is it possible to mingle jsunit with ajax

  8. hebiryu says:
    November 16, 2006 @ 14:34 — Reply

    thanks man.. very simple and very helpful what about unit testing with script.aculo.us can u make something like that?

  9. Alex says:
    January 30, 2007 @ 15:22 — Reply

    For AJAX testing I can recommend SWEXplorerAutomation SWEA (http:\\webiussoft.com)

  10. Darrell says:
    January 30, 2007 @ 19:59 — Reply

    Someone asked about part 2 of this article. It was published on November 21, 2006 at http://www.litfuel.net/plush/?postid=154.

  11. Hungnm says:
    March 3, 2007 @ 04:28 — Reply

    Very userfull Jim, and I have a prblem with unit ajax application, all everyboy can help me, - I' developing an struts app + ajax + json, how do I could test json object before return to client? Thanks very much

  12. Ed Burns says:
    March 23, 2007 @ 01:59 — Reply

    Here's a little thing that lets you use JUnit or TestNG to test an ajax application. http://www.theserverside.com/tt/articles/article.tss?l=UsingMCPTestingAjax

  13. Luca says:
    June 5, 2007 @ 08:12 — Reply

    hi people, anyone here tried AJAX testing with Mercury QuickTest Professional? and what were the results? Thanks!

  14. Prasad Pathak says:
    August 17, 2007 @ 12:03 — Reply

    This is really a nice article. This gives idea to a whole new concept of testing for js code, classes etc. Its Amazing !!!

  15. Pallet Jacks says:
    June 20, 2010 @ 06:26 — Reply

    Comment pending moderation

  16. Anonymoussdfsdf says:
    November 23, 2009 @ 03:04 — Reply

    Comment pending moderation

  17. Anonymoussd says:
    November 23, 2009 @ 03:05 — Reply

    Comment pending moderation

  18. replica watches says:
    November 23, 2009 @ 03:07 — Reply

    Comment pending moderation

  19. nike shox says:
    December 20, 2009 @ 11:50 — Reply

    Comment pending moderation

  20. migliori siti di Black Jack says:
    December 22, 2009 @ 11:38 — Reply

    Comment pending moderation

  21. buy r4i software says:
    January 21, 2010 @ 13:11 — Reply

    Comment pending moderation

  22. watches says:
    March 24, 2010 @ 01:10 — Reply

    Comment pending moderation

  23. watches says:
    March 24, 2010 @ 01:14 — Reply

    Comment pending moderation

  24. watches says:
    March 24, 2010 @ 01:18 — Reply

    Comment pending moderation

  25. Anonymous says:
    April 2, 2010 @ 05:12 — Reply

    Comment pending moderation

  26. ladies watches says:
    April 2, 2010 @ 07:01 — Reply

    Comment pending moderation

  27. Magento Development India says:
    April 2, 2010 @ 07:58 — Reply

    Comment pending moderation

  28. get him back forever review says:
    April 17, 2010 @ 16:45 — Reply

    Comment pending moderation

  29. mobile kitchen rental says:
    May 27, 2010 @ 17:54 — Reply

    Comment pending moderation

  30. blu ray ripper says:
    April 18, 2010 @ 10:58 — Reply

    Comment pending moderation

  31. Hollywood Wallpapers says:
    April 19, 2010 @ 01:35 — Reply

    Comment pending moderation

  32. Airport Car Rentals says:
    April 19, 2010 @ 19:48 — Reply

    Comment pending moderation

  33. replica watches says:
    April 20, 2010 @ 16:19 — Reply

    Comment pending moderation

  34. omega watches says:
    April 20, 2010 @ 16:22 — Reply

    Comment pending moderation

  35. convert hd video says:
    May 5, 2010 @ 06:52 — Reply

    Comment pending moderation

  36. Satellite Internet Service Providers says:
    May 7, 2010 @ 02:30 — Reply

    Comment pending moderation

  37. ev dekorasyon says:
    May 10, 2010 @ 11:22 — Reply

    Comment pending moderation

  38. logo design says:
    May 11, 2010 @ 06:34 — Reply

    Comment pending moderation

  39. sumit arora says:
    May 12, 2010 @ 07:42 — Reply

    Comment pending moderation

  40. mobile kitchen rental says:
    May 16, 2010 @ 12:13 — Reply

    Comment pending moderation

  41. Jeux de moto says:
    May 18, 2010 @ 15:36 — Reply

    Comment pending moderation

  42. Giochi per ragazze says:
    May 18, 2010 @ 16:58 — Reply

    Comment pending moderation

  43. Anonymous says:
    May 18, 2010 @ 21:12 — Reply

    Comment pending moderation

  44. cilt bak&#305;m&#305; says:
    May 18, 2010 @ 21:15 — Reply

    Comment pending moderation

  45. 642-067 says:
    May 19, 2010 @ 07:56 — Reply

    Comment pending moderation

  46. medium season 6 episode 22 says:
    May 21, 2010 @ 11:33 — Reply

    Comment pending moderation

  47. wife swap season 6 episode 8 says:
    May 21, 2010 @ 11:34 — Reply

    Comment pending moderation

  48. shopping says:
    May 21, 2010 @ 12:33 — Reply

    Comment pending moderation

  49. Pariloto says:
    May 21, 2010 @ 18:05 — Reply

    Comment pending moderation

  50. Get Him Back Forever Review says:
    May 22, 2010 @ 16:42 — Reply

    Comment pending moderation

  51. piese auto says:
    May 26, 2010 @ 15:21 — Reply

    Comment pending moderation

  52. laptop battery supplier says:
    May 29, 2010 @ 07:14 — Reply

    Comment pending moderation

  53. Jeux de voiture says:
    May 30, 2010 @ 12:35 — Reply

    Comment pending moderation

  54. nike shox says:
    June 5, 2010 @ 03:08 — Reply

    Comment pending moderation

  55. virbram five fingers says:
    June 5, 2010 @ 03:15 — Reply

    Comment pending moderation

  56. santa letters says:
    June 5, 2010 @ 18:27 — Reply

    Comment pending moderation

  57. Business Logo says:
    June 5, 2010 @ 23:25 — Reply

    Comment pending moderation

  58. coach purse says:
    June 7, 2010 @ 08:55 — Reply

    Comment pending moderation

  59. tiffany jewellery says:
    June 8, 2010 @ 01:43 — Reply

    Comment pending moderation

  60. adjustable beds says:
    June 8, 2010 @ 06:24 — Reply

    Comment pending moderation

  61. MKV to iPad says:
    June 9, 2010 @ 07:52 — Reply

    Comment pending moderation

  62. online pharmacy says:
    June 9, 2010 @ 08:11 — Reply

    Comment pending moderation

  63. asdf says:
    June 10, 2010 @ 04:54 — Reply

    Comment pending moderation

  64. air max 90 says:
    June 12, 2010 @ 07:56 — Reply

    Comment pending moderation

  65. CT0-101 says:
    June 13, 2010 @ 01:04 — Reply

    Comment pending moderation

  66. eurodebt says:
    June 15, 2010 @ 13:05 — Reply

    Comment pending moderation

  67. web designers says:
    June 15, 2010 @ 13:44 — Reply

    Comment pending moderation

  68. Acne Scar Treatment says:
    June 16, 2010 @ 11:40 — Reply

    Comment pending moderation

  69. edhardy says:
    June 17, 2010 @ 07:46 — Reply

    Comment pending moderation

  70. replica tag watches says:
    June 18, 2010 @ 02:44 — Reply

    Comment pending moderation

  71. fake tag watches says:
    June 18, 2010 @ 02:46 — Reply

    Comment pending moderation

  72. navicekarper says:
    June 19, 2010 @ 06:51 — Reply

    Comment pending moderation

  73. Self Dumping Hopper says:
    June 22, 2010 @ 09:08 — Reply

    Comment pending moderation

  74. watches replica says:
    June 19, 2010 @ 07:30 — Reply

    Comment pending moderation

  75. Pallet Jacks says:
    June 20, 2010 @ 06:27 — Reply

    Comment pending moderation

  76. jewellery earrings says:
    June 21, 2010 @ 07:32 — Reply

    Comment pending moderation

  77. replicas watches says:
    June 21, 2010 @ 07:35 — Reply

    Comment pending moderation

  78. Meiru says:
    June 21, 2010 @ 08:06 — Reply

    Comment pending moderation

  79. buy replica watches says:
    June 22, 2010 @ 06:44 — Reply

    Comment pending moderation

  80. Save my marriage today review says:
    June 22, 2010 @ 15:55 — Reply

    Comment pending moderation

  81. full lace wigs says:
    June 23, 2010 @ 05:21 — Reply

    Comment pending moderation

  82. payday loans says:
    June 23, 2010 @ 18:02 — Reply

    Comment pending moderation

  83. fast cash advance says:
    June 23, 2010 @ 18:04 — Reply

    Comment pending moderation

  84. online payday loans canada says:
    June 23, 2010 @ 18:06 — Reply

    Comment pending moderation

  85. air jordan says:
    June 24, 2010 @ 08:23 — Reply

    Comment pending moderation

  86. air max says:
    June 24, 2010 @ 08:32 — Reply

    Comment pending moderation

  87. rolex watches says:
    June 24, 2010 @ 09:48 — Reply

    Comment pending moderation

  88. POP Displays says:
    June 24, 2010 @ 11:05 — Reply

    Comment pending moderation

  89. Bet Promotion says:
    June 25, 2010 @ 08:33 — Reply

    Comment pending moderation

Leave a Comment

Line and paragraph breaks automatic, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <code> <em> <i> <strike> <strong>

Comments disabled due to spammers being losers that lead sad lives.