一、前言
pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行
app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是ios用例,哪些是android的,运行代码时候指定mark名称运行就可以
二、mark标记
1.以下用例,标记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"])
只运行用webtest标记的测试,cmd运行的时候,加个-m 参数,指定参数值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 ====================
如果不想执行标记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'"])
运行结果
============================= 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 指定的函数节点id
如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面testmethod用例
每个test开头(或_test结尾)的用例,函数(或方法)的名称就是用例的节点id,指定节点id运行用-v 参数
$ pytest -v test_server.py::TestClass::test_method
pycharm运行代码
if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass::test_method"])
运行结果
============================= 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 ===========================
当然也能选择运行整个class
$ pytest -v test_server.py::TestClass
也能选择多个节点运行,多个节点中间空格隔开
$ pytest -v test_server.py::TestClass test_server.py::test_send_http
pycharm运行参考
if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass", "test_server.py::test_send_http"])
四、-k 匹配用例名称
可以使用-k命令行选项指定在匹配用例名称的表达式
$ 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 ==================
你也可以运行所有的测试,根据用例名称排除掉某些用例:
$ 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 ==================
也可以同时选择匹配 “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 ==================
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
【软件测试技术交流(资料分享)】:320231853(备注C)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=rS49sB1dBN6wjk4SbxAjX80YS65Zy8TH&authKey=tlP2KE7Sut5Dq7EvwkG55B%2B0sWc5WpLYbuRGFftTLHed0FB22lskhUs4Dnw6hQRP&noverify=0&group_code=320231853
生命不息,奋斗不止。每一份努力都不会被辜负,只要坚持不懈,终究会有回报。珍惜时间,追求梦想。不忘初心,砥砺前行。你的未来,由你掌握!
生命短暂,时间宝贵,我们无法预知未来会发生什么,但我们可以掌握当下。珍惜每一天,努力奋斗,让自己变得更加强大和优秀。坚定信念,执着追求,成功终将属于你!文章来源:https://www.toymoban.com/news/detail-511420.html
只有不断地挑战自己,才能不断地超越自己。坚持追求梦想,勇敢前行,你就会发现奋斗的过程是如此美好而值得。相信自己,你一定可以做到!文章来源地址https://www.toymoban.com/news/detail-511420.html
到了这里,关于从0到1精通自动化测试,pytest自动化测试框架,使用自定义标记mark(十一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!