测试:

  • assert模块; //node自带

    1. var assert = require('assert');
    2.  
    3. var now = Date.now();
    4. console.log(now);
    5. assert.ok(now % 2 == 0);
    6.  
    7. ----------------------------------------
    8. var request = require('superagent');
    9. var assert = require('assert');
    10.  
    11. request.get('http://localhost:3000')
    12. .send({q: 'bieber'})
    13. .end(function (res) {
    14. //断言判断响应状态码是否正确
    15. assert.ok(200 == res.status);
    16. //断言关键字是否存在
    17. assert.ok(~res.text.toLowerCase().indexOf('bieber'));
    18. //断言列表项是否存在
    19. assert.ok(~res.text.indexOf('<li>'));
    20. });
  • ecpect.js,优化assert的代码书写方式; API:
    • ok:断言是否为真:

      1. expect(1).to.be.ok();
      2. expect(true).to.be.ok();
      3. expect({}).to.be.ok();
      4. expect(0).to.not.be,ok();
    • be/equal: ===

      1. expect(1).to.be(1);
      2. expect(NaN).not.to.equal(NaN);
      3. expect(1).not.to.be(true);
    • eql:断言非严格相等,支持对象

      1. expect({a: 'b'}).to.eql({a: 'b'});
      2. expect(1).to.eql('1');
    • a/an:断言所属属性类型,支持数组的instanceof
      1. expect(5).to.be.a('number');
      2. expect([]).to.be.an('array');
      3. expect([]).to.be.an('object');
      4. //constructor
      5. expect(5).to.be.a('Number');
      6. expect([]).to.be.an('Array');
      7. expect(tobi).to.be.a(Ferrect); //instanceof
    • match:断言字符串是否匹配一段正则表达式
      1. expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
    • contain:断言字符串是否包含另一个字符串;
      1. expect([1,2]).to.contain(1);
      2. expect('hello world').to.contain('world');
    • length:断言数组长度;
      1. expect([]).to.have.length(0);
      2. expect([1,2,3]).to.have.length(3);
    • empty:断言数组是否为空;
      1. expect([]).to.be.empty();
      2. expect([1,2,3]).to.not.be.empty();
    • property:断言某个自身属性/值是否存在;
      1. expect(window).to.have.property('expect');
      2. expect(window).to.have.property('expect',expect);
      3. expect({a: 'b'}).to.have.property('a');
    • key/keys:断言键是否存在,支持only修饰符;
      1. expect({a: 'b'}).to.have.key('a');
      2. expect({a: 'b', c: 'd'}).to.only.have.keys('a', 'c');
      3. expect({a: 'b', c: 'd'}).to.only.have.keys(['a'.'c']);
      4. expect({a: 'b', c: 'd'}).to.not.only.have.keys('a');
    • throwException:断言Function在调用时是否会抛出异常;
      1. expect(fn).to.throwException();
      2. expect(fn2).to.not.throwException();
    • within:断言数组是否在某一个区间内;
      1. expect(1).to.be.within(0,Infinity);
    • greaterThan/above:   >
      1. expect(3).to,be.above(0);
      2. expect(5).to.be.greaterThan(3);
    • lessThan/below: <
      1. expect(0).to.be.below(3);
      2. expect(1).to.be.lessThan(3);

Moncha: 测试框架

  • 例子:

    • test.js

      1. describe('a topic', function () {
      2. it('should test something', function () {
      3.  
      4. });
      5. describe('anthor topic', function () {
      6. it('should test something else', function () {
      7.  
      8. })
      9. })
      10. });
    • 运行:mocha test.js   ;报告列表形式:  mocha -R list test.js
  • 测试异步代码:Mocha默认在一个测试用例执行之后立即执行另一个;但有时候希望延缓下一个测试用例的执行;
    1. it('should not know', function (done) {
    2. setTimeout(function () {
    3. assert.ok(1 == 1);
    4. done();
    5. }, 100);
    6. });

      如果一个测试用例中有许多异步操作,可以添加一个计数器:

    1. it('should complete three requests', function (done) {
    2. var total = 3;
    3. request.get('http://localhost:3000/1', function (res) {
    4. if(200 != res.status) throw new Error('Request error'); --total || done();
    5. });
    6. request.get('http://localhost:3000/2', function (res) {
    7. if(200 != res.status) throw new Error('Request error'); --total || done();
    8. });
    9. request.get('http://localhost:3000/3', function (res) {
    10. if(200 != res.status) throw new Error('Request error'); --total || done();
    11. });
    12. })
  • BDD风格: 前面的测试例子风格为BDD(行为驱动开发);
  • TDD风格: 测试驱动开发,组织方式是使用测试集(suit)和测试(test);每个测试集都有setup和teardowm函数,这些方法会在测试集中的测试执行前执行,为了避免代码重复已经最大限度使得测试之间相互独立;
    1. suite('net', function () {
    2. suite('Stream', function () {
    3. var client;
    4. suiteSetup(function () {
    5. client = net.connect(3000, 'localhost');
    6. });
    7.  
    8. test('connect event', function (done) {
    9. client.on('connect', done);
    10. });
    11.  
    12. test('receiving data', function (done) {
    13. client.write('');
    14. client.once('data', done);
    15. });
    16.  
    17. suiteTeardown( function () {
    18. client.end();
    19. })
    20. })
    21. })
  • export风格:使用node模块系统来输出测试;每个export的键都表示测试集,嵌套的测试集可以用子对象来表示
    1. exports.Array = {
    2. '#indecOf()' : {
    3. 'should return -1 when the value is not present' : function () {},
    4. 'should return the correct index when the value is present' : function () {}
    5. }
    6. }

