The QUnit framework allows us to develop unit tests for code developed in JavaScript. The following video clip overviews a simple demo for using this framework.
The following is the code sample this video clip overviews. The QUnit website includes detailed documentation and code samples for using this framework.
<!DOCTYPE html>
<html>
<head>
<title>QUnit Demo</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="mylib.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script type="text/javascript">
test("the sum() function unit test",
function()
{
var numA = 3;
var numB = 4;
var result = sum(numA,numB);
equal( result, 7, "sum of 3 and 4 succeeds" );
numA = 9999999999999999999999999999999999999999;
numB = 1;
result = sum(numA,numB);
equal(result,10000000000000000000000000000000000000000,"sum of 9999999999999999999999999999999999999999 and 1 succeeds");
}
);
test("the biggest() function unit test",
function()
{
equal( biggest(2,5), 5, "getting that 5 is bigger than 2 succeeds" );
equal( biggest(9,5), 9, "getting that 9 is bigger than 5 succeeds" );
equal( biggest(9,5), 9, "getting that 9 is bigger than 5 succeeds" );
}
);
</script>
</body>
</html>
You can find the community version of the QUnit Basics course available for free personal usage at http://abelski.lifemichael.com.







