Pytest框架之fixture的詳細(xì)使用教程
前言
前面一篇講了setup、teardown可以實(shí)現(xiàn)在執(zhí)行用例前或結(jié)束后加入一些操作,但這種都是針對(duì)整個(gè)腳本全局生效的
如果有以下場(chǎng)景:用例 1 需要先登錄,用例 2 不需要登錄,用例 3 需要先登錄。很顯然無法用 setup 和 teardown 來實(shí)現(xiàn)了fixture可以讓我們自定義測(cè)試用例的前置條件
fixture優(yōu)勢(shì)
- 命名方式靈活,不局限于 setup 和teardown 這幾個(gè)命名
- conftest.py 配置里可以實(shí)現(xiàn)數(shù)據(jù)共享,不需要 import 就能自動(dòng)找到fixture
- scope="module" 可以實(shí)現(xiàn)多個(gè).py 跨文件共享前置
- scope="session" 以實(shí)現(xiàn)多個(gè).py 跨文件使用一個(gè) session 來完成多個(gè)用例
fixture參數(shù)列表
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
print("fixture初始化的參數(shù)列表")
參數(shù)列表
- scope:可以理解成fixture的作用域,默認(rèn):function,還有class、module、package、session四個(gè)【常用】
- autouse:默認(rèn):False,需要用例手動(dòng)調(diào)用該fixture;如果是True,所有作用域內(nèi)的測(cè)試用例都會(huì)自動(dòng)調(diào)用該fixture
- name:默認(rèn):裝飾器的名稱,同一模塊的fixture相互調(diào)用建議寫個(gè)不同的name
注意
session的作用域:是整個(gè)測(cè)試會(huì)話,即開始執(zhí)行pytest到結(jié)束測(cè)試
測(cè)試用例如何調(diào)用fixture
- 將fixture名稱作為測(cè)試用例函數(shù)的輸入?yún)?shù)
- 測(cè)試用例加上裝飾器:@pytest.mark.usefixtures(fixture_name)
- fixture設(shè)置autouse=True
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 15:50 __Author__ = 小菠蘿測(cè)試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import pytest # 調(diào)用方式一 @pytest.fixture def login(): print("輸入賬號(hào),密碼先登錄") def test_s1(login): print("用例 1:登錄之后其它動(dòng)作 111") def test_s2(): # 不傳 login print("用例 2:不需要登錄,操作 222") # 調(diào)用方式二 @pytest.fixture def login2(): print("please輸入賬號(hào),密碼先登錄") @pytest.mark.usefixtures("login2", "login") def test_s11(): print("用例 11:登錄之后其它動(dòng)作 111") # 調(diào)用方式三 @pytest.fixture(autouse=True) def login3(): print("====auto===") # 不是test開頭,加了裝飾器也不會(huì)執(zhí)行fixture @pytest.mark.usefixtures("login2") def loginss(): print(123)
執(zhí)行結(jié)果
fixture的實(shí)例化順序
- 較高 scope 范圍的fixture(session)在較低 scope 范圍的fixture( function 、 class )之前實(shí)例化【session > package > module > class > function】
- 具有相同作用域的fixture遵循測(cè)試函數(shù)中聲明的順序,并遵循fixture之間的依賴關(guān)系【在fixture_A里面依賴的fixture_B優(yōu)先實(shí)例化,然后到fixture_A實(shí)例化】
- 自動(dòng)使用(autouse=True)的fixture將在顯式使用(傳參或裝飾器)的fixture之前實(shí)例化
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 16:14 __Author__ = 小菠蘿測(cè)試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import pytest order = [] @pytest.fixture(scope="session") def s1(): order.append("s1") @pytest.fixture(scope="module") def m1(): order.append("m1") @pytest.fixture def f1(f3, a1): # 先實(shí)例化f3, 再實(shí)例化a1, 最后實(shí)例化f1 order.append("f1") assert f3 == 123 @pytest.fixture def f3(): order.append("f3") a = 123 yield a @pytest.fixture def a1(): order.append("a1") @pytest.fixture def f2(): order.append("f2") def test_order(f1, m1, f2, s1): # m1、s1在f1后,但因?yàn)閟cope范圍大,所以會(huì)優(yōu)先實(shí)例化 assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]
執(zhí)行結(jié)果
斷言成功
關(guān)于fixture的注意點(diǎn)
添加了 @pytest.fixture
,如果fixture還想依賴其他fixture,需要用函數(shù)傳參的方式,不能用 @pytest.mark.usefixtures()
的方式,否則會(huì)不生效
@pytest.fixture(scope="session") def open(): print("===打開瀏覽器===") @pytest.fixture # @pytest.mark.usefixtures("open") 不可?。。?!不生效!??! def login(open): # 方法級(jí)別前置操作setup print(f"輸入賬號(hào),密碼先登錄{open}")
前面講的,其實(shí)都是setup的操作,那么現(xiàn)在就來講下teardown是怎么實(shí)現(xiàn)的
用fixture實(shí)現(xiàn)teardown并不是一個(gè)獨(dú)立的函數(shù),而是用 yield 關(guān)鍵字來開啟teardown操作
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 15:50 __Author__ = 小菠蘿測(cè)試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import pytest @pytest.fixture(scope="session") def open(): # 會(huì)話前置操作setup print("===打開瀏覽器===") test = "測(cè)試變量是否返回" yield test # 會(huì)話后置操作teardown print("==關(guān)閉瀏覽器==") @pytest.fixture def login(open): # 方法級(jí)別前置操作setup print(f"輸入賬號(hào),密碼先登錄{open}") name = "==我是賬號(hào)==" pwd = "==我是密碼==" age = "==我是年齡==" # 返回變量 yield name, pwd, age # 方法級(jí)別后置操作teardown print("登錄成功") def test_s1(login): print("==用例1==") # 返回的是一個(gè)元組 print(login) # 分別賦值給不同變量 name, pwd, age = login print(name, pwd, age) assert "賬號(hào)" in name assert "密碼" in pwd assert "年齡" in age def test_s2(login): print("==用例2==") print(login)
yield注意事項(xiàng)
- 如果yield前面的代碼,即setup部分已經(jīng)拋出異常了,則不會(huì)執(zhí)行yield后面的teardown內(nèi)容
- 如果測(cè)試用例拋出異常,yield后面的teardown內(nèi)容還是會(huì)正常執(zhí)行
yield+with的結(jié)合
# 官方例子 @pytest.fixture(scope="module") def smtp_connection(): with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection: yield smtp_connection # provide the fixture value
該 smtp_connection
連接將測(cè)試完成執(zhí)行后已經(jīng)關(guān)閉,因?yàn)?smtp_connection
對(duì)象自動(dòng)關(guān)閉時(shí), with
語句結(jié)束。
addfinalizer 終結(jié)函數(shù)
@pytest.fixture(scope="module") def test_addfinalizer(request): # 前置操作setup print("==再次打開瀏覽器==") test = "test_addfinalizer" def fin(): # 后置操作teardown print("==再次關(guān)閉瀏覽器==") request.addfinalizer(fin) # 返回前置操作的變量 return test def test_anthor(test_addfinalizer): print("==最新用例==", test_addfinalizer)
注意事項(xiàng)
如果 request.addfinalizer() 前面的代碼,即setup部分已經(jīng)拋出異常了,則不會(huì)執(zhí)行 request.addfinalizer() 的teardown內(nèi)容(和yield相似,應(yīng)該是最近新版本改成一致了)
可以聲明多個(gè)終結(jié)函數(shù)并調(diào)用
總結(jié)
到此這篇關(guān)于Pytest框架之fixture的詳細(xì)使用教程的文章就介紹到這了,更多相關(guān)Pytest fixture使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲抓取技術(shù)的一些經(jīng)驗(yàn)
這篇文章主要介紹了Python爬蟲抓取技術(shù)的一些經(jīng)驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python基礎(chǔ)教程之正則表達(dá)式基本語法以及re模塊
正則表達(dá)式是可以匹配文本片段的模式,今天的Python就跟大家一起討論一下python中的re模塊,python re模塊感興趣的朋友一起學(xué)習(xí)吧2016-03-03Django學(xué)習(xí)之靜態(tài)文件與模板詳解
這篇文章主要為大家詳細(xì)介紹了Django靜態(tài)文件與模板,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02python 實(shí)現(xiàn)百度網(wǎng)盤非會(huì)員上傳超過500個(gè)文件的方法
這篇文章主要介紹了python 實(shí)現(xiàn)百度網(wǎng)盤非會(huì)員上傳超過500個(gè)文件的方法,幫助大家更好的利用python解決問題,感興趣的朋友可以了解下2021-01-01Django前端BootCSS實(shí)現(xiàn)分頁的方法
本文主要介紹了Django前端BootCSS實(shí)現(xiàn)分頁的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11手把手教你怎么用Python實(shí)現(xiàn)zip文件密碼的破解
之前在家里的老電腦中,發(fā)現(xiàn)一個(gè)加密zip壓縮包,由于時(shí)隔太久忘記密碼了,依稀記得密碼是6位字母加數(shù)字,網(wǎng)上下載了很多破解密碼的軟件都沒有效果,于是想到自己用Python寫一個(gè)暴力破解密碼的腳本,需要的朋友可以參考下2021-05-05Python Flask 請(qǐng)求數(shù)據(jù)獲取響應(yīng)詳解
這篇文章主要介紹了Python Flask請(qǐng)求數(shù)據(jù)獲取響應(yīng)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-10-10Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)希爾排序算法,簡單講述了希爾排序的原理并結(jié)合具體實(shí)例形式分析了Python希爾排序的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2017-11-11