For those who are new to JavaScript let me give you some information. As per Wikipedia
"JavaScript is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites.".Regarding the popularity of Java Script trends.builtwith.com says
708,651 websites using this within the top million sites on the internet and an additional extended total of 4,797,491 websites that are using Javascript.
And why my decision is wise?
- Runs on “most” browser/platform combinations
- Preserves the standards of a typical XUnit framework
- Open source - Free!!!
- Can be hosted on a web server so that tests can be run across enterprise without different setups
Some facts about JsUnit
- Unit tests in JsUnit are called Test Functions
- Test Functions live in an HTML page called a Test Page
- A Test Page is any HTML page that has a JavaScript “include” of jsUnitCore.js
- jsUnitCore.js provides the assertion functions of JsUnit, e.g. assertEquals(comment, arg1, arg2)
- JsUnit supports setUp() and tearDown()
- A Test Suite Page declares a suite() function that returns a JsUnitTestSuite for grouping Test Pages
- The JsUnit testRunner.html page runs Test Pages
For most of the open source tools I worked with, installation is a night mare. But JsUnit is pretty straight forward and is just 2 steps
- Download installation zip from http://www.jsunit.net/
- Unzip file in some local directory or on a webserver to host JsUnit
To verify the installation
- go to http://xx.xx.xx.xx/jsunit/testRunner.html, where xx.xx.xx.xx is the IPAddress or Hostname of the server on which JsUnit is hosted
- Enter xx.xx.xx.xx/jsunit/tests/TestPageTest.html
- Hit run button
- Progress should change to green
My First Test Case
I would like to unit test below function
function multiply(arg1, arg2) {
return arg1*arg2;
}
return arg1*arg2;
}
Write a html page as below and put it in tests folder under JsUnit base folder
<html>
<head>
<script language="JavaScript" src="../app/jsUnitCore.js"></script>
<script language="JavaScript" src="math.js"></script>
<script language="JavaScript">
function testWithValidArguments() {
assertEquals("2 times 3 is 6", 6, multiply(2, 3));
assertEquals("Should work with negative numbers", -20, multiply(-4, 5));
}
function testWithInvalidArguments() {
assertNull("null argument", multiply(2, null));
assertNull("string argument", multiply(2, "a string"));
assertNull("undefined argument", multiply(2, JSUNIT_UNDEFINED_VALUE));
}
<head>
<script language="JavaScript" src="../app/jsUnitCore.js"></script>
<script language="JavaScript" src="math.js"></script>
<script language="JavaScript">
function testWithValidArguments() {
assertEquals("2 times 3 is 6", 6, multiply(2, 3));
assertEquals("Should work with negative numbers", -20, multiply(-4, 5));
}
function testWithInvalidArguments() {
assertNull("null argument", multiply(2, null));
assertNull("string argument", multiply(2, "a string"));
assertNull("undefined argument", multiply(2, JSUNIT_UNDEFINED_VALUE));
}
function testStrictReturnType() {
assertNotEquals("Should return a number, not a string", "6", multiply(2, 3));
}
</script>
</head>
<body>
This is a Test Page for multiplyAndAddFive(arg1, arg2).
</body>
</html>
Thats all you have to do and now you can run your tests using http://xx.xx.xx.xx/jsunit/testRunner.html.
-------------------------------------------------------------------------------------------------------------------
Testing can be used to show the presence of bugs,but never to show their absence!
Edsger Dijkstra
No comments:
Post a Comment