iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Pytest如何使用mark的方法
  • 830
分享到

Pytest如何使用mark的方法

2024-04-02 19:04:59 830人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、常见的内置markers二、查看所有markers三、注册自定义marks四、对未注册mark的限制一、常见的内置markers usefixtures - 为测试函数或者测

一、常见的内置markers

  • usefixtures - 为测试函数或者测试类知名使用那些fixture
  • filterwarnings - 为一个测试函数过滤一个指定的告警
  • skip - 跳过一个测试函数
  • skipif - 如果满足条件就跳过测试函数
  • xfail - 标记用例失败
  • parametrize - 参数化

二、查看所有markers

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

$ pytest --markers
@pytest.mark.forked: Always fork for this test.

@pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs.

@pytest.mark.hypothesis: Tests which use hypothesis.

@pytest.mark.allure_label: allure label marker

@pytest.mark.allure_link: allure link marker

@pytest.mark.allure_description: allure description

@pytest.mark.allure_description_html: allure description html

@pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/stable/warnings.html#pytest-mark-filterwarnings

@pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.

@pytest.mark.skipif(condition, ..., *, reason=...): skip the given test function if any of the conditions evaluate to True. Example: skipif(sys.platfORM == 'win32') skip
s the test if we are on the win32 platform. See Https://docs.pytest.org/en/stable/reference.html#pytest-mark-skipif

@pytest.mark.xfail(condition, ..., *, reason=..., run=True, raises=None, strict=xfail_strict): mark the test function as an expected failure if any of the conditions eva
luate to True. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expe
cted, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See 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]
markers =
    smoke: smoke tests
    test: system tests

此时test_demo.py代码如下

import pytest

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

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

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

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=================================================================== 1 passed, 2 deselected in 0.04s ====================================================================

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

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "smoke: smoke test"
    )
    config.addinivalue_line(
        "markers", "test: system test"
    )

test_demo.py代码如下:

import pytest

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

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

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

$ pytest -m test
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=================================================================== 1 passed, 2 deselected in 0.02s ====================================================================

四、对未注册mark的限制

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

test_demo.py代码如下

import pytest

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

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

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

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected                                                                                                                           

test_demo.py .                                                                                                                                                    [100%]

=========================================================================== warnings summary ===========================================================================
test_demo.py:3
  D:\src\blog\tests\test_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can reGISter custom marks to avoid this warning - for deta
ils, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo.py:7
  D:\src\blog\tests\test_demo.py:7: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo?  You can register custom marks to avoid this warning - for detai
ls, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.test

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 1 passed, 2 deselected, 2 warnings in 0.02s ==============================================================

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

[pytest]
addopts = --strict-markers

比如test_demo.py代码:

import pytest

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

@pytest.mark.test
def test_func2():
    assert 1==1

def test_func3():
    assert 1==1

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

$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 0 items / 1 error                                                                                                                                             

================================================================================ ERRORS ================================================================================
____________________________________________________________________ ERROR collecting test_demo.py _____________________________________________________________________
'smoke' not found in `markers` configuration option
======================================================================= short test summary info ========================================================================
ERROR test_demo.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 0.14s ===========================================================================

到此这篇关于Pytest如何使用mark的方法的文章就介绍到这了,更多相关Pytest使用mark内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Pytest如何使用mark的方法

