ITPUB??ì3
ITPUB论坛 » WEB 2.0技术 » 类似于JUnit的Javascript测试JSUnit(自己写的)


标题: 类似于JUnit的Javascript测试JSUnit(自己写的)
离线 ShadowKiller
不会跳舞的猪


精华贴数 0
个人空间 0
技术积分 636 (2949)
社区积分 19 (7630)
注册日期 2004-9-17
论坛徽章:2
管理团队成员开发板块每日发贴之星    
      

发表于 2007-1-25 13:38 
类似于JUnit的Javascript测试JSUnit(自己写的)

用过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 = "&nbsp";
            tr.appendChild(td);
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            tfoot.firstChild.childNodes[1].innerHTML = getSuccessNumber();
        }else if(o.failure){
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            td = document.createElement("td");
            td.style.backgroundColor = "yellow";
            tr.appendChild(td);            
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            tfoot.firstChild.childNodes[2].innerHTML = getFailureNumber();               
        }else{
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);           
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            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;
        }   
    };
};


__________________
When you wanna get the most , there's no easy way out; when you ready to go and your heart left in doubt, Don't give up your faith , cause you can win, in this thing called love.Love can conquer all.
只看该作者    顶部
离线 justforregister
SAP


精华贴数 1
个人空间 10
技术积分 38458 (19)
社区积分 9928 (153)
注册日期 2005-6-10
论坛徽章:244
现任管理团队成员ITPUB元老奥运纪念徽章NBA2008季后赛纪念徽章欧洲冠军杯纪念徽章NBA常规赛纪念章
管理团队2007贡献徽章参与2007年甲骨文全球大会(中国上海)纪念ITPUB北京香山2007年会纪念徽章2008北京奥运纪念徽章:排球2008北京奥运纪念徽章:跳水2008北京奥运纪念徽章:摔跤

发表于 2007-1-25 21:36 
牛的,很有想法嘛


__________________
Go to Hell
大家一起BS以下菠菜作弊者

| EPS2008          |        35201 | 2008-08-17 22:18:17     |
| missingsky       |        22300 | 2008-08-17 22:19:17     |
| wws8875          |        10000 | 2008-08-17 22:25:54     |
| renxiang         |        23332 | 2008-08-17 22:28:15     |
只看该作者    顶部
离线 ShadowKiller
不会跳舞的猪


精华贴数 0
个人空间 0
技术积分 636 (2949)
社区积分 19 (7630)
注册日期 2004-9-17
论坛徽章:2
管理团队成员开发板块每日发贴之星    
      

发表于 2007-1-26 09:53 
呵呵  有个缺点。 就是它会把所有prototype下的函数都执行。 要改成只执行test开头,是很容易。

与人方便,与己方便么


__________________
When you wanna get the most , there's no easy way out; when you ready to go and your heart left in doubt, Don't give up your faith , cause you can win, in this thing called love.Love can conquer all.
只看该作者    顶部
离线 HorseShell
资深会员



精华贴数 0
个人空间 0
技术积分 1040 (1721)
社区积分 26 (6602)
注册日期 2007-1-26
论坛徽章:2
ITPUB新首页上线纪念徽章     
      

发表于 2007-1-26 15:26 
Re: 类似于JUnit的Javascript测试JSUnit(自己写的)

Thanks
无比感激
Thanks
无比感激
Thanks
无比感激

QUOTE:
最初由 ShadowKiller 发布
用过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 = "&nbsp";
            tr.appendChild(td);
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            tfoot.firstChild.childNodes[1].innerHTML = getSuccessNumber();
        }else if(o.failure){
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            td = document.createElement("td");
            td.style.backgroundColor = "yellow";
            tr.appendChild(td);            
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);
            tfoot.firstChild.childNodes[2].innerHTML = getFailureNumber();               
        }else{
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            tr.appendChild(td);           
            td = document.createElement("td");
            td.innerHTML = "&nbsp";
            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;
        }   
    };
};





只看该作者    顶部
离线 ShadowKiller
不会跳舞的猪


精华贴数 0
个人空间 0
技术积分 636 (2949)
社区积分 19 (7630)
注册日期 2004-9-17
论坛徽章:2
管理团队成员开发板块每日发贴之星    
      

发表于 2007-1-26 17:40 
是的。 是我写得。胡乱写得。 见笑了


__________________
When you wanna get the most , there's no easy way out; when you ready to go and your heart left in doubt, Don't give up your faith , cause you can win, in this thing called love.Love can conquer all.
只看该作者    顶部
离线 justforregister
SAP


精华贴数 1
个人空间 10
技术积分 38458 (19)
社区积分 9928 (153)
注册日期 2005-6-10
论坛徽章:244
现任管理团队成员ITPUB元老奥运纪念徽章NBA2008季后赛纪念徽章欧洲冠军杯纪念徽章NBA常规赛纪念章
管理团队2007贡献徽章参与2007年甲骨文全球大会(中国上海)纪念ITPUB北京香山2007年会纪念徽章2008北京奥运纪念徽章:排球2008北京奥运纪念徽章:跳水2008北京奥运纪念徽章:摔跤

发表于 2007-1-27 18:30 
谢谢分享,搂住高手啊


__________________
Go to Hell
大家一起BS以下菠菜作弊者

| EPS2008          |        35201 | 2008-08-17 22:18:17     |
| missingsky       |        22300 | 2008-08-17 22:19:17     |
| wws8875          |        10000 | 2008-08-17 22:25:54     |
| renxiang         |        23332 | 2008-08-17 22:28:15     |
只看该作者    顶部
离线 wdzwdz
版主



精华贴数 11
个人空间 0
技术积分 2544 (604)
社区积分 1064 (892)
注册日期 2001-12-30
论坛徽章:9
管理团队成员ITPUB元老管理团队2006纪念徽章会员2007贡献徽章会员2006贡献徽章授权会员
ITPUB新首页上线纪念徽章在线时间开发板块每日发贴之星   

发表于 2007-3-1 18:47 
不错




__________________
从事系统分析工作qq=24938558,msn=wdz123@hotmail.com我的bloghttp://blog.itpub.net/wdzwdz
只看该作者    顶部
离线 iooyoo
迷眼流金


精华贴数 0
个人空间 20
技术积分 4154 (327)
社区积分 3476 (380)
注册日期 2006-6-15
论坛徽章:20
现任管理团队成员2008年新春纪念徽章    
      

发表于 2007-3-6 08:58 
不错,有想法,能实现,牛人啊
ps: 可以把 createDIV.. 方法细化一下,一些常量提出来


__________________

使君欲醉离亭酒
酒醒离愁转有
紫禁多时虚右
苕霄留难久
一声歌掩双罗袖
日落乱山春后
犹有东城烟柳
青荫长依旧
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问