1. 程式人生 > 實用技巧 >pytest內建mark標記說明

pytest內建mark標記說明

標記只能應用於測試,對fixtures沒有影響。

在pytest.ini檔案中註冊marker標記

[pytest]
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    serial

:冒號後面的都是可選描述

已註冊的標記不會引發警告

使用@ pytest.mark.name_of_the_mark裝飾器應用的未註冊標記(未知標記)將始終發出警告

當在命令列使用 --strict-markers 引數時,未註冊標記將觸發error

---------------------------------

@pytest.mark.run:

指定測試的順序資訊。由pytest-ordering提供

specify ordering information for when tests should run in relation to one another. Provided by pytest-ordering. See also: http://pytest-ordering.readthedocs.org/

@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(reason =“目前無法對此進行測試”)

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=...)

:

如果滿足特定條件,則跳過測試功能

示例:skipif(sys.platform =='win32')

skip the given test function if any of the conditions evaluate to True. Example: skipif(sys.platform == 'win32') skips 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):

如果滿足特定條件,則會產生“預期的失敗”結果。可選指定原因

如果不想執行測試功能,則將run= False。

如果只期望特定的例外情況,則可以在raises中列出他們,並且如果測試以其他方式失敗,將被報告為真正的失敗。

mark the test function as an expected failure if any of the conditions evaluate 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 expected, 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#pytest-mark-xfail

@pytest.mark.parametrize(argnames, argvalues):

對同一測試功能執行多次呼叫,依次傳遞不同的引數。

如果argnames僅指定一個名稱,則argvalues通常需要是值的列表;如果argnames指定多個名稱,則argvalues的值通常是元組列表。

示例:@parametrize('arg1',[1,2])將導致函式的兩次呼叫,一次呼叫arg1 = 1,
另一次arg1 = 2。

call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values 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 of 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, ...):

將測試標記為需要所有指定的fixtures。

mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/stable/fixture.html#usefixtures

@pytest.mark.tryfirst:

標記一個hook實現函式,以便外掛機制將嘗試首先/儘早呼叫它

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

@pytest.mark.trylast:

標記一個hook實現函式,以便外掛機制將嘗試最後/盡晚呼叫它

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