浅谈Pytest中的warning处理
浅谈Pytest中的warning处理
没有处理warning
- 我们写一个简单的测试
import pytest
def test_demo():
import warnings
warnings.warn('test warn',DeprecationWarning)
assert True
if __name__ == '__main__':
pytest.main(['-sv',__file__])
- 你运行的话会有如下提示
============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-7.1.2, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
metadata: ...
rootdir: D:\pythonProject\AutoTest\PytestTemp, configfile: pytest.ini
plugins: ...
collecting ... collected 1 item
test_demo.py::test_demo PASSED
============================== warnings summary ===============================
testCases/test_demo.py::test_demo
test_demo.py:5: DeprecationWarning: test warn
warnings.warn('test warn',DeprecationWarning)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================== 1 passed, 1 warning in 0.05s =========================
进程已结束,退出代码为 0
- warning在pytest3.1之后会在执行期间捕获,初学者多见于mark漏了标记,可以参考浅谈Pytest中的marker
-W 命令行及处理
pytest提供了一个命令行参数-W
-W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS
含义: set which warnings to report, see -W option of python itself.
python的-W
-W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg
https://docs.python.org/3/library/warnings.html#warning-filter
关于格式说明
action is one of the following strings:
Value Disposition "default"print the first occurrence of matching warnings for each location (module + line number) where the warning is issued "error"turn matching warnings into exceptions "ignore"never print matching warnings "always"always print matching warnings "module"print the first occurrence of matching warnings for each module where the warning is issued (regardless of line number) "once"print only the first occurrence of matching warnings, regardless of location message is a string containing a regular expression that the start of the warning message must match, case-insensitively. In
-WandPYTHONWARNINGS, message is a literal string that the start of the warning message must contain (case-insensitively), ignoring any whitespace at the start or end of message.category is a class (a subclass of
Warning) of which the warning category must be a subclass in order to match.module is a string containing a regular expression that the start of the fully qualified module name must match, case-sensitively. In
-WandPYTHONWARNINGS, module is a literal string that the fully qualified module name must be equal to (case-sensitively), ignoring any whitespace at the start or end of module.lineno is an integer that the line number where the warning occurred must match, or
0to match all line numbers.
增加命令行处理
# action:message:category:module:lineno # 注意格式
-W error::DeprecationWarning # 意思是看到DeprecationWarning就把它当做是一个error
================================== FAILURES ===================================
__________________________________ test_demo __________________________________ def test_demo():
import warnings
> warnings.warn('test warn',DeprecationWarning)
E DeprecationWarning: test warn test_demo.py:5: DeprecationWarning
=========================== short test summary info ===========================
FAILED test_demo.py::test_demo - DeprecationWarning: test warn
============================== 1 failed in 0.09s ==============================可以看到case变成了failed
换一下,改为ignore,case就PASSED了
pytest.main(['-sv','-W ignore::DeprecationWarning',__file__])
============================== 1 passed in 0.07s ==============================
等价的装饰器pytest.mark.filterwarnings
命令行的做法也可以等价到装饰器的写法,跟大多数的插件类似,命令行是针对所有的,装饰器是针对某个case的
@pytest.mark.filterwarnings('ignore::DeprecationWarning') # 装饰在被测函数上即可 @pytest.mark.filterwarnings('error::DeprecationWarning')
等价的pytest.ini中的filterwarnings
你也可以这样写一个pytest.ini
[pytest]
filterwarnings:
ignore::DeprecationWarning或者这样
[pytest]
filterwarnings:
error::DeprecationWarning
还可以这样
[pytest]
filterwarnings:
error
ignore::DeprecationWarning这个意思是,所有的warning都被处理成error,但忽略DeprecationWarning。
注意:当警告与列表中的多个选项匹配时,将执行最后一个匹配选项的操作。
关于warning的其他
--disable-warnings命令行选项可以禁用warning summary
============================== warnings summary ===============================
testCases/test_demo.py::test_demo
test_demo.py:5: DeprecationWarning: test warn
warnings.warn('test warn',DeprecationWarning) -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html上面的warning加了命令行之后就没了
命令行的解释
--disable-warnings, --disable-pytest-warnings : disable warnings summary
还有一个命令行-pno:warnings,完全禁用警告捕获
pytest.main(['-sv','-pno:warnings',__file__])实测-pno:warnings的pno要挨在一起,这有点...
命令行的解释
-p name early-load given plugin module name or
entry point (multi-allowed).
To avoid loading of plugins, use the
`no:` prefix, e.g. `no:doctest`.如果你放在pytest.ini中的pno就可分可合(离谱)
[pytest]
addopts = -p no:warnings # -pno:warnings
浅谈Pytest中的warning处理的更多相关文章
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- 浅谈Linux中的信号处理机制(二)
首先谢谢 @小尧弟 这位朋友对我昨天夜里写的一篇<浅谈Linux中的信号处理机制(一)>的指正,之前的题目我用的“浅析”一词,给人一种要剖析内核的感觉.本人自知功力不够,尚且不能对着Lin ...
- 浅谈Java中的对象和引用
浅谈Java中的对象和对象引用 在Java中,有一组名词经常一起出现,它们就是“对象和对象引用”,很多朋友在初学Java的时候可能经常会混淆这2个概念,觉得它们是一回事,事实上则不然.今天我们就来一起 ...
- 浅谈Java中的equals和==
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: String str1 = new String("hello"); String str2 = ...
- 转【】浅谈sql中的in与not in,exists与not exists的区别_
浅谈sql中的in与not in,exists与not exists的区别 1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...
- 浅谈iOS中的userAgent
浅谈iOS中的userAgent User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产 ...
- 浅谈JavaScript中的闭包
浅谈JavaScript中的闭包 在JavaScript中,闭包是指这样一个函数:它有权访问另一个函数作用域中的变量. 创建一个闭包的常用的方式:在一个函数内部创建另一个函数. 比如: functio ...
- 浅谈sql中的in与not in,exists与not exists的区别
转 浅谈sql中的in与not in,exists与not exists的区别 12月12日北京OSC源创会 —— 开源技术的年终盛典 » sql exists in 1.in和exists ...
- 浅谈Java中的深拷贝和浅拷贝(转载)
浅谈Java中的深拷贝和浅拷贝(转载) 原文链接: http://blog.csdn.net/tounaobun/article/details/8491392 假如说你想复制一个简单变量.很简单: ...
- 浅谈Java中的深拷贝和浅拷贝
转载: 浅谈Java中的深拷贝和浅拷贝 假如说你想复制一个简单变量.很简单: int apples = 5; int pears = apples; 不仅仅是int类型,其它七种原始数据类型(bool ...
随机推荐
- docker搭建ddns
ddns 容器 https://hub.docker.com/r/chen... https://github.com/honwen/ali... docker pull chenhw2/aliyun ...
- EasyPoi 导出Excel(ExcelExportEntity生成表头)
[引入依赖] <!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artif ...
- 【Java并发007】原理层面:ReentrantLock中lock()、unlock()全解析
一.前言 Java线程同步两种方式,synchronized关键字和Lock锁机制,其中,AQS队列就是Lock锁实现公平加锁的底层支持. 二.AQS源码对于lock.lock()的实现 2.1 AQ ...
- 图文并茂解释开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
世界上的开源许可证(Open Source License)大概有上百种,而我们常用的开源软件协议大致有GPL.BSD.MIT.Mozilla.Apache和LGPL. 从下图中可以看出几种开源软件协 ...
- 记一次 .NET 某自动化集采软件 崩溃分析
一:背景 1.讲故事 前段时间有位朋友找到我,说他的程序在客户的机器上跑着跑着会出现偶发卡死,然后就崩掉了,但在本地怎么也没复现,dump也抓到了,让我帮忙看下到底怎么回事,其实崩溃类的dump也有简 ...
- 【Java并发入门】02 Java内存模型:看Java如何解决可见性和有序性问题
如何解决其中的可见性和有序性导致的问题,这也就引出来了今天的主角--Java 内存模型. 一.什么是 Java 内存模型? 导致可见性的原因是缓存,导致有序性的原因是编译优化,那解决可见性.有序性最直 ...
- 【实时数仓】Day01-数据采集层:数仓分层、实时需求、架构分析、日志数据采集(采集到指定topic和落盘)、业务数据采集(MySQL-kafka)、Nginx反向代理、Maxwell、Canel
一.数仓分层介绍 1.实时计算与实时数仓 实时计算实时性高,但无中间结果,导致复用性差 实时数仓基于数据仓库,对数据处理规划.分层,目的是提高数据的复用性 2.电商数仓的分层 ODS:原始日志数据和业 ...
- 【每日一题】【模拟】2021年11月11日--LRU 缓存机制
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 .实现 LRUCache 类: LRUCache(int capacity) 以正整数作为容量 capacity 初始化 L ...
- 说说真实Java项目的开发流程,以及面试前的项目准备说辞
介绍项目是必不可少的Java面试环节,求职者需要借此证明自己真实Java项目的经验,如果再做的好的话,需要借此展开自己的亮点说辞. 不过之前如果只有学习项目经验,比如是自己跑通一个项目,或者是在培训班 ...
- 利用python数据分析
利用python进行数据分析 本书由Python pandas项目创始人Wes McKinney亲笔撰写,详细介绍利用Python进行操作.处理.清洗和规整数据等方面的具体细节和基本要点.第2版针对P ...