亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解析Pytest3種配置文件方式

 更新時(shí)間:2024年02月26日 14:44:15   作者:愛吃 香菜  
pytest的主配置文件,可以改變pytest的默認(rèn)行為,本文主要介紹了解析Pytest3種配置文件方式,具有一定的參考價(jià)值,感興趣的可以了解一下

配置介紹

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)文章

最新評(píng)論