pytest自動化測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json
數(shù)據(jù)驅(qū)動
數(shù)據(jù)的改變從而驅(qū)動自動化測試用例的執(zhí)行,最終引起測試結(jié)果的改變。簡單說就是參數(shù)化的應(yīng)用。
測試驅(qū)動在自動化測試中的應(yīng)用場景:
- 測試步驟的數(shù)據(jù)驅(qū)動;
- 測試數(shù)據(jù)的數(shù)據(jù)驅(qū)動;
- 配置的數(shù)據(jù)驅(qū)動;
1、pytest結(jié)合數(shù)據(jù)驅(qū)動-yaml
實(shí)現(xiàn)讀yaml文件,先創(chuàng)建env.yml文件配置測試數(shù)據(jù)
工程目錄結(jié)構(gòu):
- data目錄:存放yaml文件
- dev: 127.0.0.1 #dev: 127.0.0.2 #prod: 127.0.0.3
- testcase目錄:存放測試用例文件
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("這是測試環(huán)境") # print(env) print("測試環(huán)境的ip是:", env["test"]) elif "dev" in env: print("這是開發(fā)文件") print("開發(fā)環(huán)境的ip是:", env["dev"]) # print(env)
結(jié)果示例:
2、pytest結(jié)合數(shù)據(jù)驅(qū)動-excel
常用的讀取方式有:xlrd、xlwings、pandas、openpyxl
以讀excel文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放excel數(shù)據(jù)文件
- func目錄:存放被測函數(shù)文件
def my_add(x, y): result = x + y return result
- testcase目錄:存放測試用例文件
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ū)動-csv
csv:逗號文件,以逗號分隔的string文件
讀取csv數(shù)據(jù):
- 內(nèi)置函數(shù)open()
- 內(nèi)置模塊csv
- 方法:csv.reader(iterable)
- 參數(shù):iterable,文件或列表對象
- 返回:迭代器,遍歷迭代器,每次會返回一行數(shù)據(jù)
以讀csv文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放csv數(shù)據(jù)文件
- func目錄:存放被測函數(shù)文件
def my_add(x, y): result = x + y return result
- testcase目錄:存放測試用例文件
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ū)動-json
json:js對象,是一種輕量級的數(shù)據(jù)交換格式。
json結(jié)構(gòu):
- 對象{"key":value}
- 數(shù)組[value1,value2...]
查看json文件:
- 1.pycharm
- 2.txt記事本
讀取json文件:
- 內(nèi)置函數(shù)open()
- 內(nèi)置庫json
- 方法 json.loads() json.dumps()
以讀json文件,實(shí)現(xiàn)A+B=C并斷言為例~
工程目錄結(jié)構(gòu):
data目錄:存放json數(shù)據(jù)文件
- func目錄:存放被測函數(shù)文件
def my_add(x, y): result = x + y return result
- testcase目錄:存放測試用例文件
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自動化測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json的詳細(xì)內(nèi)容,更多關(guān)于pytest測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用Anaconda創(chuàng)建虛擬環(huán)境的全過程
因?yàn)槎啻沃匦屡渲铆h(huán)境,這些命令每次都要用,每次都忘記,需要重新搜索,所以記錄這一過程,下面這篇文章主要給大家介紹了關(guān)于利用Anaconda創(chuàng)建虛擬環(huán)境的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07python操作csv格式文件之csv.DictReader()方法
這篇文章主要介紹了python操作csv格式文件之csv.DictReader()方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06如何輕松實(shí)現(xiàn)Python數(shù)組降維?
歡迎來到Python數(shù)組降維實(shí)現(xiàn)方法的指南!這里,你將探索一種神秘又強(qiáng)大的編程技術(shù),想要提升你的Python編程技巧嗎?別猶豫,跟我一起深入探索吧!2024-01-01Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的示例代碼
本篇文章主要介紹了Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Python 類屬性與實(shí)例屬性,類對象與實(shí)例對象用法分析
這篇文章主要介紹了Python 類屬性與實(shí)例屬性,類對象與實(shí)例對象用法,結(jié)合實(shí)例形式分析了java類相關(guān)的屬性、實(shí)例化、對象等相關(guān)概念與操作技巧,需要的朋友可以參考下2019-09-09Pytorch轉(zhuǎn)onnx、torchscript方式
這篇文章主要介紹了Pytorch轉(zhuǎn)onnx、torchscript方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05selenium動態(tài)數(shù)據(jù)獲取的方法實(shí)現(xiàn)
本文主要介紹了selenium動態(tài)數(shù)據(jù)獲取的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07