Pytest mark使用實(shí)例及原理解析
這篇文章主要介紹了Pytest mark使用實(shí)例及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
使用方法:
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、命令行模式
通過(guò)標(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/ 通過(guò)節(jié)點(diǎn)id來(lái)運(yùn)行: pytest TestCases/test_pytest.py::TestDemo::test_demo01 通過(guò)關(guān)鍵字表達(dá)式過(guò)濾執(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"])
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)網(wǎng)絡(luò)端口轉(zhuǎn)發(fā)和重定向的方法
這篇文章主要介紹了Python實(shí)現(xiàn)網(wǎng)絡(luò)端口轉(zhuǎn)發(fā)和重定向的方法,結(jié)合實(shí)例形式分析了Python基于threading和socket模塊實(shí)現(xiàn)端口轉(zhuǎn)發(fā)與重定向的具體操作技巧,需要的朋友可以參考下2016-09-09
Python for循環(huán)搭配else常見問(wèn)題解決
這篇文章主要介紹了Python for循環(huán)搭配else常見問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
selenium 安裝與chromedriver安裝的方法步驟
這篇文章主要介紹了selenium 安裝與chromedriver安裝的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
在Windows系統(tǒng)上搭建Nginx+Python+MySQL環(huán)境的教程
這篇文章主要介紹了在Windows系統(tǒng)上搭建Nginx+Python+MySQL環(huán)境的教程,文中使用flup中間件及FastCGI方式連接,需要的朋友可以參考下2015-12-12
Python中如何將Tqdm與Asyncio結(jié)合使用呢
這篇文章主要和大家詳細(xì)介紹了在Python中如何將Tqdm與Asyncio結(jié)合使用呢,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
Python使用虛擬環(huán)境(安裝下載更新卸載)命令
這篇文章主要為大家介紹了Python使用虛擬環(huán)境(安裝下載更新卸載)命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

