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

录制接口生成测试集

collection添加断言

添加接口响应后执行的脚本

常用的请求后执行脚本

登录获取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

未完待续.........

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • 02 你的收获 你如果耐心的看完这篇文章,你会获得些许收获 对postman的使用有一个基(全)本(面)认知 可以...
    亭子青年阅读 29,652评论 0 18
  • Postman helps you develop APIs faster. 豪华午餐 Postman 构建、管理...
    Yuan_Jie阅读 31,031评论 36 134
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,936评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,008评论 25 708