使用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

posted @ 2016-12-12 19:14  zoro_robin  Views(3104)  Comments(1Edit  收藏  举报