zoukankan      html  css  js  c++  java
  • postman和接口自动化测试

    1、postman测试接口

    (1)首先安装postman

    下载地址:https://www.getpostman.com/apps

    选择对应版本下载,然后安装即可

    (2)使用postman发送请求

    比如以下这个请求例子:

    使用postman发送请求:

    这样我们可以看到请求返回的内容是否正确。

    如果想要把这个做成接口自动化测试,如何处理,请看下一点。

    2、接口自动化

    (1)安装python

    (2)安装requests

    (3)安装unittest

    (4)安装pycharm

    (5)通过postman获得初步代码

    我们可以看到初步代码如下:

    import requests
    
    url = "https://www.v2ex.com/api/nodes/show.json"
    
    querystring = {"name":"python"}
    
    payload = ""
    headers = {
        'content-type': "application/json",
        'cache-control': "no-cache",
        'postman-token': "7590b5ad-8b0a-8336-1a24-b4564a50dba4"
        }
    
    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    
    print(response.text)

    (6)把代码在pycharm里面重构成我们需要的测试用例代码:

    import requests
    import unittest
    
    class V2exTestCase(unittest.TestCase):
    
        def test_node_api(self):
            url = "https://www.v2ex.com/api/nodes/show.json"
            querystring = {"name": "python"}
            response = requests.request("GET", url, params=querystring).json()
            self.assertEqual("python",response['name'])
            self.assertEqual(90,response['id'])
    
    if __name__=="__main__":
        unittest.main()

    执行结果:

     3、总结

    • postman可以帮助我们完成50%左右的工作,比如调试接口,导出部分代码等
    • 使用unittest重构用例可以帮助我们添加断言,提供在命令行执行的能力,很容易跟ci工具进行集成
  • 相关阅读:
    八皇后问题
    窃贼问题
    汉诺塔算法
    HDOJ(HDU) 1570 A C
    HttpClient4.2 Fluent API学习
    CUDA编程(六)进一步并行
    动态规划-迷宫-百度之星-Labyrinth
    hdu 5288||2015多校联合第一场1001题
    [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
    java 内存数据存储
  • 原文地址:https://www.cnblogs.com/pachongshangdexuebi/p/8524416.html
Copyright © 2011-2022 走看看