PostMan接口測試用例自動轉成Python的測試腳本
在使用PosMan做服務端的自動化測試中,我們可以把測試用例加到一個Collection中,但是隨著測試用例越來越多,以及工作的需求,我們需要把PostMan中的測試用例需要遷移到腳本的方式實現,平常的遷移思路是我們在腳本里面把之前的接口測試用例重新實現,這樣相對而言它的成本是比較高的,特別是涉及的自動化測試用例特別多的時候。我們可以把Collection里面的測試用例導入出來,它是JSON的文件,然后我們解析這些JSON文件,讓它自動的轉成Python測試代碼,從而輕松的完成這樣的一個過程。被測試的API代碼如下:
#!/usr/bin/env python #!coding:utf-8 from flask import Flask,jsonify from flask_restful import Api,Resource app=Flask(__name__) api=Api(app) class LoginView(Resource): def get(self): return {'status':0,'msg':'ok','data':'this is a login page'} def post(self): parser=reqparse.RequestParser() parser.add_argument('username', type=str, required=True, help='用戶名不能為空') parser.add_argument('password',type=str,required=True,help='賬戶密碼不能為空') parser.add_argument('age',type=int,help='年齡必須為正正數') parser.add_argument('sex',type=str,help='性別只能是男或者女',choices=['女','男']) args=parser.parse_args() return jsonify(args) api.add_resource(LoginView,'/login',endpoint='login') if __name__ == '__main__': app.run(debug=True)
在PostMan里面創(chuàng)建Collection名稱login,里面的接口測試用例具體如下:
在PostMan里面導出該Collection,命名為login.json,login.json文件的內容為:
{ "info": { "_postman_id": "982a3108-6710-4a71-aaf8-e62a00d1813c", "name": "login", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "校驗用戶名不能為空", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": [] }, { "name": "校驗密碼不能為空", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": [] }, { "name": "校驗性別參數不是男或者女", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"asdf\",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": [] }, { "name": "校驗年齡不是正整數", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":\"rrest\"\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": [] }, { "name": "校驗登錄成功", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":\"18\"\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": [] } ], "protocolProfileBehavior": {} }
本質上而言,它就是一個JSON文件,使用Python文件對它進行反序列化處理成字典數據類型,然后操作字典,這個過程相對來說不難,具體實現的代碼如下:
#!/usr/bin/env python #!coding:utf-8 import requests import json import pytest def operationJson(): '''對login.json文件進行處理''' return json.load(open('login.json','r'))['item'] @pytest.mark.parametrize('datas',operationJson()) def test_api_login(datas): '''登錄API的校驗測試''' r=requests.request( method=datas['request']['method'], url=datas['request']['url']['raw'], json=json.loads(datas['request']['body']['raw'])) print(json.dumps(r.json(),ensure_ascii=False)) if __name__ == '__main__': pytest.main(["-s","-v","test_login.py"])
使用Pytest框架的參數化幾行代碼就可以搞定了,當然斷言需要單獨的加。這個過程的思路其實非常簡單,就是對JSON的文件處理成字典,然后利用Pytest框架的參數化來循環(huán)處理。當然可以把JSON文件簡單的添加下斷言,就更加智能化,添加的內容添加到response的里面內容,對login.json都在里面添加下驗證點,完善后的文件內容為:
{ "info": { "_postman_id": "982a3108-6710-4a71-aaf8-e62a00d1813c", "name": "login", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "校驗用戶名不能為空", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": { "message": { "username": "用戶名不能為空" } } }, { "name": "校驗密碼不能為空", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": { "message": { "password": "賬戶密碼不能為空" } } }, { "name": "校驗性別參數不是男或者女", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"asdf\",\n\t\"age\":18\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": { "message": { "sex": "性別只能是男或者女" } } }, { "name": "校驗年齡不是正整數", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":\"rrest\"\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": { "message": { "age": "年齡必須為正正數" } } }, { "name": "校驗登錄成功", "request": { "method": "POST", "header": [ { "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" } ], "body": { "mode": "raw", "raw": "{\n\t\"username\":\"wuya\",\n\t\"password\":\"admin\",\n\t\"sex\":\"男\(zhòng)",\n\t\"age\":\"18\"\n}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:5000/login", "protocol": "http", "host": [ "localhost" ], "port": "5000", "path": [ "login" ] } }, "response": { "age": 18, "password": "admin", "sex": "男", "username": "wuya" } } ], "protocolProfileBehavior": {} }
繼續(xù)完善測試代碼,增加接口的斷言,完善后的代碼如下:
#!/usr/bin/env python #!coding:utf-8 import requests import json import pytest def operationJson(): '''對login.json文件進行處理''' return json.load(open('login.json','r'))['item'] @pytest.mark.parametrize('datas',operationJson()) def test_api_login(datas): '''登錄API的校驗測試''' # print(type(datas['response'])) r=requests.request( method=datas['request']['method'], url=datas['request']['url']['raw'], json=json.loads(datas['request']['body']['raw'])) assert r.json()==datas['response'] if __name__ == '__main__': pytest.main(["-s","-v","test_login.py"])
執(zhí)行如上的測試代碼,見如下圖展示的執(zhí)行結果信息:
依據如上,很輕松的實現了PostMan里面的接口測試用例自動的轉成了Python的測試腳本,而且?guī)Я藬嘌缘男畔ⅰ?/p>
到此這篇關于PostMan接口測試用例自動轉成Python的測試腳本的文章就介紹到這了,更多相關PostMan自動生成Python測試腳本內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用git?config?--global設置用戶名和郵件問題
這篇文章主要介紹了使用git?config?--global設置用戶名和郵件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05反向傳播BP學習算法Gradient?Descent的推導過程
這篇文章主要為大家介紹了反向傳播BP學習算法-Gradient?Descent的推導過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05