99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

pytest 核心功能-用屬性標記測試函數(shù)

2022-03-21 09:19 更新

通過使用 ?pytest.mark? 助手,您可以輕松地在測試函數(shù)上設(shè)置元數(shù)據(jù),或者,您可以使用 ?CLI - pytest --markers? 列出所有標記,包括內(nèi)置標記和自定義標記。

以下是一些內(nèi)置標記:

  • ?usefixtures?——在測試函數(shù)或類上使用fixture
  • ?filterwarnings?—過濾測試函數(shù)的某些警告
  • ?skip?—總是跳過一個測試函數(shù)
  • ?skipif?-如果滿足某個條件,則跳過某個測試函數(shù)
  • ?Xfail?——如果滿足某個條件,則產(chǎn)生一個“預(yù)期失敗”的結(jié)果
  • ?parametrize?——對同一個測試函數(shù)執(zhí)行多個調(diào)用

創(chuàng)建自定義標記或?qū)擞洃?yīng)用于整個測試類或模塊很容易。 這些標記可以被插件使用,并且通常用于在命令行上使用 ?-m? 選項選擇測試。

標記只能應(yīng)用于測試,對?fixture?沒有影響。

注冊標記

您可以像這樣在 ?pytest.ini? 文件中注冊自定義標記:

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

或在您的 ?pyproject.toml? 文件中,如下所示:

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

請注意,標記名稱后 : 之后的所有內(nèi)容都是可選描述。

或者,您可以在 ?pytest_configure? 鉤子中以編程方式注冊新標記:

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "env(name): mark test to run only on named environment"
    )

注冊的標記出現(xiàn)在pytest的幫助文本中,不會發(fā)出警告。建議第三方插件總是注冊它們的標記。

在未知標記上引發(fā)錯誤

使用 ?@pytest.mark.name_of_the_mark? 裝飾器應(yīng)用的未注冊標記將始終發(fā)出警告,以避免由于輸入錯誤的名稱而默默地做一些令人驚訝的事情。 如上一節(jié)所述,您可以通過在 ?pytest.ini? 文件中注冊自定義標記或使用自定義 ?pytest_configure? 鉤子來禁用自定義標記的警告。

傳遞 ?--strict-markers? 命令行標志時,使用 ?@pytest.mark.name_of_the_mark? 裝飾器應(yīng)用的任何未知標記都將觸發(fā)錯誤。 您可以通過將 ?--strict-markers ?添加到 ?addopts ?來在項目中強制執(zhí)行此驗證:

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號