一、pytest數據驅動框架
pytest是一種使用Python編寫的測試框架,其主要特點是排除依賴關係、可讀性強、可擴展性高。其中,pytest數據驅動框架是其最為重要的特性之一。它基於參數化,通過將測試數據與測試代碼分離,使得測試用例更加簡潔、易於維護。
在pytest中,使用參數化的方式實現數據驅動測試,通過在測試函數上使用裝飾器@pytest.mark.parametrize來傳入參數。下面是一個簡單的示例:
import pytest @pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",54)]) def test_eval(test_input,expected): assert eval(test_input) == expected
在上面的示例中,參數test_input和expected分別對應測試數據和期望值。參數化的方式使得我們可以一次性執行多組數據驅動測試,節省了我們的時間和精力。
二、pytest的數據驅動
pytest的數據驅動主要基於參數化實現,可以通過多種方式傳遞測試數據,如列表、元組、字典、CSV、JSON、Excel等,也可以通過fixture動態生成測試數據。另外,pytest可以結合unittest中的數據驅動方式進行使用。
在pytest中,使用參數化裝飾器將測試數據和測試函數進行綁定,實現數據驅動測試。下面是一個使用列表傳遞測試數據的示例:
import pytest testdata = [("3+5",8),("2+4",6),("6*9",54)] @pytest.mark.parametrize("test_input,expected",testdata) def test_eval1(test_input,expected): assert eval(test_input) == expected
在上面的示例中,將測試數據定義在testdata變數中,使用參數化裝飾器將testdata傳遞給測試函數。pytest還可以從文件中讀取測試數據,下面是一個從excel文件中讀取測試數據的示例:
import pytest import xlrd def read_excel(): datas = [] wb = xlrd.open_workbook("testdata.xlsx") sheet = wb.sheet_by_index(0) for i in range(1,sheet.nrows): data = {"test_input":sheet.cell_value(i,0),"expected":sheet.cell_value(i,1)} datas.append(data) return datas @pytest.mark.parametrize("test_input,expected",read_excel()) def test_eval2(test_input,expected): assert eval(test_input) == expected
在上面的示例中,定義了一個read_excel函數,用於從testdata.xlsx文件中讀取測試數據。使用參數化裝飾器將測試數據傳遞給測試函數,其中test_input和expected分別對應excel文件中的列。
三、pytest數據驅動傳入json
pytest也支持從json文件中讀取測試數據,下面是一個使用json文件傳遞測試數據的示例:
import pytest import json def read_json(): with open("testdata.json","r",encoding="utf-8") as f: datas = json.load(f) return [(data["test_input"],data["expected"]) for data in datas] @pytest.mark.parametrize("test_input,expected",read_json()) def test_eval3(test_input,expected): assert eval(test_input) == expected
在上面的示例中,使用read_json函數從testdata.json文件中讀取測試數據,返回一個列表。在測試函數中,使用參數化裝飾器將測試數據傳遞給測試函數。
四、pytest數據驅動怎麼讀
pytest支持從多種文件中讀取測試數據,如CSV、JSON、Excel等。其中,從CSV文件中讀取測試數據的方式與從Excel文件中讀取測試數據的方式類似。下面是一個使用CSV文件傳遞測試數據的示例:
import pytest import csv def read_csv(): datas = [] with open("testdata.csv","r",encoding="utf-8") as f: data = csv.reader(f) next(data) for row in data: datas.append((row[0],int(row[1]))) return datas @pytest.mark.parametrize("test_input,expected",read_csv()) def test_eval4(test_input,expected): assert eval(test_input) == expected
在上面的示例中,使用read_csv函數從testdata.csv文件中讀取測試數據,將測試數據傳遞給測試函數使用參數化裝飾器。
五、pytest數據驅動及多線程
pytest支持使用pytest-xdist插件進行多線程測試,將測試用例分發到多個測試節點並行執行,提高測試效率。下面是一個使用pytest-xdist插件進行多線程測試的示例:
import pytest @pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",54)]) def test_eval5(test_input,expected): assert eval(test_input) == expected @pytest.mark.parametrize("test_input,expected",[("7+3",10),("5+8",13),("6*5",30)]) def test_eval6(test_input,expected): assert eval(test_input) == expected
在上面的示例中,針對不同的測試用例,可以在不同的測試函數中進行測試,使用-x命令行參數指定多線程測試時的進程數。
六、pytest數據驅動csv
pytest支持從CSV文件中讀取測試數據進行測試。下面是一個從CSV文件中讀取測試數據的示例:
import pytest import csv def read_csv(): datas = [] with open("testdata.csv","r",encoding="utf-8") as f: data = csv.reader(f) next(data) for row in data: datas.append((row[0],int(row[1]))) return datas @pytest.mark.parametrize("test_input,expected",read_csv()) def test_eval7(test_input,expected): assert eval(test_input) == expected
在上面的示例中,定義了一個read_csv函數,用於從testdata.csv文件中讀取測試數據。使用參數化裝飾器將測試數據傳遞給測試函數,其中test_input和expected分別對應CSV文件中的列。
七、pytest框架
pytest是一個基於Python的測試框架,具有簡單、靈活、可擴展的特點。它可以與其他測試框架集成使用,並支持多種數據驅動方式,使我們在測試的過程中更加方便快捷。
八、pytest和unittest的區別
相比於unittest,pytest具有以下優點:
1.只有一個測試文件;
2.更為高級的fixtures和mock功率;
3.更為簡潔靈活的測試代碼。
在使用數據驅動方面,pytest能夠直接使用裝飾器來實現參數化,而unittest則使用的是數據驅動的TestCases和TestSuite。
九、pytest中文文檔
pytest中文文檔詳細介紹了pytest的使用方法和各種函數、命令行參數等的詳細說明,對於pytest的學習和使用非常有幫助。可以通過以下鏈接查看:
https://chenglei.github.io/posts/a-quick-guide-to-pytest-chinese.html
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190042.html