菜鸟postman接口测试_postman 接口测试(转)
本文轉(zhuǎn)載自testerhome;
作者:xinxi1990 ;
原文鏈接:https://testerhome.com/topics/18719;
轉(zhuǎn)載以分享知識(shí)為目的,著作權(quán)歸原作者所有,如有侵權(quán),請(qǐng)聯(lián)系刪除。
postman使用
創(chuàng)建用例集
啟動(dòng)postman以后,會(huì)看到這個(gè)控制面板.
點(diǎn)擊Request是創(chuàng)建一個(gè)Request測試請(qǐng)求,但是需要?jiǎng)?chuàng)建用例集保存這個(gè)請(qǐng)求.
點(diǎn)擊Collection是創(chuàng)建一個(gè)用例集來保存測試請(qǐng)求.
創(chuàng)建Collection完成后,會(huì)在左側(cè)生成用例集文件架,每次創(chuàng)建的測試接口都要保存到用例集中.
第一個(gè)接口測試
創(chuàng)建get請(qǐng)求為例,通常需要寫url、params、headers,會(huì)把params拼接到url末尾.
點(diǎn)擊send按鈕并且請(qǐng)求成功,會(huì)展示響應(yīng)結(jié)果.
創(chuàng)建post請(qǐng)求為例,通常需要寫url、body、headers等參數(shù),body參數(shù)格式一般是form或者json格式.具體body使用那個(gè)格式,需要按照接口文件中的參數(shù).
接口斷言
點(diǎn)擊Tests編寫測試斷言
斷言響應(yīng)時(shí)間
pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200);});// 斷言響應(yīng)事件小于200ms斷言狀態(tài)碼
pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([200,202]);});// 斷言狀態(tài)碼200-202區(qū)間斷言響應(yīng)中包含某個(gè)字符串
pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("ok");});// 斷言響應(yīng)中包含"ok"斷言響應(yīng)中的字段等于某個(gè)值
pm.test("message test", function () { var jsonData = pm.response.json(); pm.expect(jsonData["message"]).to.eql("ok");});// 斷言響應(yīng)中"message" = ok"斷言響應(yīng)中的字段不等于某個(gè)值
var jsonData = JSON.parse(responseBody);tests["message不為bad"] = jsonData["message"] != "bad";// 斷言響應(yīng)中"message" != bad"斷言響應(yīng)中的列表長度
pm.test("data list test", function () { var jsonData = pm.response.json(); pm.expect(jsonData["data"].length).to.eql(41);});// 斷言響應(yīng)中"list"的字段長度斷言響應(yīng)中的列表中第幾個(gè)元素的字段值
pm.test("data list 0 test", function () { var jsonData = pm.response.json(); pm.expect(jsonData["data"][0]["time"]).to.eql("2018-11-28 17:27:41");});// 斷言響應(yīng)中"list 0的"的time字段的值json schema驗(yàn)證
tv4是postman內(nèi)置的JSON Schema驗(yàn)證庫,參考:https://geraintluff.github.io/tv4/
responseBody如下==:==
{ "errCode": 0, "errMsg": "", "data": { "id": 3210, "title": "",const customerSchema = { "type": "object", "properties": { "errCode": { "type": "integer", "minimum": 0, "maximum": 3, "minLength": 2, "maxLength": 3 }, "errMsg": {"type": "string"}, }};var customer = JSON.parse(responseBody);// console.log(customer);tests["Valid Data1"] = tv4.validate(customer, customerSchema);//驗(yàn)證json中的errCode類型是integer,并且驗(yàn)證最小值和最大值區(qū)間、驗(yàn)證長度區(qū)間以上是常用斷言方法,更多使用參考:https://learning.getpostman.com/docs/postman/scripts/test_scripts/
測試前準(zhǔn)備
發(fā)送請(qǐng)求之前往往需要準(zhǔn)備數(shù)據(jù),比如設(shè)置header中參數(shù)或者計(jì)算簽名.
使用Pre-request Script可以編寫一些準(zhǔn)備數(shù)據(jù).
在header頭中引入剛剛設(shè)置{{timestamps}}環(huán)境變量.
可以看到header中已經(jīng)填寫了時(shí)間戳參數(shù).
請(qǐng)求前編寫加密算法
var username = "test";var pwd = "123321";var base64Str = CryptoJS.enc.Utf8.parse(username+pwd);var token = CryptoJS.enc.Base64.stringify(base64Str);postman.setGlobalVariable("token",token);console.log(token);// 使用賬號(hào)+密碼的base64位加密算法加密生成的字符串
header頭中攜帶生成加密的token變量
服務(wù)端使用base64位解密
接口環(huán)境變量
接口參數(shù)化
全局變量
局部變量
使用{{}}作為變量
參數(shù)化文件
.csv文件格式,第一行是變量名,后面是具體賦值.
選擇參數(shù)化文件
接口參數(shù)傳遞
在登錄接口的響應(yīng)數(shù)據(jù)中獲取token值.
把token傳遞給第二個(gè)接口中的header頭中.
第二個(gè)接口中的header頭中已經(jīng)拿到了token.
其他常用的方法
設(shè)置環(huán)境變量
pm.environment.set("variable_key", "variable_value");設(shè)置全局變量
pm.globals.set("variable_key", "variable_value");獲取環(huán)境變量
pm.environment.get("variable_key");獲取全局變量
pm.globals.get("variable_key");清除環(huán)境變量
pm.environment.unset("variable_key");清除全局變量
pm.globals.unset("variable_key");newman使用
官方教程
https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/
安裝
npm install -g newman
運(yùn)行
簡單運(yùn)行
newman run 接口測試.postman_collection.json打印循環(huán)次數(shù)、請(qǐng)求次數(shù)、斷言次數(shù)、耗時(shí)等,但是沒有輸出文件.
循環(huán)執(zhí)行
newman run 接口測試.postman_collection.json -n 2參數(shù)化
-d是參數(shù)化文件
newman run 接口參數(shù)化測試.postman_collection.json -d 參數(shù)化數(shù)據(jù).csv報(bào)告
jenkins持續(xù)集成
在jenkins中創(chuàng)建自由風(fēng)格的job
job配置
構(gòu)建shell配置
newman run 文件路徑/接口測試.postman_collection.json--reporters cli,html,json,junit--reporter-json-export jsonOut.json--reporter-junit-export xmlOut.xml--reporter-html-export htmlOut.html構(gòu)建后報(bào)告配置參數(shù)
**/*.xml總結(jié)
以上是生活随笔為你收集整理的菜鸟postman接口测试_postman 接口测试(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 五月28学习笔记
- 下一篇: 管桩的弹性模量计算公式_桩基设计计算公式