教你如何打造屬於自己的接口測試框架(超詳細版,持續更新中)

前言:對於接口自動化想必很多人都不陌生,有的人喜歡用jmeter+ant+jenkins,我現在公司的兩個項目接口在線巡檢就是用這個方式搭建的,對比用python編寫的框架,優點是不用怎麼寫代碼,個人認爲使用哪種方法看項目使用的技術棧,是否滿足所有接口的測試需求,從長遠考慮,如果團隊有代碼基礎。還是直接寫接口測試代碼比較靈活,這裏不講述jmeter接口自動化,教你打造屬於自己的接口自動化框架,適合有python基礎的同學,下面這個報告是用的jmeter做的接口自動化
在這裏插入圖片描述

一、接口測試框架Requests
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
官方文檔:https://requests.readthedocs.io/en/master/
在這裏插入圖片描述
在這裏插入圖片描述
如果大家想要練習接口,可以用這個開源的項目搭建一套服務https://github.com/postmanlabs/httpbin
也可以直接訪問已經搭建好的現成的環境或者自己用公司的項目
https://httpbin.org

在這裏插入圖片描述
在這裏插入圖片描述

import requests


class TestDemo:
    def test1(self):
        r = requests.get("https://httpbin.org/get")
        print(r.status_code, r.json())
        assert r.status_code == 200

在這裏插入圖片描述
二、 接口請求構造GET/POST/PUT/HEAD
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

def test_query(self):
        payload = {"name": "BruceLee", "age": 18}
        r = requests.get("https://httpbin.org/get", params=payload)
        print(r.status_code, r.text)
        assert r.status_code == 200

在這裏插入圖片描述
在這裏插入圖片描述

    def test_form_post(self):
        payload = {"name": "BruceLee", "age": 18}
        r = requests.post("https://httpbin.org/post", data=payload)
        print(r.status_code, r.text)
        assert r.status_code == 200

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
post可以支持哪一些參數呢,我們可以看post的源碼得出結果
在這裏插入圖片描述
在這裏插入圖片描述
三、接口測試斷言
在這裏插入圖片描述
在這裏插入圖片描述

    def test_header(self):
        header = {"name": "BruceLee"}
        r = requests.post("https://httpbin.org/post", headers=header)
        print(r.status_code, r.json())
        assert r.json()['headers']['Name'] == 'BruceLee'

在這裏插入圖片描述
斷言我們還可以用pip install pytest-assume(添加斷言,同一條測試用例每一條的斷言都會反饋出來)
參考:https://blog.csdn.net/weixin_44275820/article/details/105169871
我們斷言的內容需要根據實際的項目添加,最基本的斷言就是斷言status_code

四、 JSON/XML請求
在這裏插入圖片描述

    def test_json_post(self):
        payload = {"name": "BruceLee", "age": 18}
        r = requests.post("https://httpbin.org/post", json=payload)
        print(r.status_code, r.text)
        assert r.status_code == 200

在這裏插入圖片描述

在這裏插入圖片描述
request沒有幫我們封裝xml,所以這裏需要我們自己封裝一個,對xml做特殊處理,請求的時候我們將header = {“Content-Type”: “application/xml”}定義好

# python3字符串換行,在右邊加個反斜槓
def test_xml_post(self):
        xml = '<?xml version="1.0" encoding = "UTF-8"?>' \
              '<COM>' \
              '<REQ name="深圳-測試">' \
              '<USER_ID>yoyoketang</USER_ID>' \
              '<COMMODITY_ID>123456</COMMODITY_ID>' \
              '<SESSION_ID>absbnmasbnfmasbm1213</SESSION_ID>' \
              '</REQ>' \
              '</COM>'
        header = {"Content-Type": "application/xml"}
        # 遇到編碼報錯時候,對body進行encode
        r = requests.post("https://httpbin.org/post", data=xml.encode("utf-8"), headers=header)
        print(r.status_code, r.text)
        assert r.status_code == 200

在這裏插入圖片描述
xml格式的數據寫到代碼裏面,不太直觀,後期維護也不方便,可以把xml格式數據單獨拿出來寫到一個文件裏,再用open函數去讀取

import os
 
curpath = os.path.dirname(os.path.realpath(__file__))
xmlpath = os.path.join(curpath, "body1_xml")
 
with open(xmlpath, encoding="utf-8") as fp:
    body = fp.read()
 
 
# 讀取xml文件
print(body)

具體細節可以參考:https://blog.csdn.net/xgh1951/article/details/80981580

在這裏插入圖片描述
在這裏插入圖片描述
五、JSON/XML響應斷言
在這裏插入圖片描述

 def test_json(self):
        header = {"name": "BruceLee"}
        r = requests.post("https://httpbin.org/post", headers=header)
        print(r.status_code, r.json())
        assert r.json()['headers']['Name'] == 'BruceLee'
        #如果對象裏面是一個數組,數組裏面有多個對象,記得加上對應數組對象的索引

在這裏插入圖片描述
我們還可以用python的jsonpath第三方庫:https://pypi.org/project/jsonpath-rw/
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
官方文檔:http://hamcrest.org

六、Schema斷言
這個斷言主要針對於返回json字段類型判斷,因爲在版本迭代過程中,開發不小心將字段類型定義錯了,而測試過程中沒有發現
在這裏插入圖片描述
官方文檔:http://json-schema.org、https://jsonschema.net/home
在這裏插入圖片描述
在這裏插入圖片描述
七、Header Cookie處理
在這裏插入圖片描述

待更新,預計下週更新完

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章