" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest如何使用mark的方法

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

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

  • usefixtures - 為測(cè)試函數(shù)或者測(cè)試類知名使用那些fixture
  • filterwarnings - 為一個(gè)測(cè)試函數(shù)過(guò)濾一個(gè)指定的告警
  • skip - 跳過(guò)一個(gè)測(cè)試函數(shù)
  • skipif - 如果滿足條件就跳過(guò)測(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的說(shuō)明

[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

通過(guò)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 分發(fā)包中添加額外文件的方法

    Python 分發(fā)包中添加額外文件的方法

    這篇文章主要介紹了Python 分發(fā)包中添加額外文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python使用Nocalhost并開啟debug調(diào)試的方法

    Python使用Nocalhost并開啟debug調(diào)試的方法

    Nocalhost是一種開發(fā)者工具,支持針對(duì)Kubernetes應(yīng)用程序進(jìn)行調(diào)試和部署,這篇文章主要介紹了Python怎么使用Nocalhost并開啟debug,需要的朋友可以參考下
    2023-04-04
  • python繪制二維直方圖的代碼實(shí)現(xiàn)

    python繪制二維直方圖的代碼實(shí)現(xiàn)

    本文主要介紹了python繪制二維直方圖的代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 使用Python的Scrapy框架編寫web爬蟲的簡(jiǎn)單示例

    使用Python的Scrapy框架編寫web爬蟲的簡(jiǎn)單示例

    這篇文章主要介紹了使用Python的Scrapy框架編寫web爬蟲的簡(jiǎn)單示例,使用Python編寫爬蟲是Python應(yīng)用方面最得意的利器,Scrapy框架正是為爬蟲而生,需要的朋友可以參考下
    2015-04-04
  • Python PyYAML庫(kù)解析YAML文件使用詳解

    Python PyYAML庫(kù)解析YAML文件使用詳解

    這篇文章主要為大家介紹了Python PyYAML庫(kù)解析YAML文件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • pandas值替換方法

    pandas值替換方法

    今天小編就為大家分享一篇pandas值替換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • python神經(jīng)網(wǎng)絡(luò)編程實(shí)現(xiàn)手寫數(shù)字識(shí)別

    python神經(jīng)網(wǎng)絡(luò)編程實(shí)現(xiàn)手寫數(shù)字識(shí)別

    這篇文章主要為大家詳細(xì)介紹了python神經(jīng)網(wǎng)絡(luò)編程實(shí)現(xiàn)手寫數(shù)字識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Python制作爬蟲采集小說(shuō)

    Python制作爬蟲采集小說(shuō)

    本文給大家分享的是使用Python制作爬蟲采集小說(shuō)的代碼,非常的簡(jiǎn)單實(shí)用,雖然還是有點(diǎn)瑕疵,大家一起改改,共同進(jìn)步
    2015-10-10
  • 解決win7操作系統(tǒng)Python3.7.1安裝后啟動(dòng)提示缺少.dll文件問題

    解決win7操作系統(tǒng)Python3.7.1安裝后啟動(dòng)提示缺少.dll文件問題

    這篇文章主要介紹了解決win7操作系統(tǒng)Python3.7.1安裝后啟動(dòng)提示缺少.dll文件問題,本文給大家提供兩種解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 簡(jiǎn)單說(shuō)明Python中的裝飾器的用法

    簡(jiǎn)單說(shuō)明Python中的裝飾器的用法

    這篇文章主要簡(jiǎn)單說(shuō)明了Python中的裝飾器的用法,裝飾器在Python的進(jìn)階學(xué)習(xí)中非常重要,示例代碼基于Python2.x,需要的朋友可以參考下
    2015-04-04

最新評(píng)論