【原文链接】Pytest注册使用自定义mark标签

文章目录

  • 一、直接使用自定义mark标签
  • 二、通过conftest.py文件中重写pytest_configure函数注册标签
  • 三、通过pytest.ini文件配置注册标签
  • 四、通过标签灵活挑选测试脚本执行

一、直接使用自定义mark标签

在pytest中,可以通过@pytest.mark.xxx的方式直接自定义标签使用,比如欲对一个测试函数增加smoke标签,即直接在测试函数上使用@pytest.mark.smoke即可,如下代码:

import pytest@pytest.mark.smoke
def test_01():assert 1==1

对测试函数增加自定义的标签的作用是对自动化脚本进行分类标记,比如在测试活动常常分为冒烟测试、功能测试、系统测试、性能测试等,那么测试用例也同样的分为冒烟测试用例、功能测试用例、系统测试用例、性能测试用例,因此自动化脚本也有必要同步保持自动化脚本的分类,pytest中的mark主要就是为了实现这样一个功能的,在正式使用mark对自动化脚本标记之前,先看下上面这个自动化脚本执行结果。执行结果如下图所示,这里执行成功了,但是可以看到一条告警信息,从告警信息可以看出,此时smoke标签为未识别标签,即这里没有注册,而是直接使用的原因。

(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=<bucket_type>
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 itemtest_demo.py .                                       [100%]==================== warnings summary =====================
test_demo.py:2D:\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命令是否可以做到挑选用例执行。

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标签进行注册,当仍然可以通过指定标签的方式挑选测试脚本执行。即不注册而直接使用标签的一个不好的地方就是会显示告警。后面将继续展示通过注册标签然后再使用的情形。

(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=<bucket_type>
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 selectedtest_demo.py .                                       [100%]==================== warnings summary =====================
test_demo.py:3D:\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.smoketest_demo.py:8D:\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,唯一需要变更的就是注册的标签的名字和描述。

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进行标记。

import pytest@pytest.mark.smoke
def test_01():assert 1==1@pytest.mark.function
def test_02():assert 1==1

此时脚本的执行结果如下图所示,即告警消除了,同时也做到了挑选smoke标签的脚本执行了。因此在企业里倘若在执行脚本的时候发现有大量的提示未知标签的告警,那么就是未对告警进行注册的原因。

(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=<bucket_type>
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 selectedtest_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中编写如下配置即可。

[pytest]
markers =smoke: smoke testsfunction: function tests

此时继续使用如下测试函数来验证,即分别为test_01和test_02使用smoke和function打标签。

import pytest@pytest.mark.smoke
def test_01():assert 1==1@pytest.mark.function
def test_02():assert 1==1

通过pytest -m smoke挑选打了smoke标签的测试函数执行,执行结果如下图所示,可以发现此时同样不会打印告警,即注册标签生效了。

(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=<bucket_type>
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 selectedtest_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 配置文件中注册标签的配置如下

[pytest]
markers =smoke: smoke testsfunction: function testsperformance: performane tests

然后编写测试函数脚本如下,这里编写了三个测试函数,即test_01、test_02和test_03,这里注意一下,针对每个测试函数,是可以打多个标签的,这也是合理的,比如在实际开发中,就是存在某个用例既可以作为功能测试用例,又可以作为冒烟测试用例,实际上冒烟测试用例一般就是从功能测试用例中挑出一些最基本的用例。下面的代码中为了更好的演示标签的使用,因此对每个测试函数加了两个标签。

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标签的脚本执行。

(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=<bucket_type>
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 selectedtest_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。

(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=<bucket_type>
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 selectedtest_demo.py .                                       [100%]============= 1 passed, 2 deselected in 0.03s =============(demo--ip5ZZo0) D:\demo>

当想执行打了smoke或者function的标签的脚本时,即使用逻辑关系词or,使用的命令为pytest -m “smoke or function”,执行结果如下图所示,因为此时三个测试函数要么打了smoke标签,要么打了function标签,因此这里显示执行了三个测试函数。

(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=<bucket_type>
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 itemstest_demo.py ...                                     [100%]==================== 3 passed in 0.03s ====================(demo--ip5ZZo0) D:\demo>

同理,加入想执行了除了性能测试以外的所有用例,即执行除了打performance标签以外的所有用例,此时需要使用not逻辑词,执行的命令为 pytest -m “not performance”,执行结果如下图所示,因为test_02和test_03均打了performance标签,因此这里只执行了一个测试脚本。

(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=<bucket_type>
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 selectedtest_demo.py .                                       [100%]============= 1 passed, 2 deselected in 0.03s =============(demo--ip5ZZo0) D:\demo>
``

Pytest注册使用自定义mark标签相关推荐

  1. Pytest之自定义mark

    VOL 153 27 2020-08 今天距2021年136天 这是ITester软件测试小栈第153次推文 点击上方蓝字"ITester软件测试小栈"关注我,每周一.三.五早上  ...

  2. 【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数

    在之前的分享中,我们知道可以使用yield或者return关键字把fixture函数里的值传递给test函数. 这种方法很实用,比如我在fixture函数里向数据库里插入必要的测试数据,那我就可以把相 ...

  3. 【pytest】使用 mark 标记及运行测试用例,注册、管理 mark 标记

    使用 @pytest.mark.xxx 标记测试用例: 可以标记测试方法.测试类,标记名可以自定义,最好起有意义的名字: 同一测试类/方法可同时拥有多个标记: # test_login_logout. ...

  4. Django 自定义模板标签TemplateTags

    创建自定义的模板标签(template tags) Django提供了以下帮助函数(functions)来允许你以一种简单的方式创建自己的模板标签(template tags): simple_tag ...

  5. java 自定义xml_6.1 如何在spring中自定义xml标签

    dubbo自定义了很多xml标签,例如,那么这些自定义标签是怎么与spring结合起来的呢?我们先看一个简单的例子. 一 编写模型类 1 packagecom.hulk.testdubbo.model ...

  6. django “如何”系列4:如何编写自定义模板标签和过滤器

    django的模板系统自带了一系列的内建标签和过滤器,一般情况下可以满足你的要求,如果觉得需更精准的模板标签或者过滤器,你可以自己编写模板标签和过滤器,然后使用{% load %}标签使用他们. 代码 ...

  7. 20190325 Django自定义过滤器和自定义模板标签

    代码布局(死去活来) 自定义要先解决几个问题: 1). 在哪里定义,要将代码写在哪里? app目录下的名为templatetags的文件夹,必须叫这个名字.这个文件夹必须是一个python的包(pyt ...

  8. Django 自定义模板标签(template_tags)究竟有什么用?

    自定义模板标签,过滤器.英文翻译是Custom template tags and filters.custom filter自定义过滤器今天不在我的记录范围之内,以后用到再看官方文档也不迟. **问 ...

  9. vue-quill-editor 自定义 img 标签,给标签添加 href 属性

    vue-quill-editor 自定义 img 标签,给标签添加 href 属性 需求:上传图片成功之后,给图片标签插入 href 属性,内容为自定义的一段字符串,格式大概如下:<img sr ...

最新文章

  1. MalformedObjectNameException: Invalid character '' in value part of property
  2. OAuth的MVC实现(微软)
  3. ros系统支持java_创建一个rosjava package 并测试发布和接受消息
  4. 两个小知识:C#如何设置开机启动时自动执行程序|C# WinForm打开超链接
  5. 5G赋能中国智慧教育
  6. mysql5.5怎么升级补丁_mysql 5.5升级到5.7版本操作流程
  7. 浅谈SQL Server 数据库的触发器
  8. 【渝粤教育】国家开放大学2018年秋季 1174t水工钢筋混凝土结构(本) 参考试题
  9. delphi2007安装说明(备忘)
  10. windows批处理脚本导入注册表不弹出确认框
  11. 华为ac配置radius认证服务器_华为AAA结合Radius服务器认证学习
  12. 基于FPGA ZC706的AD9371ADRV9009网口驱动配置
  13. iOS 录音,播放并上传
  14. 大数据技术之Spark入门(一)概述
  15. 同文输入法 android,同文输入法app下载-同文输入法手机版-同文输入法最新版_易玩网...
  16. 使用百度翻译API实现一键翻译
  17. Apipost 上手指南
  18. 人工智能新目标——看懂视频
  19. onCreate(savedInstanceState)
  20. python 安装gdal成功,但是导入报错:DLL load failed 找不到指定的模块

热门文章

  1. 领域最全!多传感器融合方法综述!(Camera/Lidar/Radar等多源异构数据)
  2. oracle 查找序列号,Oracle 查找丢失数据序列号
  3. 桌面虚拟化云技术将支撑数字化医院
  4. UNIX环境高级编程——进程关系
  5. 【unity】维护一个changelog
  6. linux 基本操作
  7. 浏览器无法访问localhost
  8. matlab中的符号对象与符号运算
  9. element ui框架(准备)
  10. 什么叫MD5,MD5通常做什么用处,为什么MD5不可逆,用做密码加密的时候仍然可能会被解密?