浅谈Pytest中的marker

浅谈,pytest,marker · 浏览次数 : 101

小编点评

## Pytest Marker without Registration This document explains the problem with `pytest.mark.login` not registering a marker and provides two possible solutions. ### Problem The `pytest.mark.login` marker does not work as expected when used without registering it first. This can cause a `pytestUnknownMarkWarning` warning. **Solution 1: Use `pytest_configure`** The `pytest_configure` function allows you to define custom configurations for pytest. You can use this function to set the `markers` option, which will be used to find and run tests that match the specified keyword. **Example:** ```python pytest_configure(config) config.addinivalue_line("markers", "login: login test demo") ``` **Solution 2: Use `mark` with specific keyword** Another option is to use the `mark` keyword with a specific keyword, such as `login` in this case. This will only run tests that match that keyword, regardless of its case. **Example:** ```python @pytest.mark.login def test_demo(): assert True @pytest.mark.logout def test_demo2(): assert True ``` **Note:** * `-m` is not a recommended way to specify markers in `pytest.ini`. It is more suitable for specific use cases. * `pytest_configure` is preferred over `pytest.mark` for defining marker configurations. By understanding these methods, you can effectively use markers to control which tests are run in your pytest tests.

正文

浅谈Pytest中的marker

没有注册marker

  • 我们写一个简单的测试
# test_demo.py
import pytest

@pytest.mark.login
def test_demo():
    assert True
  • 你运行的话会有如下提示
test_demo.py:4: PytestUnknownMarkWarning: Unknown pytest.mark.login - 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.login

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
  • 上面两个文档值得你的仔细阅读

注册marker方式一:pytest.ini

  • 新建一个文件pytest.ini(在哪里创建?你应该要知道,跟你的项目结构有关),写入如下内容
[pytest]
markers:
    login: login test demo
  • 再次运行就不会有PytestUnknownMarkWarning

注册marker方式二:pytest_configure

  • 创建一个conftest.py

    def pytest_configure(config):
        config.addinivalue_line(
            "markers", "login: login test demo"
        )
    
  • 效果是一样的

带marker运行

  • 通过命令行参数-m即可

  • 比如现在有这个case

    import pytest
    
    @pytest.mark.login
    def test_demo1():
        assert True
    
    @pytest.mark.logout
    def test_demo2():
        assert True
    
  • contest.py

    def pytest_configure(config):
        config.addinivalue_line("markers", "login: login test demo",)
        config.addinivalue_line("markers", "logout: logout test demo")# 注意一个marker要写一个addinivalue_line
    
  • 运行的时候带上-m login即可选择登录用例进行测试

  • 而-m的语法还比较复杂,可以参考-k的

    general:
      -k EXPRESSION  
      only run tests which match the given substring expression. An expression is a python evaluatable
      expression where all names are substring-matched against test names and their parent classes.
      Example: -k 'test_method or test_other' matches all test functions and classes whose name
      contains 'test_method' or 'test_other', while -k 'not test_method' matches those that don't
      contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the
      matches. Additionally keywords are matched to classes and functions containing extra names in
      their 'extra_keyword_matches' set, as well as functions which have names assigned directly to
      them. The matching is case-insensitive.
    
    
    -m MARKEXPR   only run tests matching given mark expression.For example: -m 'mark1 and not mark2'.
    

关于marker的其他

  • 查询markers

    pytest --markers
    
    # 能查到当前注册的markers
    

  • 命令行参数--strict-markers

    pytest.main(['-sv','--strict-markers',__file__])   # 强制markers必须要注册
    
  • 或者放pytest.ini中

    [pytest]
    addopts = --strict-markers   
    
  • 会产生如下信息

    =================================== ERRORS ====================================
    ________________________ ERROR collecting test_demo.py ________________________
    'login' not found in `markers` configuration option
    =========================== short test summary info ===========================
    ERROR test_demo.py
    !!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
    ============================== 1 error in 0.08s ===============================
    

  • marker是pytest中pick用例的多种方式之一(-m),其他pick用例的方式比如-k,--allure的几个

      --allure-severities=SEVERITIES_SET
      --allure-epics=EPICS_SET
      --allure-features=FEATURES_SET
      --allure-stories=STORIES_SET
      --allure-ids=IDS_SET  Comma-separated list of IDs.
      --allure-link-pattern=LINK_TYPE:LINK_PATTERN
    

