pytest文档15-使用自定义标记mark
前言
pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行。一个大项目自动化用例时,可以划分多个模块,
也可以使用标记功能,标明哪些是模块1用例,哪些是模块2的,运行代码时候指定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:\YOYO\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:\YOYO\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里面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结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang
pytest文档15-使用自定义标记mark的更多相关文章
- Pytest系列(八) - 自定义标记mark的使用
一.前言: pytest 支持自定义标记,自定义标记方便用例模块划分,也可以理解方便管理,配合标记参数 -m使用 二.示例代码 # -*- coding: utf-8 -*- # @Time : 20 ...
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- Word文档中的格式标记大全
在Word中有很多的格式设置,很多格式设置都会有一些标记,这些标记是隐藏的,在打印文档时是不会打印出来的,但是它们却起着结构化文档的大作用.如果你在编辑文档,不妨点击格式标记开关,看看都有哪些格式标记 ...
- HTML文档的经常使用标记
一.HTML文档中经常使用的标记有文字标记.段落标记.列表标记.超链接标记.图像标记.表格标记.框架标记和多媒体标记,以下对这些经常使用标记进行介绍: 1.文字标记:文字是网页重要的组成部分之中的一个 ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- pytest九:使用自定义标记 mark
pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行.app 自动化的时候,如果想android 和 ios 公用一套代码时,也可以使用标记功能,标明 ...
- Pytest使用自定义标记mark只执行部分用例
• 场景:只执行符合要求的某一部分用例 可以把一个web项目划分多个模块,然后指定模块名称执行. App自动化时,如果想Android和IOS公用一套代码时,也可以使用标记功能,标明哪些是IOS 的用 ...
- Pytest系列(8) - 使用自定义标记mark
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest 可以支持自定义 ...
- pytest文档1-环境准备与入门
前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...
随机推荐
- InterSystems Ensemble学习笔记(一) Ensemble介绍及安装
系列目录 InterSystems Ensemble学习笔记(一) Ensemble介绍及安装InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移 一 ...
- INNODB表快速迁移
本实验在一台server上启动了2个mysql实例端口分别是3307 3308,目的是将3307的表aaa迁移到3308中去,并打开3308的slave 1.在3308上 mysql> dr ...
- day5模块学习--yaml文件处理
yaml文件处理(http://pyyaml.org/wiki/PyYAMLDocumentation) 摘要: 本文讲的是yaml在python上的使用教程详解, YAML是一种容易人类阅读 ...
- 【LOJ】#2066. 「SDOI2016」墙上的句子
题解 我一直也不会网络流--orz 我们分析下这道题,显然和行列没啥关系,就是想给你n + m个串 那么我们对于非回文单词之外的单词,找到两两匹配的反转单词(即使另一个反转单词不会出现也要建出来) 具 ...
- Elasticsearch环境准备(一)
一.ELKStack简介 中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details ELK Stack包含:Elasti ...
- 【知了堂学习笔记】java 编写几种常见排序算法
排序的分类: 一.交换排序 所谓交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动. 1.冒泡 ...
- .NET常用的异常类型及其中文说明
基异常类型: 类 说明 System.Exception 所有异常的基类型 System.ApplicationException 发生非致命应用程序错误时引发的异常 System.SystemExc ...
- Java中面向对象的理解
按照惯例,先做一个简单的介绍,现在开始学习 Thinging in Java 4 ,一边看,一边记录,我都不想给自己设定时间安排了,毕竟很少实现过.所以就这样吧!不定期的更新,我都会放到博客中的. 所 ...
- mysql正则表达式,实现多个字段匹配多个like模糊查询
现在有这么一个需求 一个questions表,字段有题目(TestSubject),选项(AnswerA,AnswerB,AnswerC,AnswerD,AnswerE) 要求字段不包含png,jpg ...
- 理解Linux的进程,线程,PID,LWP,TID,TGID
在Linux的top和ps命令中,默认看到最多的是pid (process ID),也许你也能看到lwp (thread ID)和tgid (thread group ID for the threa ...