pytest的參數(shù)化方式
- pytest.fixture()方式進(jìn)行參數(shù)化,fixture裝飾的函數(shù)可以作為參數(shù)傳入其他函數(shù)
- conftest.py 文件中存放參數(shù)化函數(shù)屯断,可作用于模塊內(nèi)的所有測(cè)試用例
- pytest.mark.parametrize()方式進(jìn)行參數(shù)化
本節(jié)測(cè)試依然以is_leap_year.py方法作為測(cè)試目標(biāo):
def is_leap_year(year):
# 先判斷year是不是整型
if isinstance(year, int) is not True:
raise TypeError("傳入的參數(shù)不是整數(shù)")
elif year == 0:
raise ValueError("公元元年是從公元一年開始V逞荨!")
elif abs(year) != year:
raise ValueError("傳入的參數(shù)不是正整數(shù)")
elif (year % 4 ==0 and year % 100 != 0) or year % 400 == 0:
print("%d年是閏年" % year)
return True
else:
print("%d年不是閏年" % year)
return False
pytest.fixture()
fixture是pytest的閃光點(diǎn)丸相,在pytest中fixture的功能很多灭忠,本節(jié)主要介紹用fixture的參數(shù)化功能。
- pytest.fixture()中傳入的參數(shù)為list涕蜂,用例執(zhí)行時(shí)映琳,遍歷list中的值,每傳入一次值黍瞧,則相當(dāng)于執(zhí)行一次用例原杂。
- ps:@pytest.fixture()裝飾的函數(shù)中,傳入了一個(gè)參數(shù)為request年局,試試改成其他的會(huì)出現(xiàn)什么情況咸产。
-
這里的測(cè)試數(shù)據(jù)是直接存在list中的脑溢,能否存入json文件或者xml文件再進(jìn)行讀取轉(zhuǎn)換為list呢?
fixture_param.png
測(cè)試數(shù)據(jù)和用例分離
- 參數(shù)化數(shù)據(jù)和用例怎么進(jìn)行分離呢?可以采用conftest.py文件存儲(chǔ)參數(shù)化數(shù)據(jù)和函數(shù)验庙,模塊下的用例執(zhí)行時(shí)社牲,會(huì)自動(dòng)讀取conftest.py文件中的數(shù)據(jù)
# conftest.py 記住 他叫conftest.py
import pytest
# 準(zhǔn)備測(cè)試數(shù)據(jù)
is_leap = [4, 40, 400, 800, 1996, 2996]
is_not_leap = [1, 100, 500, 1000, 1999, 3000]
is_valueerror = [0, -4, -100, -400, -1996, -2000]
is_typeerror = ['-4', '4', '100', 'ins', '**', '中文']
# params中需要傳入list
@pytest.fixture(params=is_leap)
def is_leap_y(request):
return request.param
@pytest.fixture(params=is_typeerror)
def is_type_error(request):
return request.param
- 測(cè)試用例文件:
# test_para.py
import sys
sys.path.append('.')
import is_leap_year
import pytest
class TestPara():
def test_is_leap(self, is_leap_y):
assert is_leap_year.is_leap_year(is_leap_y) == True
def test_is_typeerror(self, is_type_error):
with pytest.raises(TypeError):
is_leap_year.is_leap_year(is_type_error)
- 測(cè)試結(jié)果:
PS E:\python_interface_test\requests_practice> pytest -q .\test_para.py
............ [100%]
12 passed in 0.03 seconds
pytest.mark.parametrize()方式進(jìn)行參數(shù)化
-
采用標(biāo)記函數(shù)參數(shù)化违寿,傳入單個(gè)參數(shù)熟空,pytest.mark.parametrize("參數(shù)名",lists)
mark.png 采用標(biāo)記函數(shù)傳入多個(gè)參數(shù)菌瘪,如pytest.mark.parametrize("para1, para2", [(p1_data_0, p2_data_0), (p1_data_1, p2_data_1),...]
測(cè)試用例中傳入2個(gè)參數(shù),year和期望結(jié)果糜工,使輸入數(shù)據(jù)與預(yù)期結(jié)果對(duì)應(yīng)录淡,構(gòu)造了2組會(huì)失敗的數(shù)據(jù)嫉戚,在執(zhí)行結(jié)果中,可以看到失敗原因:
image.png
import sys
sys.path.append('.')
import is_leap_year
import pytest
class TestPara():
# 參數(shù)傳入year中
@pytest.mark.parametrize('year, expected', [(1, False), (4, True), (100, False), (400, True), (500, True)])
def test_is_leap(self, year, expected):
assert is_leap_year.is_leap_year(year) == expected
@pytest.mark.parametrize('year, expected', [(0, ValueError), ('-4', TypeError), (-4, ValueError), ('ss', TypeError), (100, ValueError)])
def test_is_typeerror(self, year,expected):
if expected == ValueError:
with pytest.raises(ValueError) as excinfo:
is_leap_year.is_leap_year(year)
assert excinfo.type == expected
else:
with pytest.raises(TypeError) as excinfo:
is_leap_year.is_leap_year(year)
assert excinfo.type == expected
———————————————————————————————————
嗨嘍,對(duì)測(cè)試感興趣窍帝,可以關(guān)注大神公眾號(hào)了解更多測(cè)試?yán)碚摾ぱАy(cè)試技術(shù)、自動(dòng)化測(cè)試压怠、接口測(cè)試飞苇、python。突梦。羽利。
開源優(yōu)測(cè).png