若是第三方插件知足不了本身的需求,那麼就須要你本身定製化開發本身的插件了。html
正因如此,pytest的插件生態很是的豐富,通常的常規性需求基本上均可以找到現成的第三方插件。python
說到開發插件的話,仍是要引入一個新的名詞hook functions ,對於鉤子函數是要另起一篇介紹的,那也是真正開發插件的開始。session
一個插件裏包含了一個或者多個鉤子函數,編寫鉤子函數能夠實現功能豐富的插件,因此pytest框架從配置、收集用例到運行用例,再到最後出報告,都是經過調用各類插件來實現的。框架
目前所說插件就是如下這3種了:函數
_pytest
這個目錄下的模塊(.\Python37\Lib\site-packages_pytest)conftest.py
,這個能夠理解成框架的固定寫法,把hook
或者fixture
寫在這個文件裏,就會自動去調用。內置插件、外部插件都仍是很好理解的,那conftest
文件是什麼?
其實,它是一個python文件。
開個玩笑,其實conftest.py
一般能夠用來作2個事情:測試
另外,能夠根據conftest.py
的文件存放位置,來決定它的適用範圍。插件
在以前的文章【pytest】(六) pytest中fixture的使用 中,咱們用到的fixture函數都是寫在了測試case的文件中。
好比:fixture
函數logout_after_login_success()
是在測試case test_login_success
執行結束後執行,用來退出登陸。code
@pytest.fixture() def logout_after_login_success(browser): yield HP = HomePage(browser) HP.logout() @pytest.mark.critical def test_login_success(browser, logout_after_login_success, login_temp): '''合法登陸''' HP = HomePage(browser) assert HP.greetings().text == "Hi, test1"
那若是你發現你的一個fixture
函數,在不少case模塊中都要用到,這時候你要怎麼引用呢?orm
不要緊,咱們能夠把這個fixture
函數放到conftest.py
中去。
好比說,我如今有一個項目結構是這樣的:htm
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py -test_module_02 /* test_case3.py -utils
在test_module_01
模塊下,有2個case用例文件,分別是 test_case1.py
和test_case2.py
。
在test_module_02
模塊下,有一個case用例文件test_case3.py
。
如今若是想要test_module_01
模塊下的2個測試用例都用上同一個fixture
函數,那麼就能夠在test_module_01
模塊下,新建一個與2個case文件同級的conftest.py
,文件結構就變成了這樣:
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py /* conftest.py -----> 在這 -test_module_02 /* test_case3.py -utils
test_case1.py
、test_case2.py
和conftest.py
代碼分別爲:
# test_case1.py import pytest def test_01(demo_fixture): print("執行了test_01") if __name__ == '__main__': pytest.main(["-s", "test_case1.py"])
# test_case2.py import pytest def test_02(demo_fixture): print("執行了test_02") if __name__ == '__main__': pytest.main(["-s", "test_case2.py"])
# /test_module_01/conftest.py import pytest @pytest.fixture def demo_fixture(): print("這是fixture函數的輸出")
分別執行test_case1.py
和test_case2.py
, 都輸出了demo_fixture()函數的內容。
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: D:\練習\demo_project\test_case\test_module_01 collected 1 item test_case1.py 這是fixture函數的輸出 執行了test_01 . ============================== 1 passed in 0.01s ============================== [Finished in 0.4s]
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: D:\練習\demo_project\test_case\test_module_01 collected 1 item test_case2.py 這是fixture函數的輸出 執行了test_02 . ============================== 1 passed in 0.02s ============================== [Finished in 0.4s]
再繼續,若是test_module_02
模塊下的也想用,怎麼辦?
那就把conftest.py
再往外提一層,與 test_module_01
、test_module_02
保持同級便可。
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py -test_module_02 /* test_case3.py /* conftest.py -utils
你們能夠試一下,這裏就不繼續代碼演示了。
總結來講,conftest.py
文件能夠做用於同級
以及 如下
的模塊。
可是,當如下層級中也存在了另外一個conftest.py
,那麼如下層級將由另外一個conftest.py
文件接管。
放個圖示輔助說明一下:
2張圖能說的事,水了這麼多,哈哈。