QUnit是一个基于JQuery的单元测试Unit Testing 框架。虽然是基于JQuery但用来测试纯Javascript代码。
  用来运行Javascript单元测试用例的html页面是这样的:

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit test runner</title>
<link rel="stylesheet" href="lib/qunit-1.10.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="lib/qunit-1.10.0.js"></script>
<!--test code goes here-->
</body>
</html>

  假设我们有如下简单的javascript代码simpleMath.js,实现基本的数学操作,阶乘,平均数。

 

SimpleMath = function() {
};
SimpleMath.prototype.getFactorial = function (number) {
if (number < 0) {
throw new Error("There is no factorial for negative numbers");
}
else if (number == 1 || number == 0) {
// If number <= 1 then number! = 1.
return 1;
} else {
// If number > 1 then number! = number * (number-1)!
return number * this.getFactorial(number-1);
}
}
SimpleMath.prototype.signum = function (number) {
if (number > 0)  {
return 1;
} else if (number == 0) {
return 0;
} else {
return -1;
}
}
SimpleMath.prototype.average = function (number1, number2) {
return (number1 + number2) / 2;
}