浅谈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

最新文章

  1. win7 装了VB虚拟机 开始挺好用 后来突然就打不开了 提示如下错误:(如图)创建 COM 对象失败.
  2. IP地址框
  3. Unsafe的应用
  4. C++ Scripting
  5. js资源
  6. !important------至高无上的宝剑
  7. oh my zsh命令
  8. Facebook 网页应用图文设置教程
  9. Python Cookbook - 1 - 数据结构和算法
  10. A Simple Game
  11. 多线程--wait()和notify(),Thread中的等待和唤醒方法
  12. MySQL 如何查看表的存储引擎
  13. PHP浮点数的精确计算BCMath
  14. JavaScript各种继承方式(四):原型式继承(prototypal inheritance)
  15. andorid 表格布局
  16. Python脚本性能分析
  17. linq操作符:限定操作符
  18. vim复制粘贴常用命令
  19. HALCON中的算子大全(中英对照)
  20. java并发编程(1)并发程序的取消于关闭

热门文章

  1. 达梦-DBLINK数据库链接
  2. cookie中 防止重复存值 (可用于历史记录等)
  3. Codeforces Round #833 (Div. 2)补题
  4. 华为云平台部署教程之CNA\VRM的安装
  5. day16 异常处理生成器
  6. React Server Component: 混合式渲染
  7. python-简单模块的使用
  8. Android 内存缓存框架 LruCache 的实现原理,手写试试?
  9. 【Java EE】Day07 HTML
  10. 【每日一题】【回溯backtrace】N皇后