Node.js的单元测试框架初体验

  Mocha是一个功能丰富的JavaScript测试框架,运行在node.js平台和浏览器端,使异步测试变得简单和有趣。Mocha测试是串行的,允许灵活和准确的报告,同时将未捕获的异常映射到相应的测试用例上。官网地址:https://mochajs.org ,项目代码目前托管在GitHub上 https://github.com/mochajs/mocha

安装

         npm install mocha 

 快速体验demo

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);
        });
    });
});

 运行测试

    打开控制台,在test目录同级运行mocha

 在WebStorm使用

   选择 Run——>Edit Configurations

上图设置完成并保存后,直接在Run中运行即可。如果运行出错,则在控制台显示如下,如果运行正确图标则为绿色

猜你喜欢

转载自www.cnblogs.com/hzhuxin/p/9633870.html
今日推荐