解析Pytest3種配置文件方式
配置介紹
pytest 的主配置文件,可以改變 pytest 的默認行為,執(zhí)行 pytest -h,這里有很多配置均可用于 pytest.ini配置
(venv) D:\Python_test\pythonpp\pytest_>pytest -h [pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found: markers (linelist): Markers for test functions empty_parameter_set_mark (string): Default marker for empty parametersets norecursedirs (args): Directory patterns to avoid for recursion testpaths (args): Directories to search for tests when no files or directories are given on the command line filterwarnings (linelist): Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings. usefixtures (args): List of default fixtures to be used with this project python_files (args): Glob-style file patterns for Python test module discovery python_classes (args): Prefixes or glob names for Python test class discovery python_functions (args): Prefixes or glob names for Python test function and method discovery disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool): Disable string escape non-ASCII characters, might cause unwanted side effects(use at your own risk) console_output_style (string): Console output: "classic", or with additional progress information ("progress" (percentage) | "count") xfail_strict (bool): Default for the strict parameter of xfail markers when not given explicitly (d to see available markers type: pytest --markers to see available fixtures type: pytest --fixtures (shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option
輸入pytest -h其實不止這些命令,我只是截取出本章最主要的部分。
配置案例
# pytest.ini [pytest] # 命令行執(zhí)行參數(shù) addopts = -vs # 排除目錄 norecursedirs = no_Case # 默認執(zhí)行目錄 testpaths = ./ # 執(zhí)行規(guī)則-class python_classes = Test* # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = test* # xfail 標志規(guī)則 xfail_strict = false # 自定義注冊標志 markers = login: 登陸類標志 information: 信息頁 index: 首頁
pytest.ini 中最好不要用中文,如果使用的話,請將文件編碼改成 gbk ,否則還請刪除ini配置文件中的中文。
目錄結構
此處建議新建一個環(huán)境,如果你的pytest本就是一個新環(huán)境,沒有其他的東西,可以不用新建。因為環(huán)境包如果過多,會對運行造成干擾。
pytest_ Case test_a.py no_Case test_b.py pytest.ini run.py
pytest.ini上面已經展示了,看看run.py:
import pytest if __name__ == '__main__': pytest.main()
就是執(zhí)行入口,本章我們不用命令執(zhí)行了。此外還有兩個目錄就是Case跟no_Case,放用例的地方,
命令樣式講解
addopts
[pytest] addopts = -vs # ---等價于--- pytest.main(["-vs"])
addopts可以接收很多了參數(shù),換句話說main中能接收的參數(shù),此處都能寫。
目錄規(guī)則
# 排除目錄 norecursedirs = no_Case # 默認執(zhí)行目錄 testpaths = ./
如果你發(fā)現(xiàn)目錄不論怎么改都沒有生效(能檢測到用例),那么就請按照你上面所說重新弄一個環(huán)境。如果環(huán)境OK了,可以檢測到目錄了,會報錯:拒絕訪問亦或者ERROR No escaped character,亦或者norecursedirs的目錄用例運行了,那么都可以歸結于目錄路徑寫錯了。 當然,你可以通過控制默認執(zhí)行目錄達到排除目錄的效果。
用例執(zhí)行規(guī)則
# 執(zhí)行規(guī)則-class python_classes = Test* # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = test*
當然,pytest默認設置的也是class檢測Test開頭,用例是test,我們也能換成自己想要的樣式:
class Qing_A: def qing_a(self): print("我是清安") def qing_b(self): print("我是拾貳")
那么pytest.ini因該如何寫呢:
# pytest.ini [pytest] # 命令行執(zhí)行參數(shù) addopts = -vs # 排除目錄 norecursedirs = no_Case # 默認執(zhí)行目錄 testpaths = ./ # 執(zhí)行規(guī)則-class python_classes = Qing* # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = qing*
py文件命名此處我就沒改,可以自己試試,類與函數(shù)用例我是改了。除了這樣還可以:
# 執(zhí)行規(guī)則-class python_classes = Qing* *Qing # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = qing* *test
代碼出僅需要添加:
class B_Qing: def b_test(self): print("我是b用例")
就能檢測到自定義的用例了,看看結果:
Case/test_a.py::Qing_A::qing_a 我是清安
PASSED
Case/test_a.py::Qing_A::qing_b 我是拾貳
PASSED
Case/test_a.py::B_Qing::b_test 我是b用例
PASSED
注意點
用例檢測這里,如果你寫了py,class,function,那么它會看著這樣的邏輯進行檢測,如果python_files都沒有檢測到了,剩下的python_classes以及python_functions也就不能進行了。其次是python_classes如果有則優(yōu)先檢測,如果沒有則檢測python_functions。
自定義標志
pytest.ini 中最好不要用中文,如果使用的話,大家要將文件編碼改成 gbk
標志名稱沒有限制,建議大家參考模塊命名,要有業(yè)務含義,不要隨心而寫
所有的自定義標志,建議大家在 pytest.ini 中進行統(tǒng)一管理和通過命令參數(shù)--strict-markers 進行授權(pytest 其實不強制)
pytest 中的 markers 配置,相當于我們對業(yè)務的一種設計歸類,尤其是大項目時非常重要
# 自定義注冊標志 markers = login: 登陸類標志 information: 信息頁 index: 首頁
import pytest @pytest.mark.login class Qing_A: def qing_a(self): print("我是清安") @pytest.mark.information def qing_b(self): print("我是拾貳") @pytest.mark.index class B_Qing: def b_test(self): print("我是b用例")
那么如何運行指定的標志呢:
[pytest] # 命令行執(zhí)行參數(shù) addopts = -vs -m login # 或者 addopts = -vs -m "not login" # 或者 addopts = -vs -m "login or index"
"""not login結果示例"""
Case/test_a.py::Qing_A::qing_a 我是清安
PASSED
Case/test_a.py::Qing_A::qing_b 我是拾貳
PASSED
亂碼問題
有些人的情況或許跟我一樣,pytest.ini輸入的中文是亂碼或者讀取出來的是亂碼,這時候可以在設置中的:
修改成GBK即可。
小結
關于并未完全講完的一些參數(shù)可以來這里直接CTRL + F搜索:https://www.osgeo.cn/pytest/reference.html#ini-options-ref一定是pytest.ini文件嗎?其他的配置文件不行嗎。官方介紹到還有.toml,tox.ini,setup.cfg,其中setup.cfg是不被推薦使用的,官方文檔這樣說道:
??警告 用法 setup.cfg 除非用于非常簡單的用例,否則不推薦使用。 .cfg 文件使用不同于 pytest.ini 和 tox.ini 這可能會導致難以追蹤的問題。如果可能,建議使用后一個文件,或者 pyproject.toml ,以保存pytest配置。
關于toml配置文件
[tool.pytest.ini_options] addopts = "-vs -m login" norecursedirs = "no_Case" testpaths = "./" python_classes = "Qing* *Qing" python_files = "test*" python_functions = "qing* *test" xfail_strict = "false" markers = ["login:登陸類標志", "information:信息頁", "index:首頁"]
如上是改寫的pytest.ini配置文件的。寫法上有些不一樣,注意點即可。此外關于官網的介紹,其實其他地方也可以改成類似于markers的寫法:
[tool.pytest.ini_options] addopts = "-vs -m login" norecursedirs = "no_Case" testpaths = "./" python_classes = ["Qing*","*Qing"] python_files = "test*" python_functions = ["qing*","*test"] xfail_strict = "false" markers = ["login:登陸類標志", "information:信息頁", "index:首頁"]
關于tox.ini配置文件
[pytest] addopts = -vs --strict-markers -m "not index" norecursedirs = no_Case testpaths = ./ python_classes = Qing* *Qing python_files = test* python_functions = qing* *test xfail_strict = false markers = login: "login info" information: "information" index: "index"
此處我刪除了中文,是因為GBK編碼問題,不想處理了,直接刪除采用英文省事。 假如你實在解決不論編碼問題,就采用全英文吧。
到此這篇關于解析Pytest3種配置文件方式的文章就介紹到這了,更多相關Pytest 配置文件 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作示例
這篇文章主要介紹了Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作,結合實例形式分析了Python堡壘機模式執(zhí)行遠程命令的原理與相關操作技巧,需要的朋友可以參考下2019-05-05Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關示例,文中有詳細的代碼示例供大家參考,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-04-04關于numpy.concatenate()函數(shù)的使用及說明
這篇文章主要介紹了關于numpy.concatenate()函數(shù)的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Broadcast廣播機制在Pytorch Tensor Numpy中的使用詳解
python中Broadcast機制非常實用,在python中的廣播機制其實很簡單,下方主要介紹Broadcast廣播機制在Pytorch Tensor Numpy中的使用,希望對你有所幫助2022-08-08pandas進行時間數(shù)據(jù)的轉換和計算時間差并提取年月日
這篇文章主要介紹了pandas進行時間數(shù)據(jù)的轉換和計算時間差并提取年月日,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07Python實現(xiàn)刪除某列中含有空值的行的示例代碼
這篇文章主要介紹了Python實現(xiàn)刪除某列中含有空值的行的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07