使用postman發送數據並構建collections執行測試


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.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import json
from tornado.escape import json_decode
from tornado.options import define, options
define("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串。

image

c.發送post請求。

選擇post方法,輸入url,在body-raw里輸入參數。注意這里默認是text格式,需要改成json格式。點擊send。

可以看到當發送的字段正確時,返回status:0

image

輸入錯誤的參數:{"operation":"get","send":"yes"},可以看到返回status:404.

image

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,查看添加的驗證條件是否通過。

image

4.生成collections

輸入完一個請求后,可以點擊save將其保存成collections,方便下次再次使用。

image

保存后可以再右側找到該collections,后期在使用時,僅需要在此Collections中找到對應的請求名,即可直接使用請求。

image

5.執行測試

點擊postman左上角runner,會彈出runner頁面。選擇一個collections,點擊start,執行完成后可以在左側查看執行結果。

image

6.分享請求

點擊collections的share,可以分享連接,點擊Export可以將collections導出成文件。

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM