Kook Testjs v1.0

来源:互联网 发布:翻墙方法 知乎 编辑:程序博客网 时间:2024/05/21 11:26
源代码:
 
  1.  /**
  2.  *
  3.  *@function 测试用例
  4.  *@time 2008-12-10
  5.  *@author Kyle
  6.  *@version 1.0
  7.  */
  8.  function TestCase(){
  9.  }
  10.  /**
  11.  *@function 初始化测试用例
  12.  *@scope protected
  13.  */
  14.  TestCase.prototype.setUp=function(){};
  15.  /**
  16.  *@function 执行函数
  17.  *@scope public
  18.  */
  19.  TestCase.prototype.run=function(){
  20.      this.setUp();//运行前调用setUP()方法以初始化用例
  21.  };
  22.  TestCase.prototype.assertNotNull=function(/*Object */obj){
  23.     if(obj==null)
  24.      throw "><b><font color=red> assertNotNull()</font>></b>Object:<b><font color=red>"+obj+"</font></b>  is null ";
  25.  };
  26.  /**
  27.  *@function 用例加载器
  28.  */
  29.  function TestRunner(){}
  30.  /**
  31.  *@function 异常处理
  32.  */
  33.  TestRunner.prototype.exception=function(/*Object*/obj,/*exception function property name list*/list){
  34.      if(!list){
  35.         list=[];
  36.      }
  37.      for(var prop in obj){
  38.             /*
  39.             *如果开始包含test字符串,且是function类型
  40.             *@note 效率需要改进
  41.             */
  42.         if(prop.indexOf("test")==0&&typeof(obj[prop])=="function"){
  43.             try{
  44.                 obj[prop]();
  45.                 list.push("<div><b><font color=green>"+prop+"() is  ok</font></b><div>");
  46.             }catch(e){
  47.                      list.push("<div><B><font color=red><b>"+prop+"()</font></b>>"+e+"</font><div>");
  48.             }
  49.         }
  50.      }
  51.  
  52.      var div = document.createElement("div");
  53.      div.innerHTML=list.join("<br>");
  54.      document.body.appendChild(div); 
  55.  };
  56.  /**
  57.  *@function 用于运行用例
  58.  */
  59.  TestRunner.prototype.run=function(/*TestCase function*/testCase){
  60.      var obj=new testCase;
  61.    obj.run();
  62.    this.exception(obj,null);
  63.     
  64.  };
  65.  var Runner=new TestRunner;
  66.  
测试:

  1. var testCase1=function(){};
  2. testCase1.prototype=new TestCase;
  3. testCase1.prototype.testHelloWorld=function(){
  4.   this.assertNotNull(null);
  5. };
  6. testCase1.prototype.testHelloWorld2=function(){
  7.   this.assertNotNull(null);
  8. };
  9. testCase1.prototype.testHelloWorld3=function(){
  10.   this.assertNotNull("sdf");
  11. };
  12. testCase1.prototype.setUp=function(){
  13.     alert("this is a init");
  14. };
  15. Runner.run(testCase1);
html
  1. <script src="Test.js"></script>
  2. <body>
  3. <script src="TestCases.js"></script>
  4. </body>