广告位联系
返回顶部
分享到

Pytest mark使用实例及原理解析

JavaScript 来源:互联网搜集 作者:秩名 发布时间:2020-02-22 19:43:26 人浏览
摘要

使用方法: 1、注册标签名 2、在测试用例/测试类前面加上:@pytest.mark.标签名 打标记范围:测试用例、测试类、模块文件 注册方式: 1、单个标签: 在conftest.py添加如下代码: 1 2 3 def pytest_configure(config): # demo是标签名 config.addinivalue_lin

使用方法:

1、注册标签名

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

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

注册方式:

1、单个标签:

在conftest.py添加如下代码:

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

2、多个标签:

在conftest.py添加如下代码:

1
2
3
4
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)

1
2
3
4
5
[pytest]
markers=
  smoke:this is a smoke tag
  demo:demo
  testdemo:testdemo

使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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、命令行模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
通过标记表达式执行
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文件运行,代码如下:

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


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。

您可能感兴趣的文章 :

原文链接 : https://www.cnblogs.com/benben-wu/p/11542105.html
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计