测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----注册使用自定义mark标签
收藏本文
作者:redrose2100 类别: 日期:2022-09-07 10:01:08 阅读:1055 次 消耗积分:0 分
[TOC]  # 一、直接使用自定义mark标签 在pytest中,可以通过@pytest.mark.xxx的方式直接自定义标签使用,比如欲对一个测试函数增加smoke标签,即直接在测试函数上使用@pytest.mark.smoke即可,如下代码: ```python import pytest @pytest.mark.smoke def test_01(): assert 1==1 ``` 对测试函数增加自定义的标签的作用是对自动化脚本进行分类标记,比如在测试活动常常分为冒烟测试、功能测试、系统测试、性能测试等,那么测试用例也同样的分为冒烟测试用例、功能测试用例、系统测试用例、性能测试用例,因此自动化脚本也有必要同步保持自动化脚本的分类,pytest中的mark主要就是为了实现这样一个功能的,在正式使用mark对自动化脚本标记之前,先看下上面这个自动化脚本执行结果。执行结果如下图所示,这里执行成功了,但是可以看到一条告警信息,从告警信息可以看出,此时smoke标签为未识别标签,即这里没有注册,而是直接使用的原因。 ```bash (demo--ip5ZZo0) D:\demo>pytest =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 1 item test_demo.py . [100%] ==================== warnings summary ===================== test_demo.py:2 D:\demo\test_demo.py:2: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html @pytest.mark.smoke -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ============== 1 passed, 1 warning in 0.09s =============== (demo--ip5ZZo0) D:\demo> ``` 下面再写一个测试函数,代码如下,即此时通过mark直接定义smoke和function两个标签,然后验证一下在不注册的情况下,通过pytest命令是否可以做到挑选用例执行。 ```python import pytest @pytest.mark.smoke def test_01(): assert 1==1 @pytest.mark.function def test_02(): assert 1==1 ``` pytest命令可以通过-m 参数指定标签来挑选脚本执行,如通过 pytest -m smoke 命令可以进行挑选打了smoke标签的脚本执行,执行结果如下图所示,可以发现此时虽然未对smoke和function标签进行注册,当仍然可以通过指定标签的方式挑选测试脚本执行。即不注册而直接使用标签的一个不好的地方就是会显示告警。后面将继续展示通过注册标签然后再使用的情形。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m smoke =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 2 items / 1 deselected / 1 selected test_demo.py . [100%] ==================== warnings summary ===================== test_demo.py:3 D:\demo\test_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html @pytest.mark.smoke test_demo.py:8 D:\demo\test_demo.py:8: PytestUnknownMarkWarning: Unknown pytest.mark.function - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html @pytest.mark.function -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ======= 1 passed, 1 deselected, 2 warnings in 0.10s ======= (demo--ip5ZZo0) D:\demo> ``` # 二、通过conftest.py文件中重写pytest_configure函数注册标签 在测试脚本根目录创建conftest.py文件,然后在conftest.py文件中重写pytest_configure函数的方式可以注册标签,比如下代码为注册了smoke标签和function标签,这里函数名是固定的,即pytest_configure,参数也是固定的,即config,唯一需要变更的就是注册的标签的名字和描述。 ```python def pytest_configure(config): config.addinivalue_line( "markers", "smoke: smoke test" ) config.addinivalue_line( "markers", "function: system test" ) ``` 在对pytest_configure函数重写后,此时再编写测试函数,如下分别为test_01和test_02使用smoke和function进行标记。 ```python import pytest @pytest.mark.smoke def test_01(): assert 1==1 @pytest.mark.function def test_02(): assert 1==1 ``` 此时脚本的执行结果如下图所示,即告警消除了,同时也做到了挑选smoke标签的脚本执行了。因此在企业里倘若在执行脚本的时候发现有大量的提示未知标签的告警,那么就是未对告警进行注册的原因。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m smoke =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 2 items / 1 deselected / 1 selected test_demo.py . [100%] ============= 1 passed, 1 deselected in 0.07s ============= (demo--ip5ZZo0) D:\demo> ``` # 三、通过pytest.ini文件配置注册标签 除了通过在conftest.py文件中重写pytest_configure函数,pytest还提供了一种更加便捷的注册标签的方式,即在pytest.ini配置文件中配置,配置的语法也更加简洁,即在测试脚本的根目录创建pytest.ini文件,比如同样注册smoke和function标签,只需要在pytest.ini中编写如下配置即可。 ```bash [pytest] markers = smoke: smoke tests function: function tests ``` 此时继续使用如下测试函数来验证,即分别为test_01和test_02使用smoke和function打标签。 ```python import pytest @pytest.mark.smoke def test_01(): assert 1==1 @pytest.mark.function def test_02(): assert 1==1 ``` 通过pytest -m smoke挑选打了smoke标签的测试函数执行,执行结果如下图所示,可以发现此时同样不会打印告警,即注册标签生效了。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m smoke =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo, configfile: pytest.ini plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 2 items / 1 deselected / 1 selected test_demo.py . [100%] ============= 1 passed, 1 deselected in 0.09s ============= (demo--ip5ZZo0) D:\demo> ``` 这里也很容易发现,通过pytest.ini配置文件配置注册标签的方式更加的便捷简单,因此在实际自动化脚本开发中,也推荐通过pytest.ini配置文件的方式配置注册使用标签。 # 四、通过标签灵活挑选测试脚本执行 Mark标签在自动化测试中是非常有用的,这里就详细的讲解在实际应用开发中如何综合使用mark标签。首先这里通过pytest.ini注册三个标签,即smoke、function和performance,分别表示冒烟测试脚本、功能测试脚本和性能测试脚本,pytest.ini 配置文件中注册标签的配置如下 ```bash [pytest] markers = smoke: smoke tests function: function tests performance: performane tests ``` 然后编写测试函数脚本如下,这里编写了三个测试函数,即test_01、test_02和test_03,这里注意一下,针对每个测试函数,是可以打多个标签的,这也是合理的,比如在实际开发中,就是存在某个用例既可以作为功能测试用例,又可以作为冒烟测试用例,实际上冒烟测试用例一般就是从功能测试用例中挑出一些最基本的用例。下面的代码中为了更好的演示标签的使用,因此对每个测试函数加了两个标签。 ```python import pytest @pytest.mark.function @pytest.mark.smoke def test_01(): assert 1==1 @pytest.mark.function @pytest.mark.performance def test_02(): assert 1==1 @pytest.mark.performance @pytest.mark.smoke def test_03(): assert 1==1 ``` 如下图所示,这里通过指定smoke标签,可以发现此时执行了两个测试用例,因为test_01和test_03均加了smoke标签,因此当指定smoke标签的时候,会将所有打了smoke标签的脚本执行。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m smoke =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo, configfile: pytest.ini plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 3 items / 1 deselected / 2 selected test_demo.py .. [100%] ============= 2 passed, 1 deselected in 0.08s ============= (demo--ip5ZZo0) D:\demo> ``` 初次以外,pytest还可以通过逻辑关系词and,or和not类指定满足一定逻辑关系的标签组合来挑选测试脚本执行。and 表示同时满足,or表示逻辑或,相当于并集,not相当于非,此外当使用逻辑关系词时,需要将指定的标签逻辑关系部分使用引号括起来,比如想执行同时打了smoke和function的标签,使用的命令就是 pytest -m “smoke and function”,执行结果如下图,即此时只执行了一个,因为同时打了smoke和function标签的脚本只有test_01。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m "smoke and function" =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo, configfile: pytest.ini plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 3 items / 2 deselected / 1 selected test_demo.py . [100%] ============= 1 passed, 2 deselected in 0.03s ============= (demo--ip5ZZo0) D:\demo> ``` 当想执行打了smoke或者function的标签的脚本时,即使用逻辑关系词or,使用的命令为pytest -m “smoke or function”,执行结果如下图所示,因为此时三个测试函数要么打了smoke标签,要么打了function标签,因此这里显示执行了三个测试函数。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m "smoke or function" =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo, configfile: pytest.ini plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 3 items test_demo.py ... [100%] ==================== 3 passed in 0.03s ==================== (demo--ip5ZZo0) D:\demo> ``` 同理,加入想执行了除了性能测试以外的所有用例,即执行除了打performance标签以外的所有用例,此时需要使用not逻辑词,执行的命令为 pytest -m “not performance”,执行结果如下图所示,因为test_02和test_03均打了performance标签,因此这里只执行了一个测试脚本。 ```bash (demo--ip5ZZo0) D:\demo>pytest -m "not performance" =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=
rootdir: D:\demo, configfile: pytest.ini plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 3 items / 2 deselected / 1 selected test_demo.py . [100%] ============= 1 passed, 2 deselected in 0.03s ============= (demo--ip5ZZo0) D:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/371
上一篇:
Pytest----如何防止导入包失败即importorskip的用法
下一篇:
基于Kubernetes平台部署Grafana Loki Promtail系统
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件