postman和接口自動化測試

一、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()

執行結果:

 

 三、總結

  • postman能夠幫助咱們完成50%左右的工做,好比調試接口,導出部分代碼等
  • 使用unittest重構用例能夠幫助咱們添加斷言,提供在命令行執行的能力,很容易跟ci工具進行集成
相關文章
相關標籤/搜索