在浏览器端使用Mocha:  例子

node相关--测试的更多相关文章

  1. Rocket - diplomacy - Node相关类

    https://mp.weixin.qq.com/s/BvK3He3GWon8ywG8Jdmcsg   介绍Node相关的类.   ​​   1. BaseNode   BaseNode是所有节点类的 ...

  2. MHA环境搭建【3】node相关依赖的解决

    mha的node软件包依赖于perl-DBD-Mysql 这个包,我之前有遇到过用yum安装perl-DBD-MySQL,安装完成后不能正常使用的情况,所以这里选择源码编译安装: perl5.10.1 ...

  3. node相关的精典材料

    node.js电子书 了不起的Node.js 深入浅出Node.js node.js入门经典 node.js开发指南 node.js相关优秀博文 官网 Infoq深入浅出Node.js系列(进阶必读) ...

  4. node压力测试

    压力测试 ab测试(ApacheBench); 介绍: 这是apache提供的压测工具; 使用: 启动node服务; 我用的XAMPP,进入bin文件夹,打开命令行,执行下面命令: // -n: 总请 ...

  5. 添加 node mocha 测试模块

    1.mocha  支持TDD 和 BDD两种测试风格 2.引用assert模块  此模块是node的原生模块,实现断言的功能,作用是声明预期的结果必须满足 3.mocha测试用例中可以使用第三方测试库 ...

  6. Node负载能力测试

    需求很简单,就是提供一个服务接口收集端上传来的日志文件并保存,要求能承受的QPS为5000. 以前从来都没考虑过Node服务的负载能力,用 koa + co-busboy 接受上传文件请求并用 fs ...

  7. node相关--代码共享

    代码共享问题: 是否值得在两个环境中运行同一份代码: //看项目 依赖的API是否在两个环境中都有或有替代: 浏览器提供的标准API:XMLHttpRequest.WebSocket.DOM.canv ...

  8. linux开发node相关的工具

    epel-release yum install epel-release node yum install nodejs mongodb 安装mongodb服务器端 yum install mong ...

  9. Node相关参考资料

    参考资料: [玩转Nodejs日志管理log4js]http://blog.fens.me/nodejs-log4js/ [dependencies与devDependencies之间的区别]http ...

随机推荐

  1. C++ Primer Plus第6版18个重点笔记

    下面是我看<C++ Primer Plus>第6版这本书后所做的笔记,作为备忘录便于以后复习. 笔记部分 C++的const比C语言#define更好的原因? 首先,它能够明确指定类型,有 ...

  2. loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination

    1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...

  3. 15 条实用 Linux/Unix 磁带管理命令

    导读 磁带设备应只用于定期的文件归档或将数据从一台服务器传送至另一台.通常磁带设备与 Unix 机器连接,用 mt 或 mtx 控制.强烈建议您将所有的数据同时备份到磁盘(也许是云中)和磁带设备中. ...

  4. A PHP extension for Facebook's RocksDB

    A PHP extension for Facebook's RocksDB 31 commits 2 branches 0 releases 2 contributors C++ 90.5% C 8 ...

  5. thinkphp 前台html调用函数 格式化输出

    仅仅是输出变量并不能满足模板输出的需要,内置模板引擎支持对模板变量使用调节器和格式化功能,其实也就是提供函数支持,并支持多个函数同时使用.用于模板标签的函数可以是PHP内置函数或者是用户自定义函数,和 ...

  6. catalog、scheme区别

    按照SQL标准的解释,在SQL环境下Catalog和Schema都属于抽象概念,可以把它们理解为一个容器或者数据库对象命名空间中的一个层次,主要用来解决命名冲突问题.从概念上说,一个数据库系统包含多个 ...

  7. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  8. sqlcmd

    使用sqlcmd可以在批处理脚本中执行SQL.虽然这个命令的参数很多,但幸运的是,我们不需要全部理解,在这里简要介绍以下几个: { -U login_id [ -P password ] } | –E ...

  9. elk+redis分布式分析nginx日志

    一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...

  10. JS控制DIV隐藏显示

    转载自:http://blog.sina.com.cn/s/blog_6c3a67be0100ldbe.html JS控制DIV隐藏显示 一,需求描述: 现在有3个DIV块,3个超链接,需要点击一个链 ...