当前位置:首页 > Windows程序 > 正文

API测试-Super Test

2021-03-25 Windows程序

Super Test:基本的HTTP请求,返回判断

Chai:对返回的结果进行判断

grunt:集成jenkins

grunt-mocha-test:grunt任务

Jenkins环境配制

使用Jenkins调度mochaTest任务,并配制结果输出相关 

执行环境配制

多个环境之间的自动切换:export NODE_ENV=dev && cd api-test && grunt

PS:若需要指定地址执行时,仅需要把local修改需要的目标地址即可

小样例

测试:访问时,返回的状态码是否为200(成功)

修改endpoints.js中local地址为:http:

修改Gruntfile.js中src为:test/module/demo.js

在module目录下新创建文件demo.js

1 var config = require(‘../config/endpoints‘), 2 request = require(‘supertest‘)(config.host[config.env]); 3 4 5 describe(‘测试访问baidu首页.‘, function() { 6 it(‘返回状态码为200.‘, function(done) { 7 request.get(‘/‘) 8 .expect(200) 9 .end(done); 10 }); 11 12 });

进入到api-test目录,,运行grunt

? api-test git:(master) ? grunt Running "mochaTest:test" (mochaTest) task 测试访问baidu首页. ? 返回状态码为200. (958ms) 1 passing (964ms) Done, without errors.

 测试(失败):访问时,返回的状态码是否为201

 修改demo.js文件

1 var config = require(‘../config/endpoints‘), 2 request = require(‘supertest‘)(config.host[config.env]); 3 4 5 describe(‘测试访问baidu首页.‘, function() { 6 it(‘返回状态码为200.‘, function(done) { 7 request.get(‘/‘) 8 //此处失败 9 .expect(201) 10 .end(done); 11 }); 12 13 });

执行测试:grunt

? api-test git:(master) ? grunt Running "mochaTest:test" (mochaTest) task 测试访问baidu首页. 1) 返回状态码为200. 0 passing (2s) 1 failing 1) 测试访问baidu首页. 返回状态码为200.: Error: expected 201 "Created", got 200 "OK" Warning: Task "mochaTest:test" failed. Use --force to continue. Aborted due to warnings.

分析:expect(201)为期待的状态码为201,但实际返回的状态码为200。此处可为测试的验证点

强化

一次执行N个js文件

修改Gruntfile.js中src即可

设置请求的header

request.get(‘/‘) .set(‘Content-Type‘,‘application/json‘)

设置post请求的数据

1 request 2 .post(‘/post/data‘) 3 .send({ 4 "A": 1, 5 "B": 2 6 })

对expect进行调整:如预期返回body中totalCount为6

.expect(function(res){ //返回中totalCount为6 if (!(res.body.totalCount.should.equal(6))) throw new Error("总数不对"); })

总结-测试规划

一个js文件针对单个接口请求进行测试

一个it仅测试一个接口功能

按文件夹来划分功能模块,按js文件来划分API请求

API测试-Super Test

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67341.html