700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 软件测试nodejs面试题 nodejs单元测试和性能测试

软件测试nodejs面试题 nodejs单元测试和性能测试

时间:2021-12-13 20:13:59

相关推荐

软件测试nodejs面试题 nodejs单元测试和性能测试

好算法理应正确、可读、健壮和效率高与低存储量;为保证正确性,多组输入数据测试这是必须条件;效率高和低存储这涉及了接口代码的性能测试。测试驱动开发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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。