python接口自動(dòng)化測(cè)試數(shù)據(jù)和代碼分離解析
common中存放的是整個(gè)項(xiàng)目中公共使用的封裝方法
從工程目錄上可以看到區(qū)分
datas中專(zhuān)門(mén)存放測(cè)試數(shù)據(jù)(yml文件)
cases中專(zhuān)門(mén)集中存放測(cè)試用例...
數(shù)據(jù)分離的第一步先找到工程項(xiàng)目路徑
# -*- encoding: utf-8 -*- """ @__Software__: PyCharm @__File__: osPath.py @__Date__: 2021/6/14 21:08 """ import os # 獲取項(xiàng)目的根目錄,apiTest層 FILE = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # 獲取存放測(cè)試數(shù)據(jù)的文件夾 DATAS = os.path.join(FILE, 'datas') print(DATAS)
F:\project_gitee\Test\apiTest\datas Process finished with exit code 0
運(yùn)行結(jié)果可以清晰看到,已經(jīng)找到存放數(shù)據(jù)的文件夾路徑并拼接成功 ...
os模塊不熟悉可參考菜鳥(niǎo)教程
數(shù)據(jù)分離的第二步封裝一個(gè)讀取yml文件的函數(shù)或類(lèi)方法
這里就先寫(xiě)一個(gè)函數(shù)把
首先要先安裝yml
pip install pyaml
再導(dǎo)入包,然后再進(jìn)行封裝
# -*- encoding: utf-8 -*- """ @__Software__: PyCharm @__File__: readData.py @__Date__: 2021/6/14 21:07 """ import os import yaml from common import osPath as sp def read_yml(file): with open(file, mode='r', encoding='utf-8') as read_data: results = yaml.load(read_data, Loader=yaml.FullLoader) return results print(read_yml(os.path.join(sp.DATAS, 'test_data.yml')))
{'test_data': [[{'type': 1}, {'reason': '查詢(xún)成功!'}], [{'type': 2}, {'reason': '查詢(xún)成功!'}], [{'type': 3}, {'reason': '查詢(xún)成功!'}]]} Process finished with exit code 0
讀取yml的函數(shù)寫(xiě)完以后,要記得測(cè)試下是否滿(mǎn)足自己需要的功能;從結(jié)果來(lái)看滿(mǎn)足我目前需要功能 ...
yaml語(yǔ)法不熟悉的也可以參考菜鳥(niǎo)教程
數(shù)據(jù)分離的第三步測(cè)試用例中引入數(shù)據(jù)并運(yùn)行
# -*- encoding: utf-8 -*- """ @__Software__: PyCharm @__File__: test_example.py @__Date__: 2021/6/13 19:00 """ import os import pytest import requests from common import osPath as sp from common.readData import read_yml class TestExample: s = requests.Session() data = read_yml(os.path.join(sp.DATAS, 'test_data.yml')) @pytest.mark.parametrize("test_data, expected", data['test_data']) def test_example(self, test_data, expected): with self.s as s: url = "http://apis.juhe.cn/fapig/euro2020/schedule?key=9d0dfd9dbaf51de283ee8a88e58e218b" response = s.get(url, params=test_data) print(response.json()) assert response.json()["reason"] == expected["reason"] if __name__ == '__main__': pytest.main(["-v", "-s", "test_example"])
Launching pytest with arguments F:/project_gitee/Test/apiTest/cases/test_example.py in F:\project_gitee\Test\apiTest\cases ============================= test session starts ============================ collecting ... collected 3 items test_example.py::TestExample::test_example[test_data0-expected0] test_example.py::TestExample::test_example[test_data1-expected1] test_example.py::TestExample::test_example[test_data2-expected2] ============================== 3 passed in 0.66s ==============================
data['test_data']是字典取值,取key為test_data的value值 ...
從返回的結(jié)果可以清晰看到,3 passed,且用時(shí)0.66s ...
至此,測(cè)試數(shù)據(jù)和代碼分離完成 ...
以上就是python接口自動(dòng)化測(cè)試數(shù)據(jù)和代碼分離解析的詳細(xì)內(nèi)容,更多關(guān)于python接口自動(dòng)化測(cè)試資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。