Sie sind auf Seite 1von 5

JsUnit In-browser Javascript Unit Testing

Introduction:
JsUnit is an open-source unit testing framework for JavaScript. It preserves the standards of the xUnit frameworks. It is written in Javascript and runs on most browsers and platforms. It has a set of assertion functions plus some JavaScriptspecific functions like setUp, tearDown, test suites, etc.

JsUnit Classes and Functions:


JsUnit has following classes that contains javascript testing, debugging and useful functions: 1. Clock 2. GLOBALS 3. JsUnitAssertionArgumentError 4. JsUnitError 5. JsUnitFailure 6. JsUnitTestSuite 7. MockXmlHttpRequest 8. Utilities

Valid Example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>TestPage Tests</title> <link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css"> <script type="text/javascript" src="../app/jsUnitCore.js"></script> <script type="text/javascript" src="../app/jsUnitTestManager.js"></script> <script type="text/javascript" src="../app/BaseUiManager.js"></script> <script type="text/javascript" src="../app/ClassicUiManager.js"></script> <script type="text/javascript" src="../app/jsUnitParams.js"></script> <script type="text/javascript"> function testGetStatus() { assertEquals("noTestsYet", createTestPage(0, false, 0, 0, 0).getStatus()); assertEquals("ready", createTestPage(2, false, 0, 0, 0).getStatus()); assertEquals("running", createTestPage(2, true, 0, 0, 0).getStatus());

assertEquals("running", createTestPage(2, true, 1, 1, 1).getStatus()); assertEquals("success", createTestPage(2, false, 1, 0, 0).getStatus()); assertEquals("failure", createTestPage(2, false, 1, 1, 0).getStatus()); assertEquals("error", createTestPage(2, false, 1, 1, 1).getStatus()); } function createTestPage(testCount, isRunning, successCount, failureCount, errorCount) { var testPage = new JsUnit.TestPage("url"); for (var i = 0; i < testCount; i++) { testPage.addTest("test " + i); } testPage.running = isRunning; testPage.successCount = successCount; testPage.failureCount = failureCount; testPage.errorCount = errorCount; return testPage; } </script> </head> <body> <h1>Valid Javascript Test Code</h1> </body> </html>

Invalid Example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css"> <script type="text/javascript" src="../app/jsUnitCore.js"></script> <script type="text/javascript"> function testFailure() { assertTrue(false); } function testError() { nonExistantFunction(); } </script> </head> <body> <h1>Invalid Javascript Test Code</h1> </body> </html>

References:
http://en.wikipedia.org/wiki/JSUnit http://sourceforge.net/projects/jsunit/ http://www.jsunit.net/ file:///C:/Documents%20and%20Settings/Administrator/Desktop/jsunit/testRunner.html file:///C:/Documents%20and%20Settings/Administrator/Desktop/jsunit/doc/js_docs_out/index.html file:///C:/Documents%20and%20Settings/Administrator/Desktop/jsunit/doc/js_docs_out/GLOBALS.html#!s!assert

Created By: M.Saad Siddiqui (06-0110)