一、postman测试接口node
(1)首先安装postmanpython
下载地址:https://www.getpostman.com/appsjson
选择对应版本下载,而后安装便可api
(2)使用postman发送请求app
好比如下这个请求例子:工具
使用postman发送请求:post
这样咱们能够看到请求返回的内容是否正确。测试
若是想要把这个作成接口自动化测试,如何处理,请看下一点。url
二、接口自动化spa
(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()
执行结果:
三、总结