pytest中处理warning的方式考虑单独开个章节讲下 TODO

与浅谈Pytest中的marker相似的内容:

浅谈Pytest中的marker

浅谈Pytest中的marker 没有注册marker 我们写一个简单的测试 # test_demo.py import pytest @pytest.mark.login def test_demo(): assert True 你运行的话会有如下提示 test_demo.py:4: Pytest

浅谈Pytest中的warning处理

浅谈Pytest中的warning处理 没有处理warning 我们写一个简单的测试 import pytest def test_demo(): import warnings warnings.warn('test warn',DeprecationWarning) assert True if

浅谈k8s中cni0和docker0的关系和区别

最近在复习k8s网络方面的知识,查看之前学习时整理的笔记和文档还有过往自己总结的博客之后发现一个问题,就是在有关flannel和calico这两个k8s网络插件的文章和博客中,会涉及到cni0和docker0这两个网桥设备,但是都没有明确说明他们俩之间的关系,有的甚至将两者混为一谈,这也是我之前的学

浅谈性能测试稳定性 Constant Throughput Timer(常数吞吐量定时器)

在性能测试过程中总会收到一些需求如:单接口每秒并发20,这种并发持续60秒,通过负载测试查看系统稳定性,今天就让我们来浅谈一下这种场景如何去实现性能测试~

浅谈如何向上管理

最近听说了很多事,加之目前自己也处在被汇报以及需要向上汇报的状态中间,迫使我开始思考向上管理(managing up)这个话题。这是一个有争议的话题,很多人(包括曾经的自己)下意识的会将向上管理与徒有其表的讨好或者迎合这类负面词划上等号。借此契机在查阅了很多资料之后,才意识到它不过是一项职场软技能而已。

[转帖]浅谈系统稳定性与高可用保障的几种思路

https://segmentfault.com/u/dewujishu 一、前言 高并发、高可用、高性能被称为互联网三高架构,这三者都是工程师和架构师在系统架构设计中必须考虑的因素之一。今天我们就来聊一聊三H中的高可用,也是我们常说的系统稳定性。 本篇文章只聊思路,没有太多的深入细节。阅读全文大概

[转帖]浅谈RAID写惩罚(Write Penalty)与IOPS计算

介绍 通常在讨论不同RAID保护类型的性能的时候,结论都会是RAID-1提供比较好的读写性能,RAID-5读性能不错,但是写入性能就不如RAID-1,RAID-6保护级别更高,但写性能相对更加差,RAID10是提供最好的性能和数据保护,不过成本最高等等。其实决定这些性能考虑的因素很简单,它就是RAI

[转帖]浅谈RAID写惩罚(Write Penalty)与IOPS计算_文字版

https://www.cnblogs.com/IvanChen/p/4491984.html 介绍 通常在讨论不同RAID保护类型的性能的时候,结论都会是RAID-1提供比较好的读写性能,RAID-5读性能不错,但是写入性能就不如RAID-1,RAID-6保护级别更高,但写性能相对更加差,RAID

[转帖]张磊:浅谈容器网络

https://zhuanlan.zhihu.com/p/595014129 你好,我是张磊。今天我和你分享的主题是:浅谈容器网络。 在前面讲解容器基础时,我曾经提到过一个Linux容器能看见的“网络栈”,实际上是被隔离在它自己的Network Namespace当中的。 而所谓“网络栈”,就包括了

[转帖]浅谈Armv8-A处理器

https://www.elecfans.com/emb/dsp/202208291886182.html 众所周知,ARM是一家设计并授权处理器和相应IP(比如互连总线,中断处理器,图像处理器等等)的公司,目前其处理器产品分为三类: Cortex-A系列:这个系列主要是应用(Application