1.pytest fixture
定義fixture跟定義普通函數(shù)差不多坚踩,唯一區(qū)別就是在函數(shù)上加個裝飾器@pytest.fixture()
fixture命名與用例區(qū)分開墓阀,不要以test開頭毡惜。fixture是有返回值的,沒有返回值默認為None斯撮。
用例調用fixture的返回值经伙,直接就是把fixture的函數(shù)名稱當做變量名稱。
主要解決測試前后的問題和依賴問題勿锅。
2. pytest.fixture()運行配置
fixture的功能主要是定制化執(zhí)行特定的方法帕膜。
比如淘寶,有些不需要登錄也可以進行直接操作溢十,比如搜索垮刹,對商品進行搜索,不需要登錄茶宵。但是加入購物車或者支付危纫,都需要登錄,才能進行操作乌庶。
核心:@pytest.fixture()
括號里面的默認值是function种蝶。
操作步驟:
1、導入pytest
2瞒大、在登錄函數(shù)上螃征,使用@pytest.fixture(),注意:函數(shù)名稱不以test開頭
3透敌、不傳入變量函數(shù)名盯滚,直接執(zhí)行測試函數(shù)
4、傳入變量函數(shù)名酗电,先登錄在執(zhí)行測試函數(shù)
import pytest
@pytest.fixture()
def login():
print('登錄用戶名和密碼')
def test_search():
print('用例1魄藕,不需要登錄也可以進行搜索')
def test_cart(login):
print('用例2,需要登錄才可以進行加入購物車')
def test_pay(login):
print('用例3撵术,需要登錄才可以進行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回內容:
test_fixture.py::test_search 用例1背率,不需要登錄也可以進行搜索
PASSED
test_fixture.py::test_cart 登錄用戶名和密碼
用例2,需要登錄才可以進行加入購物車
PASSED
test_fixture.py::test_pay 登錄用戶名和密碼
用例3,需要登錄才可以進行支付
PASSED
3.pytest.fixture(params=xx)數(shù)據傳遞
如上例子中寝姿,登錄中的用戶名和密碼交排,不在函數(shù)中,是一個變量饵筑,那就需要參數(shù)傳遞埃篓,fixture通過params進行操作。@pytest.fixture(params=xxx)根资,函數(shù)中request參數(shù)是固定
import pytest
@pytest.fixture(params=[1,2,4,'linda'])
def prepare_data(request): #固定寫法架专,傳入參數(shù):request
return request.param #固定寫法,傳入參數(shù)和變量:request.param
def test_prepare(prepare_data): #函數(shù)名為變量傳入玄帕,拆包胶征,把參數(shù)進行傳入
print('參數(shù)傳遞%s'%prepare_data)
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture_param.py'])
返回內容:
test_fixture_param.py::test_prepare[1] 參數(shù)傳遞1
test_fixture_param.py::test_prepare[2] 參數(shù)傳遞2
test_fixture_param.py::test_prepare[4] 參數(shù)傳遞4
test_fixture_param.py::test_prepare[linda] 參數(shù)傳遞linda
4.多個fixture之間的傳遞
1、fixture傳遞桨仿,直接傳入變量睛低,就先執(zhí)行website,在執(zhí)行l(wèi)ogin
import pytest
@pytest.fixture()
def login(website): #fixture傳遞服傍,直接傳入變量钱雷,就先執(zhí)行website,在執(zhí)行l(wèi)ogin
print('登錄用戶名和密碼')
@pytest.fixture()
def website():
print('打開瀏覽器')
def test_search():
print('用例1吹零,不需要登錄也可以進行搜索')
def test_cart(login):
print('用例2罩抗,需要登錄才可以進行加入購物車')
def test_pay(login):
print('用例3,需要登錄才可以進行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回內容:
test_fixture.py::test_search 用例1灿椅,不需要登錄也可以進行搜索
test_fixture.py::test_cart 打開瀏覽器
登錄用戶名和密碼
用例2套蒂,需要登錄才可以進行加入購物車
test_fixture.py::test_pay 打開瀏覽器
登錄用戶名和密碼
用例3,需要登錄才可以進行支付
2茫蛹、fixture傳遞操刀,pytest.fixture(autouse=True),表示所有的test_xx用例都會執(zhí)行這個 fixture傳遞婴洼。
import pytest
@pytest.fixture(autouse=True)
def login():
print('登錄用戶名和密碼')
@pytest.fixture()
def website():
print('打開瀏覽器')
def test_search():
print('用例1骨坑,不需要登錄也可以進行搜索')
def test_cart(website):
print('用例2,需要登錄才可以進行加入購物車')
def test_pay(website):
print('用例3柬采,需要登錄才可以進行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回結果:
test_fixture.py::test_search 登錄用戶名和密碼
用例1欢唾,不需要登錄也可以進行搜索
test_fixture.py::test_cart 登錄用戶名和密碼
打開瀏覽器
用例2,需要登錄才可以進行加入購物車
test_fixture.py::test_pay 登錄用戶名和密碼
打開瀏覽器
用例3粉捻,需要登錄才可以進行支付
3礁遣、fixture傳遞,只針對特定的test_xx方法進行參數(shù)傳遞肩刃,可以通過標記的方式進行操作祟霍。
核心:@pytest.mark.usefixtures("login")
import pytest
@pytest.fixture()
def login():
print('登錄用戶名和密碼')
def test_search():
print('用例1押搪,不需要登錄也可以進行搜索')
def test_cart():
print('用例2,需要登錄才可以進行加入購物車')
@pytest.mark.usefixtures("login")
def test_pay():
print('用例3浅碾,需要登錄才可以進行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回結果:
test_fixture.py::test_search 用例1,不需要登錄也可以進行搜索
test_fixture.py::test_cart 用例2续语,需要登錄才可以進行加入購物車
test_fixture.py::test_pay 登錄用戶名和密碼
用例3垂谢,需要登錄才可以進行支付