一、前言
pytest可以支持自定義標(biāo)記,自定義標(biāo)記可以把一個(gè)web項(xiàng)目劃分多個(gè)模塊,然后指定模塊名稱執(zhí)行
app自動(dòng)化的時(shí)候,如果想android和ios公用一套代碼時(shí),也可以使用標(biāo)記功能,標(biāo)明哪些是ios用例,哪些是android的,運(yùn)行代碼時(shí)候指定mark名稱運(yùn)行就可以
二、mark標(biāo)記
1.以下用例,標(biāo)記test_send_http()為webtest
# content of test_server.py
'''程序員曦曦'''
import pytest
@pytest.mark.webtest
def test_send_http():
pass # perform some webtest test for your app
def test_something_quick():
pass
def test_another():
pass
class TestClass:
def test_method(self):
pass
if __name__ == "__main__":
pytest.main(["-s", "test_server.py", "-m=webtest"])
只運(yùn)行用webtest標(biāo)記的測試,cmd運(yùn)行的時(shí)候,加個(gè)-m 參數(shù),指定參數(shù)值webtest
$ pytest -v -m webtest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\MOMO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 4 items / 3 deselected
test_server.py .
=================== 1 passed, 3 deselected in 0.10 seconds ====================
如果不想執(zhí)行標(biāo)記webtest的用例,那就用 " not webtest "
$ pytest -v -m "not webtest"
import pytest
'''程序員曦曦'''
@pytest.mark.webtest
def test_send_http():
pass # perform some webtest test for your app
def test_something_quick():
pass
def test_another():
pass
class TestClass:
def test_method(self):
pass
if __name__ == "__main__":
pytest.main(["-s", "test_server.py", "-m='not webtest'"])
運(yùn)行結(jié)果
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\MOMO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 4 items
test_server.py ....
========================== 4 passed in 0.06 seconds ===========================
三、-v 指定的函數(shù)節(jié)點(diǎn)id
如果想指定運(yùn)行某個(gè).py模塊下,類里面的一個(gè)用例,如:TestClass里面testmethod用例
每個(gè)test開頭(或_test結(jié)尾)的用例,函數(shù)(或方法)的名稱就是用例的節(jié)點(diǎn)id,指定節(jié)點(diǎn)id運(yùn)行用-v 參數(shù)
$ pytest -v test_server.py::TestClass::test_method
pycharm運(yùn)行代碼
if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass::test_method"])
運(yùn)行結(jié)果
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- E:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0'}, 'JAVA_HOME': 'D:\\java\\jdk17'}
rootdir: E:\MOMO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collecting ... collected 1 item
test_server.py::TestClass::test_method PASSED [100%]
========================== 1 passed in 0.06 seconds ===========================
當(dāng)然也能選擇運(yùn)行整個(gè)class
$ pytest -v test_server.py::TestClass
也能選擇多個(gè)節(jié)點(diǎn)運(yùn)行,多個(gè)節(jié)點(diǎn)中間空格隔開
$ pytest -v test_server.py::TestClass test_server.py::test_send_http
pycharm運(yùn)行參考
if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass", "test_server.py::test_send_http"])
四、-k 匹配用例名稱
可以使用-k命令行選項(xiàng)指定在匹配用例名稱的表達(dá)式
$ pytest -v -k http
$ pytest -v -k http # running with the above defined example module
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 3 deselected
test_server.py::test_send_http PASSED [100%]
================== 1 passed, 3 deselected in 0.12 seconds ==================
你也可以運(yùn)行所有的測試,根據(jù)用例名稱排除掉某些用例:
$ pytest -k “not send_http” -v
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 1 deselected
test_server.py::test_something_quick PASSED [ 33%]
test_server.py::test_another PASSED [ 66%]
test_server.py::TestClass::test_method PASSED [100%]
================== 3 passed, 1 deselected in 0.12 seconds ==================
也可以同時(shí)選擇匹配 “http” 和“quick”
$ pytest -k “http or quick” -v
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 2 deselected
test_server.py::test_send_http PASSED [ 50%]
test_server.py::test_something_quick PASSED [100%]
================== 2 passed, 2 deselected in 0.12 seconds ==================
最后感謝每一個(gè)認(rèn)真閱讀我文章的人,禮尚往來總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走:
【軟件測試技術(shù)交流(資料分享)】:320231853(備注C)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=rS49sB1dBN6wjk4SbxAjX80YS65Zy8TH&authKey=tlP2KE7Sut5Dq7EvwkG55B%2B0sWc5WpLYbuRGFftTLHed0FB22lskhUs4Dnw6hQRP&noverify=0&group_code=320231853
生命不息,奮斗不止。每一份努力都不會(huì)被辜負(fù),只要堅(jiān)持不懈,終究會(huì)有回報(bào)。珍惜時(shí)間,追求夢想。不忘初心,砥礪前行。你的未來,由你掌握!
生命短暫,時(shí)間寶貴,我們無法預(yù)知未來會(huì)發(fā)生什么,但我們可以掌握當(dāng)下。珍惜每一天,努力奮斗,讓自己變得更加強(qiáng)大和優(yōu)秀。堅(jiān)定信念,執(zhí)著追求,成功終將屬于你!文章來源:http://www.zghlxwxcb.cn/news/detail-511420.html
只有不斷地挑戰(zhàn)自己,才能不斷地超越自己。堅(jiān)持追求夢想,勇敢前行,你就會(huì)發(fā)現(xiàn)奮斗的過程是如此美好而值得。相信自己,你一定可以做到!文章來源地址http://www.zghlxwxcb.cn/news/detail-511420.html
到了這里,關(guān)于從0到1精通自動(dòng)化測試,pytest自動(dòng)化測試框架,使用自定義標(biāo)記mark(十一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!