pytest_使用自定义标记mark
前言
pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行。app自动化的时候,如果想android和ios公用一套代码时,
也可以使用标记功能,标明哪些是ios用例,哪些是android的,运行代码时候指定mark名称运行就可以
mark标记
1.以下用例,标记test_send_http()为webtest
import pytest @pytest.mark.webtest
def test_send_http():
print("")# perform some webtest test for your app def test_something_quick():
print("") def test_another():
print("") class TestClass:
def test_merhed(self):
print("") if __name__ == '__main__':
pytest.main(['-s', 'mark_01.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:\YOYO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 4 items / 3 deselected mark_01.py 890
. =================== 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():
print("")# perform some webtest test for your app def test_something_quick():
print("") def test_another():
print("") class TestClass:
def test_merhed(self):
print("") if __name__ == '__main__':
pytest.main(['-s', 'mark_02.py', "-m='not webtest'"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items mark_02.py 890
.123
.456
.678
. ========================== 4 passed in 0.02 seconds ===========================
-v 指定的函数节点id
如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面test_method用例
每个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:\YOYO\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 ==================
pytest_使用自定义标记mark的更多相关文章
- pytest九:使用自定义标记 mark
pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行.app 自动化的时候,如果想android 和 ios 公用一套代码时,也可以使用标记功能,标明 ...
- pytest文档15-使用自定义标记mark
前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时, 也可以使用标记功能,标明哪些是 ...
- 自定义标记mark
前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是i ...
- Pytest使用自定义标记mark只执行部分用例
• 场景:只执行符合要求的某一部分用例 可以把一个web项目划分多个模块,然后指定模块名称执行. App自动化时,如果想Android和IOS公用一套代码时,也可以使用标记功能,标明哪些是IOS 的用 ...
- Pytest系列(8) - 使用自定义标记mark
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest 可以支持自定义 ...
- Pytest系列(八) - 自定义标记mark的使用
一.前言: pytest 支持自定义标记,自定义标记方便用例模块划分,也可以理解方便管理,配合标记参数 -m使用 二.示例代码 # -*- coding: utf-8 -*- # @Time : 20 ...
- pytest 14 使用自定义标记mark
标记失败用到的情况是,本身就知道这是失败的例子,所以,不用让他运行,直接跳过.或者是依赖于某个方法,某个方式失败的话,用例直接标记成失败. 标记失败有两种方法,一种是方法内部,一种是方法外部.内部用p ...
- pytest 13 使用自定义标记mark
前言: pytest可以规定那些要跑,那些不跑,跑特定的哪些?比如以下的这个例子: #!/usr/bin/env/python # -*-coding:utf-8-*- import pytest @ ...
- Pytest学习笔记6-自定义标记mark
前言 在pytest中,我们可以使用mark进行用例的自定义标记,通过不同的标记实现不同的运行策略 比如我们可以标记哪些用例是生产环境执行的,哪些用例是测试环境执行的,在运行代码的时候指定对应的mar ...
随机推荐
- Python回归分析五部曲(一)—简单线性回归
回归最初是遗传学中的一个名词,是由英国生物学家兼统计学家高尔顿首先提出来的,他在研究人类身高的时候发现:高个子回归人类的平均身高,而矮个子则从另一方向回归人类的平均身高: 回归分析整体逻辑 回归分析( ...
- mvel语法指南[翻译]
mvel受到了java语法的启发,但是存在一些根本性的差异,mvel旨在使其成为更有效的表达式语言.比如直接支持集合.数组和字符串匹配,正则表达式的运算操作等. mvel2.x有以下几个部分组成: ...
- Python中如何计算字符串里面某一个字符出现的次数?
一个突发奇想,想解决一个学习中的行业痛点,让我又再度拾起了Python编程语言的学习. 刚学两天,今天遇到一个题,该题难度不高,但有一点关键点在网上找不到,网上也没有相关的答案,于是我只好千辛万苦 ...
- 虚拟机 /dev/mapper/centos-root 动态扩容
[root@bogon ~]# df -h Filesystem Size Used Avail Use% Mounted on .2G .2G 51M % / devtmpfs 908M 908M ...
- 深入理解C++中的mutable,using,decltype等关键字
深入理解C++中的mutable关键字 mutable的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设 ...
- paxos(mysql)
https://web.stanford.edu/~ouster/cgi-bin/papers/OngaroPhD.pdf https://raft.github.io/raft.pdf 如何浅显易懂 ...
- Difference Between Accuracy and Precision
What Is the Difference Between Accuracy and Precision? https://www.thoughtco.com/difference-between- ...
- [错误解决] Libreoffice转换不成功,直接不做任何操作
问题描述: Libreoffice在版本5.3.0之前都存在这个问题.现象是:当你运行其中一个LibreOffice的时候,再运行另外一个Libreoffice转换时,将不做任何操作. 解决方案: 如 ...
- Elasticsearch学习笔记——索引模板
在索引模板里面,date类型的字段的format支持多种类型,在es中全部会转换成long类型进行存储,参考 https://zhuanlan.zhihu.com/p/34240906 一个索引模板范 ...
- Hive学习笔记——metadata
Hive结构体系 https://blog.csdn.net/zhoudaxia/article/details/8855937 可以在hive的jdbc接口中使用getMetaData方法来获取hi ...