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工具进行集成
原文地址:https://www.cnblogs.com/pachongshangdexuebi/p/8524416.html