pytest自動(dòng)化測(cè)試數(shù)據(jù)驅(qū)動(dòng)yaml/excel/csv/json
數(shù)據(jù)驅(qū)動(dòng)
數(shù)據(jù)的改變從而驅(qū)動(dòng)自動(dòng)化測(cè)試用例的執(zhí)行,最終引起測(cè)試結(jié)果的改變。簡(jiǎn)單說(shuō)就是參數(shù)化的應(yīng)用。
測(cè)試驅(qū)動(dòng)在自動(dòng)化測(cè)試中的應(yīng)用場(chǎng)景:
- 測(cè)試步驟的數(shù)據(jù)驅(qū)動(dòng);
- 測(cè)試數(shù)據(jù)的數(shù)據(jù)驅(qū)動(dòng);
- 配置的數(shù)據(jù)驅(qū)動(dòng);
1、pytest結(jié)合數(shù)據(jù)驅(qū)動(dòng)-yaml
實(shí)現(xiàn)讀yaml文件,先創(chuàng)建env.yml文件配置測(cè)試數(shù)據(jù)
工程目錄結(jié)構(gòu):
- data目錄:存放yaml文件
- dev: 127.0.0.1 #dev: 127.0.0.2 #prod: 127.0.0.3
- testcase目錄:存放測(cè)試用例文件
import pytest
import yaml
class TestYaml:
@pytest.mark.parametrize("env", yaml.safe_load(open("./env.yml")))
def test_yaml(self, env):
if "test" in env:
print("這是測(cè)試環(huán)境")
# print(env)
print("測(cè)試環(huán)境的ip是:", env["test"])
elif "dev" in env:
print("這是開(kāi)發(fā)文件")
print("開(kāi)發(fā)環(huán)境的ip是:", env["dev"])
# print(env)
結(jié)果示例:

2、pytest結(jié)合數(shù)據(jù)驅(qū)動(dòng)-excel
常用的讀取方式有:xlrd、xlwings、pandas、openpyxl
以讀excel文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放excel數(shù)據(jù)文件

- func目錄:存放被測(cè)函數(shù)文件
def my_add(x, y):
result = x + y
return result
- testcase目錄:存放測(cè)試用例文件
import openpyxl
import pytest
from test_pytest.read_excel.func.operation import my_add
def test_get_excel():
"""
解析excel數(shù)據(jù)
:return: [[1,1,2],[3,6,9],[100,200,300]]
"""
book = openpyxl.load_workbook('../data/param.xlsx')
sheet = book.active
cells = sheet["A1":"C3"]
print(cells)
values = []
for row in sheet:
data = []
for cell in row:
data.append(cell.value)
values.append(data)
print(values)
return values
class TestWithExcel:
@pytest.mark.parametrize('x,y,expected', test_get_excel())
def test_add(self, x, y, expected):
assert my_add(int(x), int(y)) == int(expected)
3、pyetst結(jié)合數(shù)據(jù)驅(qū)動(dòng)-csv
csv:逗號(hào)文件,以逗號(hào)分隔的string文件
讀取csv數(shù)據(jù):
- 內(nèi)置函數(shù)open()
- 內(nèi)置模塊csv
- 方法:csv.reader(iterable)
- 參數(shù):iterable,文件或列表對(duì)象
- 返回:迭代器,遍歷迭代器,每次會(huì)返回一行數(shù)據(jù)
以讀csv文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放csv數(shù)據(jù)文件

- func目錄:存放被測(cè)函數(shù)文件
def my_add(x, y):
result = x + y
return result
- testcase目錄:存放測(cè)試用例文件
import csv
import pytest
from test_pytest.read_csv.func.operation import my_add
def test_get_csv():
"""
解析csv文件
:return:
"""
with open('../data/params.csv') as file:
raw = csv.reader(file)
data = []
for line in raw:
data.append(line)
print(data)
return data
class TestWithCsv:
@pytest.mark.parametrize('x,y,expected', test_get_csv())
def test_add(self, x, y, expected):
assert my_add(int(x), int(y)) == int(expected)
4、pytest結(jié)合數(shù)據(jù)驅(qū)動(dòng)-json
json:js對(duì)象,是一種輕量級(jí)的數(shù)據(jù)交換格式。
json結(jié)構(gòu):
- 對(duì)象{"key":value}
- 數(shù)組[value1,value2...]
查看json文件:
- 1.pycharm
- 2.txt記事本
讀取json文件:
- 內(nèi)置函數(shù)open()
- 內(nèi)置庫(kù)json
- 方法 json.loads() json.dumps()
以讀json文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放json數(shù)據(jù)文件

