pytest文檔內(nèi)置fixture的request詳情
前言
request 是 pytest 的內(nèi)置 fixture , "為請求對象提供對請求測試上下文的訪問權(quán),并且在fixture被間接參數(shù)化的情況下具有可選的“param”屬性。"這是官方文檔對request的描述,可參考的文檔不多。
一、FixtureRequest
FixtureRequest 是來自 fixture 或者 測試用例的請求,它有訪問測試上下文的權(quán)限, FixtureRequest_pytest.fixtures pytest documentation。
class FixtureRequest:請求對象提供對請求的測試上下文的訪問,并且具有可選的 param 屬性,以防設(shè)備被間接參數(shù)化。
fixturename:正在為其執(zhí)行此請求的 fixture 名稱。
scope:作用域字符串,“function”、“class”、“module”、“session”之一。
fixturenames:此請求中所有活動狀態(tài)的 fixture 的名稱。
node:基礎(chǔ)集合節(jié)點(diǎn)(取決于當(dāng)前請求范圍)。
config:與此請求關(guān)聯(lián)的 pytest 配置對象。
function:如果請求具有每個函數(shù)范圍,則測試函數(shù)對象。
cls:類(可以是None),其中收集了測試函數(shù)。
instance:在其上收集測試函數(shù)的實(shí)例(可以是None)。
module:收集測試函數(shù)的Python模塊對象。
fspath:收集此測試的測試模塊的文件系統(tǒng)路徑。
keywords:基礎(chǔ)節(jié)點(diǎn)的關(guān)鍵字/標(biāo)記詞典。
session:Pytest會話對象。
addfinalizer(finalizer: 添加finalizer/teardown函數(shù),以便在請求的測試上下文中的最后一個測試完成執(zhí)行后調(diào)用。
applymarker(marker):對單個測試函數(shù)調(diào)用應(yīng)用標(biāo)記。
如果不希望在所有函數(shù)調(diào)用上都有關(guān)鍵字/標(biāo)記,則此方法非常有用。
參數(shù):
- marker -- A _pytest.mark.MarkDecorator 調(diào)用創(chuàng)建的對象 pytest.mark.NAME(...) .
- raiseerror(msg: Optional[str]) :使用給定的消息引發(fā)FixtureLookupError。
- getfixturevalue(argname: str) 動態(tài)運(yùn)行命名的fixture函數(shù)。
如果可能,建議通過函數(shù)參數(shù)聲明fixtures。但是,如果您只能在測試設(shè)置時決定是否使用另一個fixture,那么您可以使用此函數(shù)在fixture或測試函數(shù)體中檢索它。
引發(fā):pytest.FixtureLookupError -- 如果找不到給定的固定裝置。
折疊
二、request.param
前面講fixture參數(shù)化的時候,有接觸到 "request.param" 用于獲取測試的請求參數(shù),以下示例
import pytest # 測試數(shù)據(jù) test_data = ["user1", "user2"] @pytest.fixture(params=test_data) def register_users(request): # 獲取當(dāng)前的測試數(shù)據(jù) user = request.param print("\n拿著這個賬號去注冊:%s"%user) result = "success" return user, result def test_register(register_users): user, result = register_users print("在測試用例里面里面獲取到當(dāng)前測試數(shù)據(jù):%s"%user) print(result) assert result == "success"
此案例里面我們可以在fixture參數(shù)化的時候,通過request.param獲取到測試的請求參數(shù),但是在用例里面用 request.param 卻不能獲取到測試的請求參數(shù)
def test_register_x(register_users, request): print(request.param)
這樣運(yùn)行,會拋異常:'FixtureRequest' object has no attribute 'param'
#拿著這個賬號去注冊:user1 F register_users = ('user1', 'success') request = <FixtureRequest for <Function test_register_x[user1]>> def test_register_x(register_users, request): > print(request.param) E AttributeError: 'FixtureRequest' object has no attribute 'param' D:\test_x7.py:27: AttributeError
三、request.config
request.config 是獲取測試的配置文件參數(shù),這個在前面講命令行參數(shù)的時候有用到過.
在 conftest.py 寫一個 hook函數(shù), pytest_addoption 的作用是用于獲取命令行參數(shù),request.config 用于讀取測試的配置數(shù)據(jù)
import pytest def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @pytest.fixture def cmdopt(request): return request.config.getoption("--cmdopt")
于是在測試用例里面可以通過 request.config 來獲取到配置參數(shù),也可以通過自己定義的 cmdopt 來獲取。
import pytest def test_answer_1(request): type = request.config.getoption("--cmdopt") print("獲取到命令行參數(shù):%s" % type) def test_answer_2(cmdopt): print("獲取到命令行參數(shù):%s" % cmdopt)
四、request.module
fixture 函數(shù)可以通過接受 request 對象來反向獲取請求中的測試函數(shù)、類或模塊上下文,進(jìn)一步擴(kuò)展之前的 smtp fixture示例,讓我們從fixture的測試模塊讀取可選的服務(wù)器URL
這是官方文檔的一個示例
# conftest.py @pytest.fixture(scope="module") def smtp(request): server = getattr(request.module, "smtpserver", "smtp.qq.com") print("fixture 獲取到的server :%s" %server) smtp = smtplib.SMTP(server, 587, timeout=5) yield smtp print("完成 %s (%s)" % (smtp, server)) smtp.close()
我們使用request.module屬性來從測試模塊中選擇性地獲取smtpserver屬性
快速創(chuàng)建另一個測試模塊,在其模塊名稱空間中實(shí)際設(shè)置服務(wù)器URL,新建一個test_anothersmtp.py文件,輸入以下代碼:
# test_anothersmtp.py smtpserver = "mail.python.org" def test_showhelo(smtp): print("case showhelo")
這時候運(yùn)行用例,會獲取到 test_anothersmtp.py 里面定義的 smtpserver
============================= test session starts ============================= platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type> rootdir: D:\ rerunfailures-9.1, xdist-2.1.0 collected 1 item ..\..\..\..\module2\test_anothersmtp.py fixture 獲取到的server :mail.python.org case showhelo .完成 <smtplib.SMTP object at 0x000001D00754CB00> (mail.python.org) ========================== 1 passed in 0.64 seconds ===========================
用例里面沒定義 smtpserver 的話,會用默認(rèn)屬性 "smtp.qq.com"
五、request的相關(guān)成員對象
在conftest.py 寫一個fixture 可以獲取到request的一些成員對象相關(guān)信息
# conftest.py @pytest.fixture(autouse=True) def print_request(request): print("\n=======================request start=================================") print(request.module) print(request.function) print(request.cls) print(request.fspath) print(request.fixturenames) print(request.fixturename) print(request.scope) print("\n=======================request end=================================")
使用命令行"pytest -s text_x.py"運(yùn)行用例,會看到打印的結(jié)果:
test_1.py
=======================request start=================================
<module 'web.cases.module2.test_1' from 'D:\\web\\cases\\module2\\test_1.py'>
<function test_answer_1 at 0x0000012D1C9FD9D8>
None
D:\web\cases\module2\test_1.py
['_verify_url', 'base_url', '__pytest_repeat_step_number', 'show_request', 'request']
show_request
function
=======================request end=================================
獲取到命令行參數(shù):type1
.
=======================request start=================================
<module 'web.cases.module2.test_1' from 'D:\\web\\cases\\module2\\test_1.py'>
<function test_answer_2 at 0x0000012D1C9FD730>
None
D:\web\cases\module2\test_1.py
['_verify_url', 'base_url', '__pytest_repeat_step_number', 'show_request', 'cmdopt', 'request']
show_request
function
=======================request end=================================
在打印測試用例的詳細(xì)日志的時候,還是很有用的。
到此這篇關(guān)于pytest文檔內(nèi)置fixture的request詳情的文章就介紹到這了,更多相關(guān)pytest fixture內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python分享一款地理數(shù)據(jù)可視化神器keplergl
這篇文章主要介紹了分享一款地理數(shù)據(jù)可視化神器keplergl,keplergl是由Uber開源的一款地理數(shù)據(jù)可視化工具,通過keplergl我們可以在Jupyter?notebook中使用,下文分享需要的小伙伴可以參考一下2022-02-02opencv-python的RGB與BGR互轉(zhuǎn)方式
這篇文章主要介紹了opencv-python的RGB與BGR互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所 幫助。一起跟隨小編過來看看吧2020-06-06Python?matplotlib中更換畫布背景顏色的3種方法
這篇文章主要給大家介紹了關(guān)于Python?matplotlib中更換畫布背景顏色的3種方法,在Matplotlib中,我們可以使用set_facecolor()方法來設(shè)置背景顏色,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11python 裝飾器功能以及函數(shù)參數(shù)使用介紹
之前學(xué)習(xí)編程語言大多也就是學(xué)的很淺很淺,基本上也是很少涉及到裝飾器這些的類似的內(nèi)容??偸怯X得是一樣很神奇的東西,舍不得學(xué)(嘿嘿)。今天看了一下書籍。發(fā)現(xiàn)道理還是很簡單的2012-01-01關(guān)于Python中的海象運(yùn)算符使用方法詳解
這篇文章主要介紹了關(guān)于Python中的海象運(yùn)算符“:=”使用方法詳解,海象運(yùn)算符(walrus?operator)是?Python?3.8?中引入的一種新的語法,需要的朋友可以參考下2023-04-04Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案
這篇文章主要介紹了Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯教程,通過圖文并茂的形式給大家介紹了配置自動代碼提示的方法,需要的朋友可以參考下2022-01-01Python實(shí)現(xiàn)TXT數(shù)據(jù)轉(zhuǎn)三維矩陣
在數(shù)據(jù)處理和分析中,將文本文件中的數(shù)據(jù)轉(zhuǎn)換為三維矩陣是一個常見的任務(wù),本文將詳細(xì)介紹如何使用Python實(shí)現(xiàn)這一任務(wù),感興趣的小伙伴可以了解下2024-01-01Django?項目配置拆分獨(dú)立的實(shí)現(xiàn)
Django 項目中,我們默認(rèn)的配置是都在 settings.py 文件里面的,但是實(shí)際本地調(diào)試和線上應(yīng)該是需要兩個環(huán)境的,我們現(xiàn)在來拆分下配置,本文就詳細(xì)的來介紹一下2021-11-11Tkinter canvas的畫布參數(shù),刪除組件,添加垂直滾動條詳解
這篇文章主要介紹了python tkinter 畫布參數(shù),刪除組件,添加垂直滾動條使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-10-10