Nodejs 测试框架Mocha的简单介绍

2018-10-31  本文已影响0人  咆哮小狮子

声明

本人也在不断的学习和积累中,文章中有不足和误导的地方还请见谅,可以给我留言指正。希望和大家共同进步,共建和谐学习环境。

测试的重要性

  测试?哥是萌萌的开发,为什么要干这种粗活?我想有不少的同学和我的想法是一样的,测试真的有必要吗?答案是有必要!代码写完了之后,多少是有bug的,首先测试就能帮助我们提前查出bug,减少之后的返工次数。就算我们写的代码没有bug,可以正常运行,测试的第二个好处就来了,就是可以有效的提升我们代码的效率,尤其是我们使用一些测试的框架,可以改善我们的代码书写习惯。要是还是觉得测试没用,你可以想想大牛为什么要开发这个,肯定是有一定道理的。

Mocha 是什么?

  Mocha官网是这样解释的Mocha是一个在Node.js和浏览器上运行的功能丰富的JavaScript测试框架,使异步测试变得简单有趣。Mocha测试以串行方式运行,允许灵活准确的报告,同时将未捕获的异常映射到正确的测试用例。在GitHub托管
  好像有点儿看不懂,那就跟着官网上的教程一点儿点儿的往下看吧!

安装

NPM全局安装

      $ npm install --global mocha

为项目单独安装

      $ npm install --save-dev mocha

入门

      $ mkdir test
      $ $EDITOR test/test.js

在test.js 中写上

  var assert = require('assert');
    describe('Array', function() {
        describe('#indexOf()', function() {
            it('should return -1 when the value is not present', function() {
                 assert.equal([1,2,3].indexOf(4), -1);
             });
        });
     });

直接输入mocha查看测试结果

     $ mocha
     Array
        #indexOf()
          ✓ should return -1 when the value is not present
       1 passing (6ms)

  这个是判定测试用例是否通过,默认下可以用nodejs的assert库,与此同时,Mocha支持我们使用不同的断言库,现在可以支持下面的断言库,每个断言库的用法有一些差异,自己可以参考相应的文档。
  1 should.js BDD style shown throughout these docs (BDD模式,本文档用的都是这个断定库)
  2 better-assert c-style self-documenting assert()(C-模型下的断定库)
  3 expect.js expect() style assertions (expect模式的断定库)
  4 unexpected the extensible BDD assertion toolkit
  5 chai expect(), assert() and should style assertions

详解describe和it

describe 块称为"测试套件"(test suite),表示一组相关的测试。它是一个函数,第一个参数是测试套件的名称("#indexOf()"),第二个参数是一个实际执行的函数。

it 块称为"测试用例"(test case),表示一个单独的测试,是测试的最小单位。它也是一个函数,第一个参数是测试用例的名称("should return -1 when the value is not present"),第二个参数是一个实际执行的函数。

测试脚本里面应该包括一个或多个describe块,每个describe块应该包括一个或多个it块。

异步测试

当遇到异步函数时,就需要用到异步测试,只有done()函数执行完毕后,该测试用例才算完成

     describe('User', function() {
        describe('#save()', function() {
            it('should save without error', function(done) {
                  var user = new User('Luna');
                  user.save(function(err) {
                      if (err) done(err);
                      else done();  // 通知Mocha测试结束
                  });
            });
        });
    });

Hooks(钩子)

其默认“BDD”式接口,mocha提供钩before(),after(),beforeEach(),和afterEach()。这些应该用于设置前提条件并在测试后进行清理。

describe('hooks', function() {

  before(function() {
    // runs before all tests in this block
  });

  after(function() {
    // runs after all tests in this block
  });

  beforeEach(function() {
    // runs before each test in this block
  });

  afterEach(function() {
    // runs after each test in this block
  });

  // test cases
});

Exclusive Tests (排它测试)

排它测试就是允许一个测试集合或者测试用例,只有一个被执行,其他都被跳过。如下面测试用例集合:

describe('Array', function() {
  describe.only('#indexOf()', function() {
      // ...
  });
   // 测试集合不会被执行
    describe('#ingored()', function() {
      // ...
    });
});

Inclusive Tests(包含测试)

与only函数相反,skip函数,将会让mocha系统无视当前的测试用例集合或者测试用例,所有被skip的测试用例将被报告为Pending。

describe('Array', function() {
   //该测试用例会被ingore掉 
  describe.skip('#indexOf()', function() {
    // ...
  });
   // 该测试会被执行
   describe('#indexOf()', function() {
    // ...
  });
});
describe('Array', function() {
  describe('#indexOf()', function() {
     // 测试用例会被ingore掉
    it.skip('should return -1 unless present', function() {
      // ...
    });
     // 测试用例会被执行
    it('should return the index when present', function() {
      // ...
    });
  });
});

ARROW FUNCTIONS(箭头函数)

不鼓励将箭头函数(“lambdas”)传递给Mocha。Lambdas词法绑定this,无法访问Mocha上下文。例如,以下代码将失败:

describe('my suite', () => {
  it('my test', () => {
    // should set the timeout of this test to 1000 ms; instead will fail
    this.timeout(1000);
    assert.ok(true);
  });
});

总结

我只是介绍了几种常见的测试方法,还有很多的测试方法,有兴趣的可以去看Mocha官网 查看,我也会不断的更新此文档。

参考

Mocha官网
leoleocs
阮一峰

上一篇 下一篇

猜你喜欢

热点阅读