" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytest如何使用mark的方法

 更新時(shí)間:2022年05月15日 09:27:46   作者:Redrose2100  
本文主要介紹了Pytest如何使用mark的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、常見的內(nèi)置markers

  • usefixtures - 為測(cè)試函數(shù)或者測(cè)試類知名使用那些fixture
  • filterwarnings - 為一個(gè)測(cè)試函數(shù)過濾一個(gè)指定的告警
  • skip - 跳過一個(gè)測(cè)試函數(shù)
  • skipif - 如果滿足條件就跳過測(cè)試函數(shù)
  • xfail - 標(biāo)記用例失敗
  • parametrize - 參數(shù)化

二、查看所有markers

如下,可以查看到當(dāng)前環(huán)境中的所有markers

$ pytest --markers
@pytest.mark.forked: Always fork for this test.

@pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs.

@pytest.mark.hypothesis: Tests which use hypothesis.

@pytest.mark.allure_label: allure label marker

@pytest.mark.allure_link: allure link marker

@pytest.mark.allure_description: allure description

@pytest.mark.allure_description_html: allure description html

@pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/stable/warnings.html#pytest-mark-filterwarnings

@pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.

@pytest.mark.skipif(condition, ..., *, reason=...): skip the given test function if any of the conditions evaluate to True. Example: skipif(sys.platform == 'win32') skip
s the test if we are on the win32 platform. See https://docs.pytest.org/en/stable/reference.html#pytest-mark-skipif

@pytest.mark.xfail(condition, ..., *, reason=..., run=True, raises=None, strict=xfail_strict): mark the test function as an expected failure if any of the conditions eva
luate to True. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expe
cted, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See https://docs.pytest.org/en/stable/reference.html#pytes
t-mark-xfail

@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of valu
es if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls o
f the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/stable/parametrize.html for more info and examples.

@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/stable/fixture.html#usefix
tures

@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.

@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.

三、注冊(cè)自定義marks

方式一:在pytest.ini中按照如下格式聲明即可,冒號(hào)之前為注冊(cè)的mark名稱,冒號(hào)之后為此mark的說明

[pytest]
markers =
    smoke: smoke tests
    test: system tests

此時(shí)test_demo.py代碼如下

import pytest

@pytest.mark.smoke
def test_func():
    assert 1==1

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

使用pytest -m smoke執(zhí)行結(jié)果如下,發(fā)現(xiàn)此時(shí)即只執(zhí)行了標(biāo)記為smoke的一個(gè)用例,這就是和自定義mark的使用方法

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=================================================================== 1 passed, 2 deselected in 0.04s ====================================================================

方式二:在conftest.py文件中重寫pytest_configure函數(shù)即可,比如如下,注冊(cè)兩個(gè)mark:smoke和test

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "smoke: smoke test"
    )
    config.addinivalue_line(
        "markers", "test: system test"
    )

test_demo.py代碼如下:

import pytest

@pytest.mark.smoke
def test_func():
    assert 1==1

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

通過pytest -m test 執(zhí)行結(jié)果如下:

$ pytest -m test
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=================================================================== 1 passed, 2 deselected in 0.02s ====================================================================

四、對(duì)未注冊(cè)mark的限制

默認(rèn)情況下,對(duì)未注冊(cè)mark直接使用是會(huì)產(chǎn)生一條告警信息,比如這里把pytest.ini和conftest.py都刪除掉,只剩test_demo.py一個(gè)文件

test_demo.py代碼如下

import pytest

@pytest.mark.smoke
def test_func():
    assert 1==1

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

直接使用 pytest -m smoke 執(zhí)行結(jié)果如下,可以發(fā)現(xiàn)這里產(chǎn)生了兩條告警,這就是因?yàn)檫@兩條告警未在pytest.ini或者conftest.py中進(jìn)行注冊(cè)的原因,在實(shí)際項(xiàng)目開發(fā)中如果在執(zhí)行測(cè)試的時(shí)候發(fā)現(xiàn)了這種大片告警打印,解決辦法就是在pytest.ini或者conftest.py將這些告警報(bào)出的mark都進(jìn)行注冊(cè)即可

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=========================================================================== warnings summary ===========================================================================
test_demo.py:3
  D:\src\blog\tests\test_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for deta
ils, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo.py:7
  D:\src\blog\tests\test_demo.py:7: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo?  You can register custom marks to avoid this warning - for detai
ls, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.test

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 1 passed, 2 deselected, 2 warnings in 0.02s ==============================================================

如果希望強(qiáng)制限制必須先注冊(cè)再使用mark,則可以在pytest.ini中加上如下配置即可

