JavaScript Unit Test - Closure Library
Closure library has a powerful js unit testing framework included. I've used multiple javascript unit testing frameworks and for me closure library looked very easy to understand and use. I will explain step by step process to use it on a basic testing scenario below.
JS unit testing requires following two javascript files for any basic test case, and then you would add other libraries depends of what you use in your project.
<script src="../closure/goog/base.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
base.js library is where most of the core APIs resides for closure, and goog.testing.jsunit namespace resides under the file ...closure\goog\testing\jsunit.js.
Now, you can include your project libraries and scripts tags below these tags.
I am using a linklist implementation in javascript as a demo to show how to use the closure unit test to test the output. I have a linklist.js file which contains the implementation as follows
linklist.js
function LinkedList () {
this._head = null;
this._length = 0;
}
LinkedList.prototype = {
constructor : LinkedList,
add : function (data) {
var node = {_data: data, _next: null};
if (this._head === null) {
this._head = node;
} else {
var curr = this._head;
while(curr._next) {
curr = curr._next;
}
curr._next = node;
}
this._length ++;
},
// zero based index
item: function(index) {
if (index < 0 || index > this._length) {
alert("Index out of range");
}
var curr = this._head;
var i = 0;
while (i++ < index) {
curr = curr._next;
}
return {
getData: function() {
return curr._data;
}
}
},
length: function() {
return this._length;
}
};
And then, the test case in an html file as follows.
<!DOCTYPE html>
<html>
<head>
<script src="../closure/goog/base.js"></script>
<script>
goog.require('goog.testing.jsunit'); </script>
<script type="text/javascript" src="LinkList.js"></script>
<script type="text/javascript">
var linkedList= new LinkedList();
var i = 0;
while (i < 10) {
linkedList.add("item - " + i++);
}
</script>
<script>
function testAddedValues() {
for (var i = 0; i < 10; i++) {
assertEquals("item - " + i, linkedList.item(i).getData());
}
}
function testFailureSimulated() {
for (var i = 0; i < 10; i++) {
assertEquals("", linkedList.item(i).getData());
}
}
</script>
</head>
<body>
</body>
</html>
As simple as that, after including the the base libraries, you just need to define your test methods with name prefix with "test". E.g. testAddedValues(), testFailureSimulated etc. You just need to open this test case html file in any browser you want test with. Internally, when you open this file, jsunit will load all the method prefixed with "test" and then executes it one by one in ascending order of the names. So if you have detailed assert statements written against each of the possible scenario, you will see a well formatted result on the page when the testcase executed all the test methods. See below the output from the above test page.
You could also use closure library test runner page and choose to run individual test cases from this page, if you have multiple testcase organized in multiple test files.
See more details about closure here http://code.google.com/closure/ .
Comments
Post a Comment