解析Pytest3種配置文件方式
配置介紹
pytest 的主配置文件,可以改變 pytest 的默認(rèn)行為,執(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其實(shí)不止這些命令,我只是截取出本章最主要的部分。
配置案例
# pytest.ini [pytest] # 命令行執(zhí)行參數(shù) addopts = -vs # 排除目錄 norecursedirs = no_Case # 默認(rèn)執(zhí)行目錄 testpaths = ./ # 執(zhí)行規(guī)則-class python_classes = Test* # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = test* # xfail 標(biāo)志規(guī)則 xfail_strict = false # 自定義注冊(cè)標(biāo)志 markers = login: 登陸類標(biāo)志 information: 信息頁 index: 首頁
pytest.ini 中最好不要用中文,如果使用的話,請(qǐng)將文件編碼改成 gbk ,否則還請(qǐng)刪除ini配置文件中的中文。
目錄結(jié)構(gòu)
此處建議新建一個(gè)環(huán)境,如果你的pytest本就是一個(gè)新環(huán)境,沒有其他的東西,可以不用新建。因?yàn)榄h(huán)境包如果過多,會(huì)對(duì)運(yùn)行造成干擾。
pytest_ Case test_a.py no_Case test_b.py pytest.ini run.py
pytest.ini上面已經(jīng)展示了,看看run.py:
import pytest if __name__ == '__main__': pytest.main()
就是執(zhí)行入口,本章我們不用命令執(zhí)行了。此外還有兩個(gè)目錄就是Case跟no_Case,放用例的地方,
命令樣式講解
addopts
[pytest] addopts = -vs # ---等價(jià)于--- pytest.main(["-vs"])
addopts可以接收很多了參數(shù),換句話說main中能接收的參數(shù),此處都能寫。
目錄規(guī)則
# 排除目錄 norecursedirs = no_Case # 默認(rèn)執(zhí)行目錄 testpaths = ./
如果你發(fā)現(xiàn)目錄不論怎么改都沒有生效(能檢測(cè)到用例),那么就請(qǐng)按照你上面所說重新弄一個(gè)環(huán)境。如果環(huán)境OK了,可以檢測(cè)到目錄了,會(huì)報(bào)錯(cuò):拒絕訪問亦或者ERROR No escaped character,亦或者norecursedirs的目錄用例運(yùn)行了,那么都可以歸結(jié)于目錄路徑寫錯(cuò)了。 當(dāng)然,你可以通過控制默認(rèn)執(zhí)行目錄達(dá)到排除目錄的效果。
用例執(zhí)行規(guī)則
# 執(zhí)行規(guī)則-class python_classes = Test* # 執(zhí)行規(guī)則-py 文件 python_files = test* # 執(zhí)行規(guī)則-function python_functions = test*
當(dāng)然,pytest默認(rèn)設(shè)置的也是class檢測(cè)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 # 默認(rèn)執(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用例")
就能檢測(cè)到自定義的用例了,看看結(jié)果:
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
注意點(diǎn)
用例檢測(cè)這里,如果你寫了py,class,function,那么它會(huì)看著這樣的邏輯進(jìn)行檢測(cè),如果python_files都沒有檢測(cè)到了,剩下的python_classes以及python_functions也就不能進(jìn)行了。其次是python_classes如果有則優(yōu)先檢測(cè),如果沒有則檢測(cè)python_functions。
自定義標(biāo)志
pytest.ini 中最好不要用中文,如果使用的話,大家要將文件編碼改成 gbk
標(biāo)志名稱沒有限制,建議大家參考模塊命名,要有業(yè)務(wù)含義,不要隨心而寫
所有的自定義標(biāo)志,建議大家在 pytest.ini 中進(jìn)行統(tǒng)一管理和通過命令參數(shù)--strict-markers 進(jìn)行授權(quán)(pytest 其實(shí)不強(qiáng)制)
pytest 中的 markers 配置,相當(dāng)于我們對(duì)業(yè)務(wù)的一種設(shè)計(jì)歸類,尤其是大項(xiàng)目時(shí)非常重要
# 自定義注冊(cè)標(biāo)志 markers = login: 登陸類標(biāo)志 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用例")
那么如何運(yùn)行指定的標(biāo)志呢:
[pytest] # 命令行執(zhí)行參數(shù) addopts = -vs -m login # 或者 addopts = -vs -m "not login" # 或者 addopts = -vs -m "login or index"
"""not login結(jié)果示例"""
Case/test_a.py::Qing_A::qing_a 我是清安
PASSED
Case/test_a.py::Qing_A::qing_b 我是拾貳
PASSED
亂碼問題
有些人的情況或許跟我一樣,pytest.ini輸入的中文是亂碼或者讀取出來的是亂碼,這時(shí)候可以在設(shè)置中的:
修改成GBK即可。
小結(jié)
關(guān)于并未完全講完的一些參數(shù)可以來這里直接CTRL + F搜索:https://www.osgeo.cn/pytest/reference.html#ini-options-ref一定是pytest.ini文件嗎?其他的配置文件不行嗎。官方介紹到還有.toml,tox.ini,setup.cfg,其中setup.cfg是不被推薦使用的,官方文檔這樣說道:
??警告 用法 setup.cfg 除非用于非常簡(jiǎn)單的用例,否則不推薦使用。 .cfg 文件使用不同于 pytest.ini 和 tox.ini 這可能會(huì)導(dǎo)致難以追蹤的問題。如果可能,建議使用后一個(gè)文件,或者 pyproject.toml ,以保存pytest配置。
關(guān)于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:登陸類標(biāo)志", "information:信息頁", "index:首頁"]
如上是改寫的pytest.ini配置文件的。寫法上有些不一樣,注意點(diǎn)即可。此外關(guān)于官網(wǎng)的介紹,其實(shí)其他地方也可以改成類似于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:登陸類標(biāo)志", "information:信息頁", "index:首頁"]
關(guān)于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"
此處我刪除了中文,是因?yàn)镚BK編碼問題,不想處理了,直接刪除采用英文省事。 假如你實(shí)在解決不論編碼問題,就采用全英文吧。
到此這篇關(guān)于解析Pytest3種配置文件方式的文章就介紹到這了,更多相關(guān)Pytest 配置文件 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作,結(jié)合實(shí)例形式分析了Python堡壘機(jī)模式執(zhí)行遠(yuǎn)程命令的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Pytorch學(xué)習(xí)筆記DCGAN極簡(jiǎn)入門教程
網(wǎng)上GAN的教程太多了,這邊也談一下自己的理解,本文給大家介紹一下GAN的兩部分組成,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04python知識(shí):裝飾器@property到底有啥用途
這篇文章主要介紹了python裝飾器@property到底有啥用途,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法
這篇文章主要介紹了Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法,實(shí)例分析了Python讀取文件夾中文件及字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07關(guān)于numpy.concatenate()函數(shù)的使用及說明
這篇文章主要介紹了關(guān)于numpy.concatenate()函數(shù)的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Broadcast廣播機(jī)制在Pytorch Tensor Numpy中的使用詳解
python中Broadcast機(jī)制非常實(shí)用,在python中的廣播機(jī)制其實(shí)很簡(jiǎn)單,下方主要介紹Broadcast廣播機(jī)制在Pytorch Tensor Numpy中的使用,希望對(duì)你有所幫助2022-08-08pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日
這篇文章主要介紹了pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python實(shí)現(xiàn)刪除某列中含有空值的行的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)刪除某列中含有空值的行的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07