解析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: 信息頁(yè)
index: 首頁(yè)
pytest.ini 中最好不要用中文,如果使用的話,請(qǐng)將文件編碼改成 gbk ,否則還請(qǐng)刪除ini配置文件中的中文。
目錄結(jié)構(gòu)
此處建議新建一個(gè)環(huán)境,如果你的pytest本就是一個(gè)新環(huán)境,沒有其他的東西,可以不用新建。因?yàn)榄h(huán)境包如果過(guò)多,會(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ù),換句話說(shuō)main中能接收的參數(shù),此處都能寫。
目錄規(guī)則
# 排除目錄 norecursedirs = no_Case # 默認(rèn)執(zhí)行目錄 testpaths = ./
如果你發(fā)現(xiàn)目錄不論怎么改都沒有生效(能檢測(cè)到用例),那么就請(qǐng)按照你上面所說(shuō)重新弄一個(gè)環(huán)境。如果環(huán)境OK了,可以檢測(cè)到目錄了,會(huì)報(bào)錯(cuò):拒絕訪問(wèn)亦或者ERROR No escaped character,亦或者norecursedirs的目錄用例運(yùn)行了,那么都可以歸結(jié)于目錄路徑寫錯(cuò)了。 當(dāng)然,你可以通過(guò)控制默認(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)一管理和通過(guò)命令參數(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: 信息頁(yè)
index: 首頁(yè)
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
亂碼問(wèn)題
有些人的情況或許跟我一樣,pytest.ini輸入的中文是亂碼或者讀取出來(lái)的是亂碼,這時(shí)候可以在設(shè)置中的:

修改成GBK即可。
小結(jié)
關(guān)于并未完全講完的一些參數(shù)可以來(lái)這里直接CTRL + F搜索:https://www.osgeo.cn/pytest/reference.html#ini-options-ref一定是pytest.ini文件嗎?其他的配置文件不行嗎。官方介紹到還有.toml,tox.ini,setup.cfg,其中setup.cfg是不被推薦使用的,官方文檔這樣說(shuō)道:
??警告 用法 setup.cfg 除非用于非常簡(jiǎn)單的用例,否則不推薦使用。 .cfg 文件使用不同于 pytest.ini 和 tox.ini 這可能會(huì)導(dǎo)致難以追蹤的問(wèn)題。如果可能,建議使用后一個(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:信息頁(yè)", "index:首頁(yè)"]
如上是改寫的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:信息頁(yè)", "index:首頁(yè)"]
關(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編碼問(wèn)題,不想處理了,直接刪除采用英文省事。 假如你實(shí)在解決不論編碼問(wèn)題,就采用全英文吧。
到此這篇關(guān)于解析Pytest3種配置文件方式的文章就介紹到這了,更多相關(guān)Pytest 配置文件 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python的內(nèi)置數(shù)據(jù)類型中的數(shù)字
這篇文章主要介紹Python內(nèi)置數(shù)據(jù)類型中的數(shù)字(Number),包括整數(shù)(int),小數(shù)(float),復(fù)數(shù)(Complex),布爾類型(bool)這幾種數(shù)據(jù)類型。本文介紹的都是Python3.x中的數(shù)據(jù)類型,需要的朋友請(qǐng)參考下面文章2021-09-09
Python3實(shí)現(xiàn)定時(shí)任務(wù)的四種方式
Python實(shí)現(xiàn)定點(diǎn)與定時(shí)任務(wù)方式比較多,找到下面四中實(shí)現(xiàn)方式,每個(gè)方式都有自己應(yīng)用場(chǎng)景;下面來(lái)快速介紹Python中常用的定時(shí)任務(wù)實(shí)現(xiàn)方式,一起看看吧2019-06-06
python常見問(wèn)題之ModuleNotFoundError: No module nam
這篇文章主要介紹了python常見問(wèn)題之ModuleNotFoundError: No module named ‘rest_framework‘解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-07-07
詳解tf.device()指定tensorflow運(yùn)行的GPU或CPU設(shè)備實(shí)現(xiàn)
這篇文章主要介紹了詳解tf.device()指定tensorflow運(yùn)行的GPU或CPU設(shè)備實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
詳解如何為eclipse安裝合適版本的python插件pydev
這篇文章主要介紹了詳解如何為eclipse安裝合適版本的python插件pydev,pydev是一款優(yōu)秀的Eclipse插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Python基礎(chǔ)學(xué)習(xí)之函數(shù)和代碼復(fù)用詳解
函數(shù)能提高應(yīng)用的模塊性,和代碼的重復(fù)利用率,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)學(xué)習(xí)之函數(shù)和代碼復(fù)用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

