SEARCH :
This SectionSite Wide
 

Ektron's Developer Group Blog

A blog for Ektron users, by Ektron Developers

Unit Test JavaScript

 Permanent link

JsUnit is a JavaScript framework similar to JUnit and NUnit for unit testing JavaScript code.

Once JsUnit is installed on your development computer, browse to the testRunner.html page included with JsUnit, that is, http://localhost/jsunit/testRunner.html. Then enter the URL to your test page and click the button to run the tests.

In the test page, define one or more functions that began with the name 'test', as in 'testMyCode'. The testRunner page will call every function that begins with the name 'test'.

Write your 'test' functions to call various 'assert' functions defined by JsUnit. They include:

  assert("true should be true", true);
  assertTrue("true should be true", true);
  assertFalse("false should be false", false);
  assertEquals("1 should equal 1", 1, 1);
  assertNotEquals("1 should not equal 2", 1, 2);
  assertNull("null should be null", null);
  assertNotNull("1 should not be null", 1);
  assertUndefined("A declared but unassigned variable should have the undefined value", myVar);
  assertNotUndefined("1 should not be undefined", 1);
  assertNaN("a string should not be a number", "string");
  assertNotNaN("1 should not be not a number", 1);

And, of course, be sure to include JsUnit in your test page.

 <script language="JavaScript" type="text/javascript" src="/jsunit/app/jsUnitCore.js"></script>

Example,

 function testMyCode()
 {
  assertNotUndefined("MyGlobal must be defined", MyGlobal);
  assertEquals("1.0", MyCode.VERSION);
  assertNotNull(MyObject);
 }

http://www.jsunit.net/


After downloading I was worried when I saw the JSP and all the java code. That stuff is for an optional Server part of jsUnit. I was happy to see it was not necessary for run tests as you descibe.
Posted by: Eric Maddox at 8/1/2008 4:36 PM