美文网首页
pytest-mark使用实例及运行方式

pytest-mark使用实例及运行方式

作者: C1R2 | 来源:发表于2020-10-13 22:39 被阅读0次

使用方法:
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.jb51.net/article/181093.htm

相关文章

  • pytest-mark使用实例及运行方式

    使用方法:1、注册标签名2、在测试用例/测试类前面加上:@pytest.mark.标签名打标记范围:测试用例、测试...

  • SpringBean管理——XML方式

    1.三种实例化Bean的方式 使用类构造器实例化(默认无参数) 使用静态工厂方式实例化(简单工厂模式) 使用实例工...

  • Java 数组

    Java 实例 - 数组排序及元素查找 以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 ...

  • 关于Python调用Tensorflow模型

    1.了解模型基本的加载和保存方式: tensorflow 模型封装使用 2.简洁的模型加载教程,实例清楚,可以运行...

  • synchronized

    1.解决的问题 解决多线程数据共享及同步 2.使用方式 2.1修饰实例方法 作用于当前实例,进入同步代码需要获取当...

  • 常用命令

    运行mysql实例(容器) 运行redis实例(容器) 运行nginx实例(容器) 运行php-fpm实例(容器)

  • 使用docker运行mysql实例

    希望在自己的机器模拟一下公司中微服务的构建,使用docker部署了mysql实例,使用spring boot进行了...

  • swift xib创建view时实例化

    方式一 Xib 自定义View实例化 使用 方式二 创建Bundle的通用方法 使用方式1 使用方式2 注意:

  • css之IE6 7bug的解决方案

    Bugs And FixedBugs及解决方案列表(*以下实例默认运行环境都为Standardmode): 如何在...

  • 《JavaScript高级程序设计》笔记5:引用类型

    Object类型 创建object实例方式一:使用new操作符跟Object构造函数 创建object实例方式二:...

网友评论

      本文标题:pytest-mark使用实例及运行方式

      本文链接:https://www.haomeiwen.com/subject/nigwghtx.html