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)文章
Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實(shí)例
今天小編就為大家分享一篇Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05python實(shí)現(xiàn)從wind導(dǎo)入數(shù)據(jù)
今天小編就為大家分享一篇python實(shí)現(xiàn)從wind導(dǎo)入數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python基礎(chǔ)知識(shí)+結(jié)構(gòu)+數(shù)據(jù)類型
這篇文章主要介紹了Python基礎(chǔ)知識(shí)+結(jié)構(gòu)+數(shù)據(jù)類型,文章基于python基礎(chǔ)知識(shí)圍繞主題展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05python爬蟲(chóng)指南之xpath實(shí)例解析(附實(shí)戰(zhàn))
在進(jìn)行網(wǎng)頁(yè)抓取的時(shí)候,分析定位html節(jié)點(diǎn)是獲取抓取信息的關(guān)鍵,目前我用的是lxml模塊,下面這篇文章主要給大家介紹了關(guān)于python爬蟲(chóng)指南之xpath實(shí)例解析的相關(guān)資料,需要的朋友可以參考下2022-01-01深入討論P(yáng)ython函數(shù)的參數(shù)的默認(rèn)值所引發(fā)的問(wèn)題的原因
這篇文章主要介紹了深入討論P(yáng)ython函數(shù)的參數(shù)的默認(rèn)值所引發(fā)的問(wèn)題的原因,利用了Python解釋器在內(nèi)存地址分配中的過(guò)程解釋了參數(shù)默認(rèn)值帶來(lái)陷阱的原因,需要的朋友可以參考下2015-03-03