溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Pytest中使用mark

發(fā)布時(shí)間:2021-03-20 16:44:51 來源:億速云 閱讀:161 作者:Leah 欄目:開發(fā)技術(shù)

如何在Pytest中使用mark?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

使用方法:

1、注冊(cè)標(biāo)簽名

2、在測(cè)試用例/測(cè)試類前面加上:@pytest.mark.標(biāo)簽名

打標(biāo)記范圍:測(cè)試用例、測(cè)試類、模塊文件

注冊(cè)方式:

1、單個(gè)標(biāo)簽:

在conftest.py添加如下代碼:

def pytest_configure(config):
  # demo是標(biāo)簽名
  config.addinivalue_line("markers", "demo:示例運(yùn)行")

2、多個(gè)標(biāo)簽:

在conftest.py添加如下代碼:

def pytest_configure(config):
  marker_list = ["testdemo", "demo", "smoke"] # 標(biāo)簽名集合
  for markers in marker_list:
    config.addinivalue_line("markers", markers)

3、添加pytest.ini 配置文件(在你項(xiàng)目的任意一個(gè)文件下,新建一個(gè)file,文件命名為pytest.ini)

[pytest]
markers=
  smoke:this is a smoke tag
  demo:demo
  testdemo:testdemo

使用方法:

import pytest


@pytest.mark.testdemo
def test_demo01():
  print("函數(shù)級(jí)別的test_demo01")


@pytest.mark.smoke
def test_demo02():
  print("函數(shù)級(jí)別的test_demo02")


@pytest.mark.demo
class TestDemo:
  def test_demo01(self):
    print("test_demo01")

  def test_demo02(self):
    print("test_demo02")

運(yùn)行方式:

1、命令行模式

通過標(biāo)記表達(dá)式執(zhí)行
pytest -m demo
這條命令會(huì)執(zhí)行被裝飾器@pytest.mark.demo裝飾的所有測(cè)試用例

生成html報(bào)告:
pytest -m demo --html=Report/report.html

生成xml報(bào)告:
pytest -m demo --junitxml=Report/report.xml

運(yùn)行指定模塊:
pytest -m demo --html=Report/report.html TestCases/test_pytest.py

運(yùn)行指定測(cè)試目錄
pytest -m demo --html=Report/report.html TestCases/

通過節(jié)點(diǎn)id來運(yùn)行:
pytest TestCases/test_pytest.py::TestDemo::test_demo01

通過關(guān)鍵字表達(dá)式過濾執(zhí)行
pytest -k "MyClass and not method"
這條命令會(huì)匹配文件名、類名、方法名匹配表達(dá)式的用例

獲取用例執(zhí)行性能數(shù)據(jù)
獲取最慢的10個(gè)用例的執(zhí)行耗時(shí)
pytest --durations=10

2、新建run.py文件運(yùn)行,代碼如下:

pytest.main(["-m","demo","--html=Report/report.html"])

看完上述內(nèi)容,你們掌握如何在Pytest中使用mark的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI