標籤:輸入參數 點擊 write google export 程式 options import 返回結果
1.安裝
:https://www.getpostman.com/。直接安裝,成功後在chorme的應用程式中會多出一個Postman。如果無法在google store上直接安裝,可以下載.crx檔案手動安裝:http://chromecj.com/utilities/2015-04/423.html
2.發送請求
a.搭建伺服器。
先用tonado在本機搭一個簡易伺服器,連接埠為8000,定義兩個兩個handler,一個post方法,一個get方法。
post方法的參數為{"operation":"post","send":"yes"}
import tornado.httpserverimport tornado.ioloopimport tornado.optionsimport tornado.webimport jsonfrom tornado.escape import json_decodefrom tornado.options import define, optionsdefine("port", default=8000, help="run on the given port", type=int)class IndexHandler(tornado.web.RequestHandler): def get(self): resp = {‘status‘: 0, ‘description‘: ‘ok‘} resp = json.dumps(resp) self.write(resp)class PostHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): data = json_decode(self.request.body) if data[‘operation‘] == "post" and data[‘send‘] == "yes": resp = {‘status‘: 0, ‘description‘: ‘ok‘} else: resp = {‘status‘: 404, ‘description‘: ‘params error‘} resp = json.dumps(resp) self.write(resp) if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application(handlers=[(r"/index", IndexHandler), (r‘/test‘, PostHandler)]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
b.使用postman發送get請求。
選擇get方法,輸入url,點擊發送。發送後在Response-Body裡可以查看返回的json串。
c.發送post請求。
選擇post方法,輸入url,在body-raw裡輸入參數。注意這裡預設是text格式,需要改成json格式。點擊send。
可以看到當發送的欄位正確時,返回status:0
輸入錯誤的參數:{"operation":"get","send":"yes"},可以看到返回status:404.
3.驗證介面請求
點擊test,再點擊右側的“Response body:Contains string”,該方法可以判斷返回結果中是否含有某個值。指令碼編輯框中會顯示出驗證的具體指令碼:tests["Body matches string"] = responseBody.has("string_you_want_to_search"),修改“string_you_want_to_search”為“description”。
點擊右側的“Response body:Is equal to a string”,該方法用來判斷返回結果是否等於某個字串。
添加完驗證條件後點擊send,再點擊response-Tests,查看添加的驗證條件是否通過。
4.產生collections
輸入完一個請求後,可以點擊save將其儲存成collections,方便下次再次使用。
儲存後可以再右側找到該collections,後期在使用時,僅需要在此Collections中找到對應的請求名,即可直接使用請求。
5.執行測試
點擊postman左上方runner,會彈出runner頁面。選擇一個collections,點擊start,執行完成後可以在左側查看執行結果。
6.分享請求
點擊collections的share,可以分享串連,點擊Export可以將collections匯出成檔案。
使用postman發送資料並構建collections執行測試