首页 > 其他分享 >浅谈Pytest中的marker

浅谈Pytest中的marker

时间:2023-02-06 13:47:34浏览次数:41  
标签:浅谈 -- pytest markers test Pytest marker login

浅谈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的其他

  • 查询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,markers,test,Pytest,marker,login
From: https://www.cnblogs.com/wuxianfeng023/p/17095146.html

相关文章

  • 浅谈工业4.0背景下的空中数据端口,无人机3D 可视化系统的应用
    前言近年来,无人机的发展越发迅速,既可民用于航拍,又可军用于侦察,涉及行业广泛,把无人机想象成一个“会飞的传感器”,无人机就成了工业4.0的一个空中数据端口,大至地球物理、气象......
  • 浅谈Python中的包
    浅谈Python中的包Package的定义(你以为的)你在很多的地方都能看到关于package的定义:在Python中在当前目录下有__init__.py文件的目录即为一个package。嗯,包括pytho......
  • java 和mysql 开发中的数据处理浅谈
    我们在开发过程中经常会用到java和mysql,两者都能处理数据。那么什么时候用这两个,或者在特定的情况下使用正确的工具,发挥工具的本身的特点提高开发的效率就显得尤为重要。j......
  • 浅谈浏览器端 WebGIS 开发可能会用到的、提升效率的 js 库
    目录前置说明1.与数据格式转换解析相关1.1.解析和转换WKT几何数据1.2.前端直接读取GeoPackage-@ngageoint/geopackage1.3.前端直接读取EsriShapefile-ts-shap......
  • python-pytest.ini介绍
    一、pytest.ini执行方式含义[pytest]addopts=-vsq--html=./report.htmltestpaths=./testConftestpython_files=test*.pypython_classes=Test*python_func......
  • 浅谈最短路算法在信息学竞赛中的应用
    最短路温馨提示:如果下文档中没有做特殊提示,默认所有下标从\(1\)开始,并且默认\(n,m\)同阶因为在生活当中,路径的长度大多为正数,没有特殊说明,不考虑长度为负数的情况\(s\)......
  • 浅谈python容器、迭代器与生成器
    前言:for...in...循环时,与while不同的是,for可以自动访问容器中的下一个元素,这是为什么呢?#用while循环访问列表容器iter_a=iter(a)whileTrue:try:print(ne......
  • python3 测试框架pytest入门
    1、安装pytest2、安装html插件3、编写测试用例4、执行测试5、生成测试报告1.1安装pytestpip3installpytest执行该命令安装pytest1.2验证安装pytest-V查看当前已安装......
  • 浅谈线段树优化建图
    前置知识前言用途方法思想实现例题LuoguP2627[USACO11OPEN]MowingtheLawnG前置知识线段树建图(?)前言接触到线段树优化建图还是因为做"[USACO......
  • pytest.main()参数详解
      importpytestpytest.main(["-n3"])#3个进程并发执行,需要装pytest-xdist库pytest.main(["--collect-only"])#仅仅展......