测试:

  • assert模块; //node自带

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

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

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

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

Moncha: 测试框架

  • 例子:

    • test.js

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

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

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

在浏览器端使用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. Stanford机器学习---第七讲. 机器学习系统设计

    原文:http://blog.csdn.net/abcjennifer/article/details/7834256 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  2. C#开发实例 键盘篇

    键盘的操作控制: 键盘和鼠标一样是重要输入设备的一部分.开发过程中,会涉及到很多的键盘操作控制. 2.1获取键盘信息 ①获取组合键 Windows中有很多默认的组合键,如Ctrl+v,Ctrl+A.本 ...

  3. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  4. puppet 安装

    yum源配置 1. wget http://ftp.kaist.ac.kr/fedora//epel/6/i386/epel-release-6-8.noarch.rpm 2. yum list | ...

  5. HDOJ 1026 dfs路径保存

    #include<cstdio> #include<cstring> #include<cmath> ][]; #define inf 0xffffff int n ...

  6. 【Markdown】notepad++ 支持 markdown语法、预览

    Notepad++中支持Markdown   最近在学习Markdown语言的使用,很想在XP主机上使用Markdown的离线编辑器,但MarkdownPad.作业部分的离线客户端都不能再XP上运行, ...

  7. 【Other】千字文 硬笔 楷书 字帖

    <千字文>是我国最优秀的一篇训蒙教材,用一千个汉字勾划出一部完整的中国文化史的基本轮廓,代表了中国传统教育启蒙阶段的最高水平.<千字文>通篇首尾连贯,音韵谐美,读起来朗朗上口, ...

  8. Continuous Subarray Sum

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  9. 73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)

    [本文链接] http://www.cnblogs.com/hellogiser/p/swap-odd-even-bits.html [分析] 假定一个数字是8位数,设为ABCDEFGH ABCDEF ...

  10. codeforces A. Dima and Continuous Line 解题报告

    题目链接:http://codeforces.com/problemset/problem/358/A 题目意思:在横坐标上给出n个不同的点,需要把前一个点跟后一个点(这两个点的顺序是紧挨着的)用一个 ...