美文网首页
nodejs单元测试和性能测试

nodejs单元测试和性能测试

作者: stonejianbu | 来源:发表于2020-12-15 17:08 被阅读0次

好算法理应正确、可读、健壮和效率高与低存储量;为保证正确性,多组输入数据测试这是必须条件;效率高和低存储这涉及了接口代码的性能测试。测试驱动开发TDD以实现有效和高效的算法代码。

一、安装配置

确保已安装nodejs环境

mkdir my-project
cd my-project
npm init -y
npm install -D mocha  // 代码测试,模块化写测试代码,批量测试,格式化输出测试结果
npm install -D axios  // 模拟客户端发送请求,获取服务器数据, 辅助测试web api接口
npm install -D benchmark // 查看接口性能
npm install -D autocannon // 压力测试,查看API响应性能


// 修改项目的project.json文件中参数test,可用`npm test`运行测试
"scripts": {
  "test": "mocha"
}

单元测试

在项目目录下新建test目录,然后创建相应测试代码文件,如app.test.js

var assert = require('assert').strict;
const http = require('axios');

// 简单的封装assert.equal
function test(response, obj, status=200) {
    assert.equal(JSON.stringify(response.data), JSON.stringify(obj))
    assert.equal(response.status, status)
}
describe('My Test Module', function() {
  describe('#indexOf1()', function() {
    it('should return -1 when the value is not present', function() {
      // 被测试代码部分
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
    it('should return 0 when the value is not present', function() {
      assert.equal([4, 2, 3, 1].indexOf(4), 0);
    });
  });
  describe('GET /', function() {
    it('200 {hello: "world"}', async function() {
      // http接口测试
        const response = await http({
            method: 'get',
            url: 'http://127.0.0.1:3000'
        })
        test(response, {hello: 'world'}, 200)
    });
  });
});

// 执行测试
npm test

/*测试结果
My Test Module
    #indexOf1()
      √ should return -1 when the value is not present
      √ should return 0 when the value is not present
    GET /
      √ 200 {hello: "world"}
*/

三、性能测试

可参考文档 代码接口性能测试benchmark和 web接口压力测试autocannon

  • 本地代码
var suite = new Benchmark.Suite;
 
// add tests
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 });
 
// logs:
// => RegExp#test x 4,161,532 +-0.99% (59 cycles)
// => String#indexOf x 6,139,623 +-1.00% (131 cycles)
// => Fastest is String#indexOf
  • web API
'use strict'

const autocannon = require('autocannon');
async function start () {
  const result = await autocannon({
    url: 'http://localhost:3000',
    method: 'GET', // default
    connections: 10, //default
    pipelining: 1, // default
    duration: 10 // default
    // body: '{"hello": "world"}',
  })
  console.log(result)
}
start();

相关文章

  • nodejs单元测试和性能测试

    好算法理应正确、可读、健壮和效率高与低存储量;为保证正确性,多组输入数据测试这是必须条件;效率高和低存储这涉及了接...

  • nodejs深入学(11)测试

    前言 测试一般包括单元测试、性能测试、安全测试、功能测试,这篇文章主要讲解单元测试和性能测试。 单元测试 在开始讲...

  • nodejs mock数据 单元测试

    nodejs 单元测试 nodejs koa 框架实现Java 中mock+junit类似的单元测试。 需要用到的...

  • iOS中单元测试中的性能测试(二)

    之前说过单元测试的一些基本测试发放和使用,这一篇文章说下单元测试中的性能测试。在没有单元测试之前,测试性能的方法是...

  • 技术博客汇总

    对微信公众号接口的单元测试 Github与Travis CI测试与部署 Django单元测试 NodeJs单元测试...

  • Karma - 01 介绍

    测试一般包含几个方面: 单元测试 功能测试 性能测试 安全测试 开发人员最常的是单元测试和功能测试。单元测试是对一...

  • 5.自动化测试工具

    单元测试工具 JUnit:Java单元测试 NUnit CppUnit:C++单元测试 性能测试工具 LoadRu...

  • 单元测试

    单元测试 线程 性能优化

  • golang基准测试Benchmark和Jmeter压测实践

    golang的性能测试Benchmark go test 自带有三种测试: 功能测试(单元测试) 基准测试 (性能...

  • NodeJs单元测试

    本博客简要介绍NodeJs如何进行单元测试 单元测试类型 TDD:测试驱动开发TDD的原理是在开发功能代码之前,先...

网友评论

      本文标题:nodejs单元测试和性能测试

      本文链接:https://www.haomeiwen.com/subject/qdvwgktx.html