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ù)的文件夾路徑并拼接成功 ...
數(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': '查詢成功!'}], [{'type': 2}, {'reason': '查詢成功!'}], [{'type': 3}, {'reason': '查詢成功!'}]]}
Process finished with exit code 0
讀取yml的函數(shù)寫(xiě)完以后,要記得測(cè)試下是否滿足自己需要的功能;從結(jié)果來(lái)看滿足我目前需要功能 ...
數(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)文章!
- Python3+Requests+Excel完整接口自動(dòng)化測(cè)試框架的實(shí)現(xiàn)
- 利用Python如何實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)的接口自動(dòng)化測(cè)試
- python接口自動(dòng)化測(cè)試之接口數(shù)據(jù)依賴的實(shí)現(xiàn)方法
- Python http接口自動(dòng)化測(cè)試框架實(shí)現(xiàn)方法示例
- Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)
- Python+unittest+requests+excel實(shí)現(xiàn)接口自動(dòng)化測(cè)試框架
- Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼
- python使用pytest接口自動(dòng)化測(cè)試的使用
- Python接口自動(dòng)化測(cè)試框架運(yùn)行原理及流程
- Python+Requests+PyTest+Excel+Allure?接口自動(dòng)化測(cè)試實(shí)戰(zhàn)
- Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情
- python使用requests+excel進(jìn)行接口自動(dòng)化測(cè)試的實(shí)現(xiàn)
相關(guān)文章
pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法
今天小編就為大家分享一篇pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決
這篇文章主要介紹了python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總
這篇文章主要為大家介紹了科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Python使用PIL將圖片或GIF轉(zhuǎn)為字符畫(huà)的方法詳解
PIL是專(zhuān)為Python語(yǔ)言設(shè)計(jì)的圖像處理庫(kù),它涵蓋了廣泛的圖像處理功能,如圖像的加載、保存、編輯,以及執(zhí)行多樣化的圖像處理任務(wù),本文給大家介紹了Python使用PIL將圖片或GIF轉(zhuǎn)為字符畫(huà)的方法,需要的朋友可以參考下2025-03-03
Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)及numpy統(tǒng)計(jì)函數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)的實(shí)例代碼,給大家介紹了Python數(shù)據(jù)分析numpy統(tǒng)計(jì)函數(shù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
python讀取nc數(shù)據(jù)并繪圖的方法實(shí)例
最近項(xiàng)目中需要處理和分析NC數(shù)據(jù),所以下面這篇文章主要給大家介紹了關(guān)于python讀取nc數(shù)據(jù)并繪圖的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