- func目錄:存放被測(cè)函數(shù)文件
def my_add(x, y):
result = x + y
return result
- testcase目錄:存放測(cè)試用例文件
import json
import pytest
from test_pytest.read_json.func.operation import my_add
def test_get_json():
"""
解析json數(shù)據(jù)
:return: [[1,1,2],[3,6,9],[100,200,300]]
"""
with open('../data/params.json', 'r') as file:
data = json.loads(file.read())
print(list(data.values()))
return list(data.values())
class TestWithJson:
@pytest.mark.parametrize('x,y,expected', test_get_json())
def test_add(self, x, y, expected):
assert my_add(int(x), int(y)) == int(expected)以上就是pytest自動(dòng)化測(cè)試數(shù)據(jù)驅(qū)動(dòng)yaml/excel/csv/json的詳細(xì)內(nèi)容,更多關(guān)于pytest測(cè)試數(shù)據(jù)驅(qū)動(dòng)yaml/excel/csv/json的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Pytest 自動(dòng)化測(cè)試框架的使用
- pytest接口自動(dòng)化測(cè)試框架搭建的全過(guò)程
- python+pytest自動(dòng)化測(cè)試函數(shù)測(cè)試類測(cè)試方法的封裝
- Pytest+Yaml+Excel?接口自動(dòng)化測(cè)試框架的實(shí)現(xiàn)示例
- 淺談基于Pytest框架的自動(dòng)化測(cè)試開(kāi)發(fā)實(shí)踐
- Python自動(dòng)化測(cè)試框架pytest的詳解安裝與運(yùn)行
- 自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹
- python使用pytest接口自動(dòng)化測(cè)試的使用
- Pytest接口自動(dòng)化測(cè)試框架搭建模板
- 詳解如何使用Pytest進(jìn)行自動(dòng)化測(cè)試
- Pytest自動(dòng)化測(cè)試的具體使用
相關(guān)文章
Python爬蟲(chóng) scrapy框架爬取某招聘網(wǎng)存入mongodb解析
這篇文章主要介紹了Python爬蟲(chóng) scrapy框架爬取某招聘網(wǎng)存入mongodb解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別
這篇文章主要介紹了關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別,queue隊(duì)列中的put()或者get()方法中都提供了timeout參數(shù),利用這個(gè)參數(shù)可以有效解決上述消除不能消費(fèi)和線程一直阻塞問(wèn)題,需要的朋友可以參考下2023-05-05
Pandas數(shù)據(jù)結(jié)構(gòu)詳細(xì)說(shuō)明及如何創(chuàng)建Series,DataFrame對(duì)象方法
本篇文章中,我們主要側(cè)重于介紹Pandas數(shù)據(jù)結(jié)構(gòu)本身的特性,以及如何創(chuàng)建一個(gè)Series或者DataFrame數(shù)據(jù)對(duì)象,并填入一些數(shù)據(jù)2021-10-10
Python實(shí)現(xiàn)的爬蟲(chóng)功能代碼
這篇文章主要介紹了Python實(shí)現(xiàn)的爬蟲(chóng)功能,涉及Python使用urllib2、BeautifulSoup模塊實(shí)現(xiàn)網(wǎng)頁(yè)源碼的獲取、解析等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Pycharm使用遠(yuǎn)程linux服務(wù)器conda/python環(huán)境在本地運(yùn)行的方法(圖解))
這篇文章主要介紹了Pycharm使用遠(yuǎn)程linux服務(wù)器conda/python環(huán)境在本地運(yùn)行的方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
復(fù)習(xí)Python中的字符串知識(shí)點(diǎn)
這篇文章主要介紹了Python中字符串的一些知識(shí)點(diǎn),來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04

