postman+jenkins 接口压测

序言

本文章是讲postman+jenkins的实践,前提是已学习了postman和jenkins的使用
实践步骤是:
1.前期准备
2.编写jenkins构建执行的shell
3.录制接口请求生成collections
4.对collection中的有些接口添加断言
5.postman导出json文件,jenkins执行构建
6.分析输出报告

前期准备

1.安装newman命令(postman的命令行版),然后预先准备命令格式

命令格式:

newman run   -n <执行次数> -r(--reporters)   cli,json,html  <执行文件path>  --reporter-html-export <html报告文件路径>
--reporter-json-export   <json报告文件路径>

(如果想不打印执行的断言添加:–reporter-cli-no-assertions )

2.在谷歌浏览器上安装postman interceptor 插件
使用技巧:
(1)过滤url,填写只抓取的网站的url主干(如:baidu)
(2)抓取的请求在postman 的history中

jenkins的构建shell

设置当前JOB的环境变量

postmanjson=C:\\Users\\Administrator\\Desktop\\CI\\newman\\test.json
reporter=C:\\Users\\Administrator\\Desktop\\CI\\repoter\\
requestnum=20
processnum=8

构建的shell

#!python.exe
import os
from multiprocessing import Pool

def long_time_task(count):
    print('Run task %s (%s)...' % (count, os.getpid()))
    reporterhtml= '%reporter%'+str(count)+'.html'
    shell='newman run -n %requestnum% -r cli,html %postmanjson% --reporter-cli-no-assertions  --reporter-html-export '+reporterhtml
    os.system(shell)
   
if __name__=='__main__':
    print(os.getenv("BUILD_NUMBER"))
    print('Parent process %s.' % os.getpid())
    p = Pool(int(os.getenv("processnum"))-1)
    for i in range(int(os.getenv("processnum"))):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

生成collections

《postman+jenkins 接口压测》 录制接口生成测试集

collection添加断言

《postman+jenkins 接口压测》 添加接口响应后执行的脚本

常用的请求后执行脚本

登录获取session:

var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("sessionId",jsonData.result);

断言验证接口返回是否正常

tests["Status code is 200"] = responseCode.code === 200;
tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
var jsonData = JSON.parse(responseBody);
tests["have result "]=jsonData.hasOwnProperty("error")!==true;
tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000;

导出json文件执行jenkins

未完待续………

    原文作者:Valkyrie0
    原文地址: https://www.jianshu.com/p/02549f72f3f3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