Pytest mark使用实例及原理解析


Posted in Python onFebruary 22, 2020

这篇文章主要介绍了Pytest mark使用实例及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

使用方法:

1、注册标签名

2、在测试用例/测试类前面加上:@pytest.mark.标签名

打标记范围:测试用例、测试类、模块文件

注册方式:

1、单个标签:

在conftest.py添加如下代码:

def pytest_configure(config):
  # demo是标签名
  config.addinivalue_line("markers", "demo:示例运行")

2、多个标签:

在conftest.py添加如下代码:

def pytest_configure(config):
  marker_list = ["testdemo", "demo", "smoke"] # 标签名集合
  for markers in marker_list:
    config.addinivalue_line("markers", markers)

3、添加pytest.ini 配置文件(在你项目的任意一个文件下,新建一个file,文件命名为pytest.ini)

[pytest]
markers=
  smoke:this is a smoke tag
  demo:demo
  testdemo:testdemo

使用方法:

import pytest


@pytest.mark.testdemo
def test_demo01():
  print("函数级别的test_demo01")


@pytest.mark.smoke
def test_demo02():
  print("函数级别的test_demo02")


@pytest.mark.demo
class TestDemo:
  def test_demo01(self):
    print("test_demo01")

  def test_demo02(self):
    print("test_demo02")

运行方式:

1、命令行模式

通过标记表达式执行
pytest -m demo
这条命令会执行被装饰器@pytest.mark.demo装饰的所有测试用例

生成html报告:
pytest -m demo --html=Report/report.html

生成xml报告:
pytest -m demo --junitxml=Report/report.xml

运行指定模块:
pytest -m demo --html=Report/report.html TestCases/test_pytest.py

运行指定测试目录
pytest -m demo --html=Report/report.html TestCases/

通过节点id来运行:
pytest TestCases/test_pytest.py::TestDemo::test_demo01

通过关键字表达式过滤执行
pytest -k "MyClass and not method"
这条命令会匹配文件名、类名、方法名匹配表达式的用例

获取用例执行性能数据
获取最慢的10个用例的执行耗时
pytest --durations=10

2、新建run.py文件运行,代码如下:

pytest.main(["-m","demo","--html=Report/report.html"])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python通过pil模块获得图片exif信息的方法
Mar 16 Python
Python基于回溯法子集树模板解决数字组合问题实例
Sep 02 Python
python语言中with as的用法使用详解
Feb 23 Python
selenium + python 获取table数据的示例讲解
Oct 13 Python
Python 最大概率法进行汉语切分的方法
Dec 14 Python
python可视化爬虫界面之天气查询
Jul 03 Python
python3使用print打印带颜色的字符串代码实例
Aug 22 Python
Python使用itchat模块实现群聊转发,自动回复功能示例
Aug 26 Python
手把手教你Python yLab的绘制折线图的画法
Oct 23 Python
Python中zip函数如何使用
Jun 04 Python
Pytorch 实现变量类型转换
May 17 Python
用 Python 定义 Schema 并生成 Parquet 文件详情
Sep 25 Python
python如何通过闭包实现计算器的功能
Feb 22 #Python
postman和python mock测试过程图解
Feb 22 #Python
Python实现链表反转的方法分析【迭代法与递归法】
Feb 22 #Python
Python实现队列的方法示例小结【数组,链表】
Feb 22 #Python
python实现从尾到头打印单链表操作示例
Feb 22 #Python
python实现滑雪游戏
Feb 22 #Python
Python实现栈的方法详解【基于数组和单链表两种方法】
Feb 22 #Python
You might like
php实现监听事件
2013/11/06 PHP
ThinkPHP中limit()使用方法详解
2016/04/19 PHP
PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
2016/11/14 PHP
Javascript中的相等与不等运算
2010/04/25 Javascript
解析JavaScript中的标签语句
2013/06/19 Javascript
JS中typeof与instanceof之间的区别总结
2013/11/14 Javascript
浅谈JavaScript中null和undefined
2015/07/09 Javascript
JavaScript运动减速效果实例分析
2015/08/04 Javascript
js分页之前端代码实现和请求处理
2017/08/04 Javascript
Vue.js的动态组件模板的实现
2018/11/26 Javascript
解析原来浏览器原生支持JS Base64编码解码
2019/08/12 Javascript
pygame学习笔记(2):画点的三种方法和动画实例
2015/04/15 Python
在Python中marshal对象序列化的相关知识
2015/07/01 Python
Python获取邮件地址的方法
2015/07/10 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
2015/12/14 Python
Python使用django搭建web开发环境
2017/06/09 Python
Python使用tkinter模块实现推箱子游戏
2019/10/08 Python
使用pyshp包进行shapefile文件修改的例子
2019/12/06 Python
Python流程控制常用工具详解
2020/02/24 Python
python numpy--数组的组合和分割实例
2020/02/24 Python
Python 格式化打印json数据方法(展开状态)
2020/02/27 Python
Python环境下安装PyGame和PyOpenGL的方法
2020/03/25 Python
使用Keras预训练模型ResNet50进行图像分类方式
2020/05/23 Python
Python eval函数介绍及用法
2020/11/09 Python
5分钟让你掌握css3阴影、倒影、渐变小技巧(小编推荐)
2016/08/15 HTML / CSS
希尔顿酒店官方网站:Hilton Hotels
2017/06/01 全球购物
用C#语言写出在本地创建一个UDP接收端口的具体过程
2016/02/22 面试题
小学生中国梦演讲稿
2014/04/23 职场文书
《三亚落日》教学反思
2014/04/26 职场文书
政治表现评语
2014/05/04 职场文书
学校政风行风评议心得体会
2014/10/21 职场文书
护士个人年终总结
2015/02/13 职场文书
学校证明范文
2015/06/24 职场文书
入党宣誓仪式主持词
2015/06/29 职场文书
2019年汽车租赁合同范本!
2019/08/12 职场文书
正确使用MySQL update语句
2021/05/26 MySQL