python中pytest教程_Pytest mark使用实例及原理解析

这篇文章主要介绍了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博客。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值