zoukankan      html  css  js  c++  java
  • mocha测试接口类型及测试报告收集

    记录参考:

    参考文档:

    测试报告以及es6:

    http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html

    测试接口类型:

    https://blog.csdn.net/hustzw07/article/details/73920809

    参数

    之前我们写 Mocha测试用例的时候,主要用 describe(), it() 组织用例。这跟 Jasmine 风格是类似的。实际上,这只是 Mocha 支持的一种而已。

    在命令行中,有如下命令可供我们选择。

    [plain] view plain copy
     
    1. -u, --ui <name>  specify user-interface (bdd|tdd|qunit|exports)  

    默认情况下,Mocha使用 bdd 帮我们执行 describe(), it() 的用例。

    [javascript] view plain copy
     
    1. describe('#indexOf()', function(){  
    2.     it('should return -1 when not present', function(){  
    3.       [1,2,3].indexOf(4).should.equal(-1);  
    4.     });  
    5. });  

    我们也可以选择其他接口类型。比如 tdd。
    Mocha的测试接口类型指的是集中测试用例组织模式的选择,更像是为用户提供几种组织测试用例的方式。

    BDD

    BDD行为驱动开发,全名 Behavior Driven Development。
    Mocha默认的测试接口。 BDD测试接口提供 describe(), context(), it(), specify(), before(), after(), beforeEach(), 和 afterEach()几种函数,其中context函数只是describe函数的别名,specify函数也是it函数的别名。

    另外,Jasmine的测试风格就是bdd,它的特征就是使用describe,it。 对 BDD 有兴趣的可以去这个网站看下
    http://jbehave.org/introduction.html

    TDD

    TDD,测试驱动开发(Test-Driven Development)
    TDD接口提供 suite(), test(), suiteSetup(), suiteTeardown(), setup(), 和 teardown()函数,用例写法如下:

    [javascript] view plain copy
     
    1. suite('#indexOf()', function(){  
    2.        test('should return -1 when not present', function(){  
    3.            ([1,2,3].indexOf(4)).should.be.eql(-1);  
    4.        });  
    5.    });  

    tdd跟bdd区别在于,它使用suite,test,suiteSetup,suiteTeardown,setup,teardown 等函数。

    Exports

    exports 跟 node 里的模块语法很像,before, after, beforeEach, and afterEach 都是作为对象的属性,其它对象的值默认是 suite,属性是函数的话,代表是一个test。简单来说,除了钩子函数,对象是 测试集, 属性是 测试用例。

    [javascript] view plain copy
     
    1. require("should");  
    2. module.exports = {  
    3.       before: function(){  
    4.             // ...  
    5.       },  
    6.   
    7.       'Array': {  
    8.             '#indexOf()': {  
    9.               'should return -1 when not present': function(){  
    10.                 [1,2,3].indexOf(4).should.equal(-1);  
    11.               }  
    12.             }  
    13.       }  
    14. };  

    钩子函数

    从 BDD 到 TDD,describe 和 it 变变成了 suite, test。对应的钩子函数也变了。那它们的行为有没有改变呢?下面是个例子。

    [javascript] view plain copy
     
    1. var assert = require('assert');  
    2. var mocha  = require('mocha');  
    3. require("should");  
    4.   
    5. suite('Array', function(){  
    6.     suiteSetup(function(){  
    7.         console.log();  
    8.         console.log('-----------suiteSetup');  
    9.     });  
    10.     suiteTeardown(function(){  
    11.         console.log('-----------suiteTeardown');  
    12.         console.log();  
    13.     });  
    14.     setup(function(){  
    15.         console.log();  
    16.         console.log('-----------setup');  
    17.     });  
    18.     teardown(function(){  
    19.         console.log('-----------teardown');  
    20.         console.log();  
    21.     });  
    22.     test('First layer', function(){  
    23.         ([1,2,3].indexOf(4)).should.eql(-1);  
    24.     });  
    25.     suite('Second layer', function(){  
    26.         suiteSetup(function(){  
    27.             console.log();  
    28.             console.log('-----------Second layer suiteSetup');  
    29.         });  
    30.         setup(function(){  
    31.             console.log();  
    32.             console.log('-----------Second layer setup');  
    33.         });  
    34.         test('Second layer test', function(){  
    35.             ([1,2,3].indexOf(4)).should.eql(-1);  
    36.         });  
    37.     });  
    38. });  

    输出

  • 相关阅读:
    ambari之hbase数据迁移
    elasticsearch之python备份
    python之rabbitMQ篇
    python协程与异步I/O
    Python进程、线程、协程
    paramiko模块使用
    HTTP网络协议(四)
    HTTP网络协议(三)
    HTTP网络协议(二)
    HTTP网络协议(一)
  • 原文地址:https://www.cnblogs.com/yoyoblogs/p/9111454.html
Copyright © 2011-2022 走看看