本文链接: http://www.lsjlt.com/news/148820.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Pytest如何使用mark的方法
    目录一、常见的内置markers二、查看所有markers三、注册自定义marks四、对未注册mark的限制一、常见的内置markers usefixtures - 为测试函数或者测...
    99+
    2024-04-02
  • pytest如何实现mark标记功能
    这篇文章将为大家详细讲解有关pytest如何实现mark标记功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。mark标记在实际工作中,我们要写的自动化用例会比较多,也不会都放在一个py文件中,如果有几十...
    99+
    2023-06-14
  • pytest中的fixture如何使用
    本篇内容介绍了“pytest中的fixture如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!简介:  fixture区别于unnit...
    99+
    2023-07-05
  • 如何正确的使用pytest
    本篇文章为大家展示了如何正确的使用pytest,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1、安装pytest,打开dos窗口输入:pip install pytest2、通过pycharm工具下...
    99+
    2023-06-07
  • Pytest中skip skipif跳过的使用方法
    这篇文章主要讲解了“Pytest中skip skipif跳过的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Pytest中skip skipif跳过的使用方法”吧!前言pytest....
    99+
    2023-06-20
  • Pytest中skip和skipif的使用方法是什么
    本篇内容主要讲解“Pytest中skip和skipif的使用方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Pytest中skip和skipif的使用方法是什么”吧!skip的用法使用示...
    99+
    2023-06-20
  • 如何在pytest中使用conftest.py文件
    这篇文章将为大家详细讲解有关如何在pytest中使用conftest.py文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、conftest.py的特点可以跨.py文件调用,有多个.py...
    99+
    2023-06-08
  • Python测试框架pytest如何使用
    本文小编为大家详细介绍“Python测试框架pytest如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python测试框架pytest如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。前言pytes...
    99+
    2023-06-30
  • Pytest中conftest.py的用法
    目录前言什么是conftest.pyconftest.py特点conftest.py用法conftest.py实际案例test_baidu目录下前言 在之前介绍fixture的文章中...
    99+
    2024-04-02
  • pytest官方文档解读之安装和使用插件的方法
    目录一、pip 安装二、查找可用插件三、在测试模块或者conftest文件中加载指定插件四、查看被激活的插件五、注销插件本节讨论安装和使用第三方插件。关于编写自己的插件,我们下一章继...
    99+
    2024-04-02
  • pytest中配置文件pytest.ini如何使用
    本篇内容介绍了“pytest中配置文件pytest.ini如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、pytest.ini说明...
    99+
    2023-06-30
  • pytest如何使用@pytest.mark.parametrize()实现参数化
    这篇文章主要介绍“pytest如何使用@pytest.mark.parametrize()实现参数化”,在日常操作中,相信很多人在pytest如何使用@pytest.mark.parametrize()实现...
    99+
    2024-04-02
  • Pytest allure命令行参数如何使用
    这篇文章将为大家详细讲解有关Pytest allure命令行参数如何使用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。先看看 allure 命令的帮助文档cmd 敲allure -hallure...
    99+
    2023-06-14
  • python单元测试之如何使用pytest
    这篇文章主要介绍python单元测试之如何使用pytest,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、前提准备前提:需要安装pytest和pytest-html(生成html测试报告)pip install p...
    99+
    2023-06-15
  • 详解Pytest测试用例的执行方法
    pytest概述 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1、简单灵活,容易上手,文档丰富; 2、支持参数化,可以细粒度地控制要测试的测试用...
    99+
    2024-04-02
  • 如何在pytest中使用pytest.ini配置文件
    这篇文章将为大家详细讲解有关如何在pytest中使用pytest.ini配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。pytest配置文件可以改变pytest的运行方式,它是一个固定...
    99+
    2023-06-14
  • Pytest自动化测试框架如何使用
    这篇文章主要讲解了“Pytest自动化测试框架如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Pytest自动化测试框架如何使用”吧!Pytest和Unittest测试框架的区别?如何...
    99+
    2023-07-05
  • pytest中的fixture基本用法
    目录简介:fixture的功能特点及优势基本用法fixture在自动化中的应用--作用域fixture在自动化中的应用-yield关键字fixture在自动化中的应用--数据共享fi...
    99+
    2023-02-24
    pytest fixture用法 pytest fixture
  • Pytest断言的具体使用
    目录assert断言方法异常断言Excepiton检查断言装饰器Pytest使用的断言是使用python内置的断言assert。Python assert(断言)用于判断一个表达式,...
    99+
    2023-02-07
    Pytest断言
  • 如何使用callbacks.has()的方法
    这篇文章给大家分享的是有关如何使用callbacks.has()的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。   callbacks.has( callback ) ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作