pytest marker,pytest.mark.skip

  pytest marker,pytest.mark.skip

  本文主要介绍了Pytest如何使用标记的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  

目录

一、常见的内置标记二、查看所有标记三、注册自定义记号四、对未注册标记的限制

 

  

一、常见的内置markers

 

  使用固定装置-为测试函数或者测试类知名使用那些fixturefilterwarnings -为一个测试函数过滤一个指定的告警跳过-跳过一个测试函数斯基皮夫-如果满足条件就跳过测试函数xfail -标记用例失败参数化-参数化

  

二、查看所有markers

 

  如下,可以查看到当前环境中的所有标记

  $ pytest -标记

  @pytest.mark.forked:这个测试永远分叉。

  @ py测试。马克。flally(重新运行=1,重新运行_延迟=0):标记测试重新运行最多"重新运行"次。在重新运行之间添加"重新运行_延迟"秒的延迟。

  @pytest.mark.hypothesis:使用假设的测试。

  @ pytest。马克。诱惑_标签:诱惑标签马克笔

  @ pytest。马克。倾城_ link :倾城链接标记

  @ pytest。马克。倾城_描述:诱惑描述

  @ pytest。马克。诱惑_描述_ html :诱惑描述超文本标记语言

  @ py测试。马克。过滤警告(warning):给定的测试添加一个警告过滤器。见https://份文件。py测试。org/en/stable/warnings。html # py测试标记-过滤警告

  @ pytest。马克。跳过(原因=无):跳过给定的测试函数,原因可选Example:跳过(reason=目前无法测试这个)跳过测试。

  @pytest.mark.skipif(条件,*,原因=.):如果任一条件评估为真,则跳过给定的测试函数。示例:跳过if(sys。平台== win32 )跳过

  测试我们是否在win32平台上。见https://份文件。py测试。org/en/stable/referencehtml # py测试标记-跳过if

  @pytest.mark.xfail(条件,*,原因=.run=True,raises=None,strict=xfail_strict):如果任何条件伊娃

  评估为真。如果您甚至不想执行测试函数,可以选择指定一个更好报告的原因并运行=假.如果只排除特定的例外

  反恐执行局,你可以把它们列在举起里面,如果测试在其他方面失败了,就会被报告为真失败。参见https://docs.pytest.org/en/stable/reference.html#pytes

  t-

  mark-xfail

  @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of valu

  es if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize(arg1, [1,2]) would lead to two calls o

  f the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/stable/parametrize.html for more info and examples.

  @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/stable/fixture.html#usefix

  tures

  @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.

  @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.

  

 

  

三、注册自定义marks

 

  方式一:在pytest.ini中按照如下格式声明即可,冒号之前为注册的mark名称,冒号之后为此mark的说明

  

[pytest]

 

  此时test_demo.py代码如下

  

import pytest

 

  使用pytest -m smoke执行结果如下,发现此时即只执行了标记为smoke的一个用例,这就是和自定义mark的使用方法

  

$ pytest -m smoke

 

  方式二:在conftest.py文件中重写pytest_configure函数即可,比如如下,注册两个mark:smoke和test

  

def pytest_configure(config):

 

  test_demo.py代码如下:

  

import pytest

 

  通过pytest -m test 执行结果如下:

  

$ pytest -m test

 

  

 

  

四、对未注册mark的限制

 

  默认情况下,对未注册mark直接使用是会产生一条告警信息,比如这里把pytest.ini和conftest.py都删除掉,只剩test_demo.py一个文件

  test_demo.py代码如下

  

import pytest

 

  直接使用 pytest -m smoke 执行结果如下,可以发现这里产生了两条告警,这就是因为这两条告警未在pytest.ini或者conftest.py中进行注册的原因,在实际项目开发中如果在执行测试的时候发现了这种大片告警打印,解决办法就是在pytest.ini或者conftest.py将这些告警报出的mark都进行注册即可

  

$ pytest -m smoke

 

  如果希望强制限制必须先注册再使用mark,则可以在pytest.ini中加上如下配置即可

  

[pytest]

 

  比如test_demo.py代码:

  

import pytest

 

  此时继续使用pytest -m smoke执行结果如下,发现此时已经报错了,即强制限制必须对mark进行注册

  

$ pytest -m smoke

 

  到此这篇关于Pytest如何使用mark的方法的文章就介绍到这了,更多相关Pytest使用mark内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: