浅谈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的更多相关文章

  1. 浅谈Java中的equals和==(转)

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...

  2. 浅谈Linux中的信号处理机制(二)

    首先谢谢 @小尧弟 这位朋友对我昨天夜里写的一篇<浅谈Linux中的信号处理机制(一)>的指正,之前的题目我用的“浅析”一词,给人一种要剖析内核的感觉.本人自知功力不够,尚且不能对着Lin ...

  3. 浅谈Java中的对象和引用

    浅谈Java中的对象和对象引用 在Java中,有一组名词经常一起出现,它们就是“对象和对象引用”,很多朋友在初学Java的时候可能经常会混淆这2个概念,觉得它们是一回事,事实上则不然.今天我们就来一起 ...

  4. 浅谈Java中的equals和==

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: String str1 = new String("hello"); String str2 = ...

  5. 转【】浅谈sql中的in与not in,exists与not exists的区别_

    浅谈sql中的in与not in,exists与not exists的区别   1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...

  6. 浅谈iOS中的userAgent

    浅谈iOS中的userAgent   User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产 ...

  7. 浅谈JavaScript中的闭包

    浅谈JavaScript中的闭包 在JavaScript中,闭包是指这样一个函数:它有权访问另一个函数作用域中的变量. 创建一个闭包的常用的方式:在一个函数内部创建另一个函数. 比如: functio ...

  8. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  9. 浅谈Java中的深拷贝和浅拷贝(转载)

    浅谈Java中的深拷贝和浅拷贝(转载) 原文链接: http://blog.csdn.net/tounaobun/article/details/8491392 假如说你想复制一个简单变量.很简单: ...

  10. 浅谈Java中的深拷贝和浅拷贝

    转载: 浅谈Java中的深拷贝和浅拷贝 假如说你想复制一个简单变量.很简单: int apples = 5; int pears = apples; 不仅仅是int类型,其它七种原始数据类型(bool ...

随机推荐

  1. Pytest学习

    pytest简介 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高. 使用介绍 编写规则 测试函数以t ...

  2. 😀 Java并发 - (并发基础)

    Java并发 - (并发基础) 1.什么是共享资源 堆是被所有线程共享的一块内存区域.在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例 Java中几乎所有的对象实例都在这里分配内存.方法区与堆 ...

  3. beego-orm-sqlite

    dao.go ``` package daoimport ( "fmt" "github.com/astaxie/beego/orm" "html&q ...

  4. win 10玩魔兽争霸/黑边,不能全屏,闪退

    1.win键+s键搜索注册表 打开 找到路径 计算机\HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III\Video 这里有两 ...

  5. error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or di

    前言 运行 ida软件报错, 但是我的系统中存在 libSM.so.6 解决办法 首先查看系统中的 libsm.so.6 ldconfig -p |grep -i libsm.so.6 输出: lib ...

  6. combobox 只能选择第一个

    在使用combobox时有时对于特定的下拉框内容我们一般不需要去请求url获得值,我们只用在js里面控制就好了,昨天遇到的问题是在js里面按照api给的方法写进去,显示是正常的 但是当我去选择的时候发 ...

  7. async.js 版本兼容问题 async.filter举例

    async3.x 和async2.6.1 版本下 const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; // Usin ...

  8. python基础之hashilb模块、logging模块

    hashlib加密模块 1.何为加密 将文明数据处理成密文数据 让人无法看懂 2.为什么要加密 保证数据的安全,防止密码泄露 3.如何判断数据是否加密 密文数据的表现形式一般都是一串没有规则的字符串( ...

  9. MySQL字符编码、存储引擎、严格模式、字段类型之浮点 字符串 枚举与集合 日期类型

    目录 字符编码与配置文件 数据路储存引擎 创建表的完整语法 字段类型之整型 严格模式 字段类型之浮点型 字段类型之字符串类型 数字的含义 字段类型之枚举与集合 字段类型之日期类型 字符编码与配置文件 ...

  10. js实现n秒内只能点击一次操作

    <button @click="submit">5秒内只能点一次</button>