如何在Pytest中使用mark

发布时间:2021-03-20 16:44:51 作者:Leah
来源:亿速云 阅读:157

如何在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"])

看完上述内容,你们掌握如何在Pytest中使用mark的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 如如何使用journalctl命令?
  2. html中mark标签如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pytest mark

上一篇:怎么在C++中使用set_new_handler函数

下一篇:php中与html标签相关的函数有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》