Pytest mark使用實(shí)例及原理解析
這篇文章主要介紹了Pytest mark使用實(shí)例及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
使用方法:
1、注冊標(biāo)簽名
2、在測試用例/測試類前面加上:@pytest.mark.標(biāo)簽名
打標(biāo)記范圍:測試用例、測試類、模塊文件
注冊方式:
1、單個標(biāo)簽:
在conftest.py添加如下代碼:
def pytest_configure(config): # demo是標(biāo)簽名 config.addinivalue_line("markers", "demo:示例運(yùn)行")
2、多個標(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)目的任意一個文件下,新建一個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ù)級別的test_demo01") @pytest.mark.smoke def test_demo02(): print("函數(shù)級別的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 這條命令會執(zhí)行被裝飾器@pytest.mark.demo裝飾的所有測試用例 生成html報告: pytest -m demo --html=Report/report.html 生成xml報告: pytest -m demo --junitxml=Report/report.xml 運(yùn)行指定模塊: pytest -m demo --html=Report/report.html TestCases/test_pytest.py 運(yùn)行指定測試目錄 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" 這條命令會匹配文件名、類名、方法名匹配表達(dá)式的用例 獲取用例執(zhí)行性能數(shù)據(jù) 獲取最慢的10個用例的執(zhí)行耗時 pytest --durations=10
2、新建run.py文件運(yùn)行,代碼如下:
pytest.main(["-m","demo","--html=Report/report.html"])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實(shí)例
今天小編就為大家分享一篇Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05python實(shí)現(xiàn)從wind導(dǎo)入數(shù)據(jù)
今天小編就為大家分享一篇python實(shí)現(xiàn)從wind導(dǎo)入數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python基礎(chǔ)知識+結(jié)構(gòu)+數(shù)據(jù)類型
這篇文章主要介紹了Python基礎(chǔ)知識+結(jié)構(gòu)+數(shù)據(jù)類型,文章基于python基礎(chǔ)知識圍繞主題展開詳細(xì)內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05python爬蟲指南之xpath實(shí)例解析(附實(shí)戰(zhàn))
在進(jìn)行網(wǎng)頁抓取的時候,分析定位html節(jié)點(diǎn)是獲取抓取信息的關(guān)鍵,目前我用的是lxml模塊,下面這篇文章主要給大家介紹了關(guān)于python爬蟲指南之xpath實(shí)例解析的相關(guān)資料,需要的朋友可以參考下2022-01-01深入討論P(yáng)ython函數(shù)的參數(shù)的默認(rèn)值所引發(fā)的問題的原因
這篇文章主要介紹了深入討論P(yáng)ython函數(shù)的參數(shù)的默認(rèn)值所引發(fā)的問題的原因,利用了Python解釋器在內(nèi)存地址分配中的過程解釋了參數(shù)默認(rèn)值帶來陷阱的原因,需要的朋友可以參考下2015-03-03