python做自动化控制postman_使用postman+newman+python做接口自动化测试

postman是一款API调试工具,可用于测试接口,相类似的工具还有jmeter、soupUI。通过postman+newman+python可以批量运行调试接口,达到自动化测试的效果。

1、PostMan安装

共有两种方式,一种是chrome浏览器上的插件,一种是postman客户端。我使用的是postman客户端。

1)在Chrome浏览器怎么安装Postman

https://www.cnblogs.com/mafly/p/postman.html

2)安装Postman客户端

a、下载软件https://www.getpostman.com/apps

b、安装

2、使用

1)发送请求、查看响应

2)环境变量、全局变量

环境变量:只作用于设置的环境

设置环境变量:pm.environment.set("variable_key", "variable_value");

获取环境变量:pm.environment.get("variable_key");

全局变量:作用于所有环境

设置全局变量:pm.globals.set("variable_key", "variable_value");

获取全局变量:pm.globals.get("variable_key");

使用例子:

var data=JSON.parse(responseBody);

var act=data.data.accessToken;

postman.setGlobalVariable("accessToken", act);

postman.setGlobalVariable("userId", data.data.userId);

postman.setGlobalVariable("refreshToken", data.data.refreshToken);

var afterUrl="?access_token="+act+"&userId="+data.data.userId;

pm.globals.set("afterUrl", afterUrl);

console.log(afterUrl)

使用变量:

在使用的变量地方用 {{variableName}}代替

具体查看文档:https://www.getpostman.com/docs/postman/environments_and_globals/variables

3)设置断言

tests["Your test nickName"] = data.data.nickName === "2589" //响应内容 nickName =2589

pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

}); //返回为200

var responseJSON=JSON.parse(responseBody);

tests['response matches the data posted'] = (responseJSON.data && responseJSON.data.length === 10);

//返回data数据共10条

4)调试console

需要在postman客户端,点击 view->show postman console 调出

在test或 Pre-request Script中写脚本打印出有疑问的值

console.log(variableName); 之后运行请求

5)collection

需要在postman客户端,点击collection->Runner ,运行

具体查看文档:https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

6)具体使用如下图

1431699-20180630151309147-1132036517.png\

7)导出json文件

1431699-20180630151715128-116278099.png

2、newman安装

官方帮助文档地址:https://www.npmjs.com/package/newman

1)需要安装nodejs,并配置好环境

2)打开控制台,运行:npm install -g newman

3)校验是否安装成功,运行:newman --version

Newman 执行脚本

Newman在3版本后做了比较大的改动,但是运行命令越来越简单如下:

newman run [options]

run 后面跟上要执行的json文件或者URL(json 和 URL 都由postman导出生成),再后面跟一些参数,例如环境变量,测试报告,接口请求超时时间等等。最后给两个完整的例子做参考:

newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html

3、使用python脚本执行newman

# coding=utf-8

import time

import os

class postmanApiTest:

#运行postman生成报告

#通过newman

def postman(self):

jSONfname = 'D:/htmlOut' + time.strftime('%Y-%m-%d', time.gmtime())+'.html'

# cmd = 'newman run ‪D:/Buddy_Test_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-html-export '+jSONfname

cmd='newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html'

os.system(cmd)

print('------------------------------------------------------------')

print(jSONfname)

if os.path.isfile(jSONfname):

return jSONfname

print(jSONfname)

else:

return False

if __name__ == '__main__':

a=postmanApiTest()

a.postman()

4、最终生成报告如下:

1431699-20180630152505790-1758954186.png

1431699-20180630152631524-291189896.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值