NodeJs单元测试之 API性能测试

1.下载插件

#初始化仓库
npm init -y
 
#断言库
npm install --save-dev chai 
 
#测试框架 (不包括断言库,所以需要自己加载断言库)
npm install --save-dev mocha
 
#代码覆盖率
npm install --save-dev istanbul

2.简单实现断言demo

//path:test/simple.js 
/*使用抽象相等比较(==)测试 actual 和 expected 参数之间的浅层强制相等。 NaN 是特殊处理的,如果双方都是 NaN,则视为相同。*/
const assert = require("assert");//node 内置断言门模块
const { add, mul } = require("../src/math");
assert.equal(add(2, 3), 5);
//执行 node test/simple.js 根据add方法返回值2+3=5,equal两个参数相等所以控制台不会做任何输出
 
assert.equal(add(2,4),5)
//验证一个错误的case 控制台会输出expected 6 to equal 5
//path:src/math.js  //验证文件
function getMin(arr) {
  return b - a;
}
module.exports = {
  add: (...args) => {
    return args.reduce((prev, curr) => {
      return prev + curr;
    });
  },
  mul: (...args) => {
    return args.reduce((prev, curr) => {
      return prev * curr;
    });
  },
 
  cover: (a, b) => {
    if (a > b) {
      return a - b;
    } else if (a === b) {
      return a + b;
    } else {
      return getMin(a, b);
    }
  },
};

3.基于mocha编写单元测试脚本

//path:test/mocha.js
const { should, expect, assert } = require("chai");//引入断言库
 
const { add, mul, cover } = require("../src/math");//引入测试方法
//描述
describe("#math", () => {
    //描述
  describe("add", () => {
    it("should return 5 when 2 + 3", () => {
      assert.equal(add(2, 3), 5);
    });
 
    it("should return 8 when 2 + 6", () => {
      assert.equal(add(2, 6), 8);
    });
  });
  describe("mul", () => {
    it("should return 15 when 3 * 5", () => {
      assert.equal(mul(3, 5), 15);
    });
  });
 
  describe("cover", () => {
    it("should return 2 when cover(5, 3)", () => {
      assert.equal(cover(5, 3), 2);
    });
    it("should return 4 when cover(2, 2)", () => {
      assert.equal(cover(2, 2), 4);
    });
    it("should return 2 when cover(2, 4)", () => {
      assert.equal(cover(2, 4), 2);
    });
  });
});
 
 
//更多mocha 请查看官方文档再次不做过多介绍
//https://mochajs.cn/#exclusive-tests

4.配置scripts in package.json

"scripts": {
    "test": "mocha test/mocha.js",
    //windows
    "cover":"istanbul cover node_modules/mocha/bin/_mocha test/mocha.js"
    //mac
     "cover":"istanbul _mocha test/mocha.js"
 },

5.性能测试

# npm i --save-dev benchmark
//path:test/m.js
const Benchmark=require('benchmark')
const suite = new Benchmark.Suite;
 
// add tests 添加两个测试
//1.正则表达式 2.indexOf
suite.add('RegExp#test', function() {
  /o/.test('Hello World!');
})
.add('String#indexOf', function() {
  'Hello World!'.indexOf('o') > -1;
})
// add listeners
.on('cycle', function(event) {
    //每一个事件执行完 输出测试结果
  console.log(String(event.target));
})
.on('complete', function() {
    //当执行完测试 返回执行最快的测试名称
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
//执行 node test/benchmark.js
 
//RegExp#test x 41,459,903 ops/sec ±0.93% (93 runs sampled)
//String#indexOf x 932,426,480 ops/sec ±0.41% (94 runs sampled)
//Fastest is String#indexOf
//

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取  

猜你喜欢

转载自blog.csdn.net/kk_lzvvkpj/article/details/130650002