01 訂閱鏈接轉yaml文件格式檢查系統(yaml文件使用)

时间:2024-05-05 05:58:28 编辑: 来源:

使用yaml文件管理測試數據

知道ddt的基本使用方法之后,練習把之前用excel文件來維護的接口測試用例改用unittest+ddt來實現。

這里我選用yaml文件來管理接口參數,開始本來想用json,但是json無法添加注釋,可讀性不好。

下面截圖是接口文檔中的各個接口,每個接口都有一個固定的序號,所以在設計每個接口的測試數據時,以序號來區分不同接口

yaml文件內容如下,需要注意的是yaml的語法:

(1)鍵值對用冒號分割,但是冒號后需要加一個空格

(2)禁止使用tab縮進,只能使用空格鍵;縮進長度沒有限制,只要元素對齊就表示這些元素屬于一個層級

(3)字符串可以不用引號標注,也可以加引號,如果想把數字變為字符串,加引號即可

(4)使用#表示注釋

詳情可以參考博客: 買粉絲s://blog.csdn.買粉絲/vincent_hbl/article/details/75411243

2. 簡單 demo : python 讀取 yaml 文件,取出接口參數

import yaml

fp = open('../data買粉絲nfig/信息互動模塊接口.yaml', en買粉絲ding='utf-8')   #有中文字符的話,加編碼格式

                testdata = yaml.load(fp)

t = testdata['5.2.1.4']

print(t)

(1)封裝讀取yaml文件方法

handle_yaml.py

# 買粉絲ding: utf-8

# author: hmk

importyaml

importos

classHandleYaml:

    def __init__(self,file_path=None):

        if file_path:

           self.file_path = file_path

        else:

            root_dir =os.path.dirname(os.path.abspath('.'))

            # os.path.abspath('.')表示獲取當前文件所在目錄;os.path.dirname表示獲取文件所在父目錄;所以整個就是項目的所在路徑self.file_path = root_dir +'/data買粉絲nfig/信息互動模塊接口.yaml'  #獲取文件所在的相對路徑(相對整個項目)

        #elf.data = self.get_data()

    def get_data(self):

        fp =open(self.file_path, en買粉絲ding='utf-8')

        data =yaml.load(fp)

        return data

if __name__ == '__main__':

    test = HandleYaml()

    p = test.get_data()

    print(p['5.2.1.1'])

[if !vml][endif]

(2)封裝requests請求方法

[if !vml][endif]

# 買粉絲ding: utf-8

# author: Archer

importrequests

importjson

classRunMethod:

    defpost_main(self, url, data, header=None):if header is notNone:

             res =requests.post(url=url, data=data, headers=header)

        else:

            res =requests.post(url=url, data=data)

        # print(res.status_買粉絲de)

        # return res.json()

        return res    #為了方便后面斷言,這里不再對服務器響應進行json格式編碼

def get_main(self, url, data=None, header=None):if header is notNone:

            res =requests.get(url=url, params=data, headers=header)

        else:

            res =requests.get(url=url, params=data)

            print(res.status_買粉絲de)

        # return

res.json()

        return res

    def run_main(self, method, url, data=None, header=None):

    if method== 'POST':

            res =self.post_main(url, data, header)

        else:

            res =self.get_main(url, data, header)

        returnres

        # returnjson.mps(res, indent=2, sort_keys=False, ensure_ascii=False)  #使用json模塊格式化顯示結果

[if !vml][endif]

(3)一個接口測試用例

[if !vml][endif]

# 買粉絲ding: utf-8

# author: Archer

importunittest

importddt

from base.run_method importRunMethod

from utils.handle_yaml importHandleYaml

get_data = HandleYaml()  # 從yaml文件中取出該接口的參數

params = get_data.get_data()['5.2.1.4']

@ddt.ddt

classTest(unittest.TestCase):

    """加載買粉絲詳情接口"""

    defsetUp(self):

        self.url ='買粉絲://localhost:8088/ApprExclusiveInterface/api/enterpr

搜索关键词: