用过JUnit的朋友,可能一看就明白了。
没有用过的,估计也不会有太大问题。
好了废话不说,返回正题。
1。 怎么使用写测试函数
简单例子
JSUnitTest.prototype.testAdd = function(){
this.assertEquals("3", 1+2);
}
只需要扩展JSUnitTest.prototype就可以。(当然必须是function)
2。我在测试前由一些初始工作要做,怎么办?
扩展
JSUnitTest.prototype.setUp = function(){
// 初始代码
}
3。我要释放一些东西
扩展
JSUnitTest.prototype.tearDown = function(){
// 释放资源
}
下面我们就需要执行测试。
执行用例如下
<html>
<head>
<script tyep="text/javascript" src="jsunit.js"></script> // 应用JS文件
<script type="text/javascript">
JSUnitTest.prototype.setUp = function(){
}
// 测试ADD
JSUnitTest.prototype.testAdd = function(){
this.assertEquals("3", 1+2);
}
// 测试 MINUS
JSUnitTest.prototype.testMinus = function(){
alert(document.getElementById("hahah"

.tagName);
this.assertEquals("3", 4-1);
}
// 测试SHOW
JSUnitTest.prototype.testShow = function(){
this.assertEquals("abc", "abc"

;
}
JSUnitTest.prototype.tearDown = function(){
}
window.onload = function(){
with(new JSUnitTest()){
main();
}
}
</script>
</head>
<body>
</body>
</html>
/-----------------------------------------------------------------------------------------------------------
执行完后结果都会在页面的一个表格中显示出来。
成功用例 失败用例 错误用例 这些都会统计出来
好了下面 放上JSUnitTest的源代码,那位仁兄如果有幸使用,小弟不胜感激。 如发现bug请给小弟一个mail
ymfssd@hotmail.com 或者QQ 542639820 小弟一下。
/** JSUnitTest
* @ author ShadowKiller
* @ version 1.0
* @ date 2007-01-25
*/
function JSUnitTest(){
// the flag which store the test status
var flag = true;
function getFlag(){return flag;};
function setFlag(a){flag = a;};
// store the failure number
var failureNumber = 0;
function getFailureNumber(){return failureNumber;};
function increseFailureNumber(){failureNumber++;};
// store the success number
var successNumber = 0;
function getSuccessNumber(){return successNumber;};
function increseSuccessNumber(){successNumber++;};
// store the error number
var errorNumber = 0;
function getErrorNuber(){return errorNumber;};
function increseErrorNumber(){errorNumber++;};
// the id of the area which is used for displaying the test results
var divResultId = "JSUnitTest";
// create area which is used for displaying the test results
function createDivResult(){
var div = document.getElementById(divResultId);
if(div){
var p = div.parentNode;
p.removeChild(div);
}
div = document.createElement("div"

;
div.id = divResultId;
var table = document.createElement("table"

;
table.cellspacing = 0;
table.cellpadding = 0;
table.border = 1;
table.style.border = "1px solid #33CC99";
var thead = document.createElement("thead"

;
var tbody = document.createElement("tbody"

;
var tfoot = document.createElement("tfoot"

;
var tr = document.createElement("tr"

;
var th = document.createElement("th"

;
th.appendChild(document.createTextNode("Mehod"

);
tr.appendChild(th);
th = document.createElement("th");
th.appendChild(document.createTextNode("Success"));
tr.appendChild(th);
th = document.createElement("th");
th.appendChild(document.createTextNode("Failure"));
tr.appendChild(th);
th = document.createElement("th");
th.appendChild(document.createTextNode("Error"));
tr.appendChild(th);
thead.appendChild(tr);
tr = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = "Totle";
tr.appendChild(th);
th = document.createElement("th");
th.innerHTML = "0";
tr.appendChild(th);
th = document.createElement("th");
th.innerHTML = "0";
tr.appendChild(th);
th = document.createElement("th");
th.innerHTML = "0";
tr.appendChild(th);
tfoot.appendChild(tr);
table.appendChild(thead);
table.appendChild(tbody);
table.appendChild(tfoot);
div.appendChild(table);
document.body.appendChild(div);
}
// add the execute result
function refreshResult(o){
var tbody = document.getElementById(divResultId).firstChild.childNodes[1];
var tfoot = tbody.nextSibling;
var tr = document.createElement("tr");
var td = document.createElement("td");
td.innerHTML = o.name;
tr.appendChild(td);
if(o.success){
td = document.createElement("td");
td.style.backgroundColor = "green";
tr.appendChild(td);
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
tfoot.firstChild.childNodes[1].innerHTML = getSuccessNumber();
}else if(o.failure){
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
td = document.createElement("td");
td.style.backgroundColor = "yellow";
tr.appendChild(td);
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
tfoot.firstChild.childNodes[2].innerHTML = getFailureNumber();
}else{
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
td = document.createElement("td");
td.innerHTML = " ";
tr.appendChild(td);
td = document.createElement("td");
td.style.backgroundColor = "red";
tr.appendChild(td);
tfoot.firstChild.childNodes[3].innerHTML = getErrorNuber();
}
tbody.appendChild(tr);
};
// set test flag
this.assertEquals = function(a, b){
if(a === b){
}else{
setFlag(false);
}
};
//this.getFailure = function(){return getFailureNumber();};
//this.getSuccess = function(){return getSuccessNumber();};
//this.getResultArray = function(){return getStatusArray();};
this.main = function(){
// drawn the table for displaying the execute results
createDivResult();
// if function JSUnitTest.prototype.setUp exist, set it to this object, and execute it
if(JSUnitTest.prototype.setUp && typeof JSUnitTest.prototype.setUp == "function"){
this.setUp = JSUnitTest.prototype.setUp;
this.setUp();
delete JSUnitTest.prototype.setUp;
}
// if function JSUnitTest.prototype.tearDown exist, set it to this object, and execute it
if(JSUnitTest.prototype.tearDown && typeof JSUnitTest.prototype.tearDown == "function"){
this.tearDown = JSUnitTest.prototype.tearDown;
delete JSUnitTest.prototype.tearDown;
}
// execute all the test method
for(var o in JSUnitTest.prototype){
if(typeof JSUnitTest.prototype[o] == "function"){
try{
// execute test method
this[o]();
// if all the assertEquals method execute successfully
if(getFlag()){
// increse success number
increseSuccessNumber();
// refresh the result data
refreshResult({name: o,success:true,failure:false,error:false});
}else{
increseFailureNumber();
refreshResult({name: o,success:false,failure:true,error:false});
}
}catch(e){
// increse error number
increseErrorNumber();
refreshResult({name: o,success:false,failure:false,error:true});
continue;
}finally{
// make the execute-flag restitute
setFlag(true);
}
}
}
// set tearDwon to JSUnitTest.prototype
if(this.tearDown){
this.tearDown();
var f = this.tearDwon;
delete this.tearDown;
JSUnitTest.prototype.tearDown = f;
}
// set setUp to JSUnitTest.prototype
if(this.setUp){
var f = this.setUp;
delete this.setUp;
JSUnitTest.prototype.setUp = f;
}
};
};