为JavaScript项目写了一个极简的测试框架-EasyTest

最近在忙JavaScript的存储管理项目,为了稳定性需要一个测试框架,想起自己曾经写过,这次把老的代码从日记里翻出来,又重写了一下.发现很有效果,分分钟测出无数BUG,所以分享出来.

别人框架太重,来个特别轻和简单的.

EasyTest提供最基础(简单)的测试功能.

  1. 每种断言均可提供额外的注释
    就是告诉大家这次错误/正确 的具体含义,例子是:下图的错误存储异常测试
    总之就是灰常简单.
  2. 四种基础断言
  • IsTrue
  • IsFalse
  • NoReturn
    供声明永远不该返回的函数(比如一定在此条件下抛异常)
  • HaveException
    当然供分析异常信息的啦,详细看例子.

效果图

 //测试驱动的代码
 /*
 用途:构建了一个测试框架
 0.2
 2018年12月13日
 新版设计:利用了class就简单很多了...
 - 增加异常捕获分析,以及更清晰的指定异常信息
 - 增加不可能返回函数的捕获(用来处理一定会发生异常,而不会返回的情况,见异常测试例子)

  本框架暂不提供异常捕获.但是提供异常信息的分析.需要用户自行捕获,eg:在最外侧提供捕获.
 -------------------------
 老版:
 原理:通过闭包,将申请次数信息:即第几次测试的信息保存在函数中。
 特别之处:两个函数共用一个变量。
 */

 class EasyTest {
     constructor(testName) {
         if (!testName) {
             throw ("需要给测试一个名字")
         }
         this.m_testName = testName;
         this.m_allTestNumber = 0;
         this.m_errTestNumber = 0;
     }
     /*
     @param b{bool}打印正确还是打印错误
     @introduction{string}注释
     */
     InfoLog(b, introduction) {
         this.m_allTestNumber++;

         if (!introduction) {
             introduction = "";
         } else {
             introduction = ":" + introduction
         }
         if (!b) {
             console.error("测试" + ":" + this.m_testName + ":" + this.m_allTestNumber + "不通过" + introduction);
             this.m_errTestNumber++;
         } else {
             console.log("测试" + ":" + this.m_testName + ":" + this.m_allTestNumber + " PASS" + introduction);

         }
     }
     /*
     @param b{bool}要测试的值
     @param introduction{string}[可选]此项测试的介绍注释
     */
     IsTrue(b, introduction) {
         this.InfoLog(b, introduction)
     }
     /*
     @param b{bool}要测试的值
     @param introduction{string}[可选]此项测试的介绍注释
     */
     IsFalse(b, introduction) {
         this.InfoLog(!b, introduction)
     }
     /*
     本方法一经调用即报错,用于一定不返回的函数,eg:假设其必定异常的函数
     @param b{bool}任意值
     @param introduction{string}[可选]此项测试的介绍注释
   
     */
     NoReturn(b, introduction) {
         this.InfoLog(false, introduction)
     }
     /*
       @param error{string}从catch接收到的异常值
       @param exceptionText{string}期待的异常值,如不关心该值使用""
       @param introduction{string}[可选]此项测试的介绍注释
  
     */
     HaveException(error, exceptionText, introduction) {
         if (exceptionText) {
             this.InfoLog(error === exceptionText, introduction)
         } else {
             this.InfoLog(true, introduction);
         }
     }
     getResult() {
         let text = "";
         if (this.m_errTestNumber) {
             text = "共有" + this.m_errTestNumber + "/" + this.m_allTestNumber + "个没有通过测试";
         } else {
             text = "全部通过测试";
         }
         console.log("小结:" + this.m_testName + ":" + text);
     }
 }
 function exceptionTest() {
     throw ("exception")
 }
 let test = new EasyTest("存储测试")
 test.IsTrue(false, "错误存储");
 test.IsTrue(true);
 test.IsFalse(true);
 test.IsFalse(false);
 try {
    test.NoReturn(exceptionTest())
} catch (error) {
    test.HaveException(error, "exception", "异常测试");
 }
  test.getResult();

?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值