Pytest mark使用实例及原理解析

作者:奔奔-武 时间:2021-02-27 14:30:11 

这篇文章主要介绍了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"])

来源:https://www.cnblogs.com/benben-wu/p/11542105.html

标签:py,test,mark
0
投稿

猜你喜欢

  • 关于Python中的main方法教程

    2021-12-30 08:31:37
  • JS实现的倒计时效果实例(2则实例)

    2023-08-23 17:12:05
  • python直接访问私有属性的简单方法

    2022-09-18 12:03:47
  • Python多进程同步简单实现代码

    2021-05-17 23:48:04
  • 分类、属性、关键词与Tag

    2009-08-27 12:57:00
  • 设计师的幸福

    2009-05-21 11:59:00
  • Python 命令行解析工具 argparse基本用法

    2023-06-15 01:34:46
  • Asp 单页查询数据库

    2010-05-11 20:11:00
  • asp如何制作一个防止多次刷新计数的图片计数器?

    2010-06-29 21:28:00
  • sql server对字段的添加修改删除、以及字段的说明

    2012-01-05 18:50:52
  • Python pandas之求和运算和非空值个数统计

    2023-11-19 03:04:59
  • Python实现简单状态框架的方法

    2022-08-20 14:13:44
  • 怎样缩小SQL Server数据库的日志文件

    2009-01-15 13:08:00
  • php简单浏览目录内容的实现代码

    2023-10-25 01:32:43
  • javascript权威指南,学习笔记,之运算符号

    2008-04-20 16:43:00
  • 怎样正确的解决 MySQL 中文模糊检索问题

    2008-12-19 17:26:00
  • Oracle时间日期操作方法小结

    2010-11-25 18:04:00
  • 10分钟用Python快速搭建全文搜索引擎详解流程

    2023-11-06 16:13:41
  • Python中使用items()方法返回字典元素对的教程

    2023-11-20 13:07:09
  • 如何实现SQL Server的分页显示?

    2010-05-18 18:36:00
  • asp之家 网络编程 m.aspxhome.com