[pytest]
addopts = --strict-markers

比如test_demo.py代碼:

import pytest

@pytest.mark.smoke
def test_func():
    assert 1==1

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

此時(shí)繼續(xù)使用pytest -m smoke執(zhí)行結(jié)果如下,發(fā)現(xiàn)此時(shí)已經(jīng)報(bào)錯(cuò)了,即強(qiáng)制限制必須對(duì)mark進(jìn)行注冊(cè)

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 0 items / 1 error                                                                                                                                             

================================================================================ ERRORS ================================================================================
____________________________________________________________________ ERROR collecting test_demo.py _____________________________________________________________________
'smoke' not found in `markers` configuration option
======================================================================= short test summary info ========================================================================
ERROR test_demo.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 0.14s ===========================================================================

到此這篇關(guān)于Pytest如何使用mark的方法的文章就介紹到這了,更多相關(guān)Pytest使用mark內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解python列表(list)的使用技巧及高級(jí)操作

    詳解python列表(list)的使用技巧及高級(jí)操作

    這篇文章主要介紹了詳解python列表(list)的使用技巧及高級(jí)操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python3通過字符串訪問和修改局部變量的方法實(shí)例

    Python3通過字符串訪問和修改局部變量的方法實(shí)例

    最近在看python中nonlocal和global的使用,參考網(wǎng)上的大作,寫了點(diǎn)自己的心得,下面這篇文章主要給大家介紹了關(guān)于Python3通過字符串訪問和修改局部變量的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • python 圖像插值 最近鄰、雙線性、雙三次實(shí)例

    python 圖像插值 最近鄰、雙線性、雙三次實(shí)例

    這篇文章主要介紹了python 圖像插值 最近鄰、雙線性、雙三次實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • django在保存圖像的同時(shí)壓縮圖像示例代碼詳解

    django在保存圖像的同時(shí)壓縮圖像示例代碼詳解

    這篇文章主要介紹了django在保存圖像的同時(shí)壓縮圖像,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 使用keras時(shí)input_shape的維度表示問題說明

    使用keras時(shí)input_shape的維度表示問題說明

    這篇文章主要介紹了使用keras時(shí)input_shape的維度表示問題說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 淺析PHP與Python進(jìn)行數(shù)據(jù)交互

    淺析PHP與Python進(jìn)行數(shù)據(jù)交互

    本篇文章給大家分享了PHP與Python進(jìn)行數(shù)據(jù)交互的詳細(xì)方法以及重點(diǎn)點(diǎn)撥,有興趣的朋友可以學(xué)習(xí)下。
    2018-05-05
  • Win7上搭建Cocos2d-x 3.1.1開發(fā)環(huán)境

    Win7上搭建Cocos2d-x 3.1.1開發(fā)環(huán)境

    現(xiàn)在,越來越多的公司采用Cocos2d-x 3.0來開發(fā)游戲了,但是現(xiàn)在這樣的文章并不多,所以打算寫一系列來幫助初學(xué)者快速掌握Cocos2d-x 3.0。首先就從開發(fā)環(huán)境的大家說起吧
    2014-07-07
  • Python字典中items()函數(shù)案例詳解

    Python字典中items()函數(shù)案例詳解

    這篇文章主要介紹了Python字典中items()函數(shù)案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • python中wordcloud安裝的方法小結(jié)

    python中wordcloud安裝的方法小結(jié)

    這篇文章主要介紹了安裝python中wordcloud的幾種方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • PyQt5 關(guān)于Qt Designer的初步應(yīng)用和打包過程詳解

    PyQt5 關(guān)于Qt Designer的初步應(yīng)用和打包過程詳解

    Qt Designer中的操作方式十分靈活,其通過拖拽的方式放置控件可以隨時(shí)查看控件效果。這篇文章主要介紹了PyQt5 關(guān)于Qt Designer的初步應(yīng)用和打包,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

保亭| 互助| 洮南市| 景德镇市| 芒康县| 永福县| 晋城| 孟村| 杂多县| 桓台县| 德清县| 永仁县| 江安县| 卫辉市| 东乡| 夏邑县| 松原市| 格尔木市| 唐河县| 江津市| 辽阳县| 新泰市| 随州市| 涟水县| 郸城县| 云南省| 宁陵县| 南华县| 呼伦贝尔市| 会理县| 乌兰察布市| 南通市| 桐庐县| 青海省| 鄂伦春自治旗| 灵璧县| 东海县| 中西区| 咸丰县| 定西市| 图木舒克市|