用于捕获日志消息的py.test插件。这是Pytest CaptureLog的分叉。

pytest-catchlog的Python项目详细描述


pytest catchlog

在https://gitter.im/eisensheng/pytest-catchlog加入聊天

用于捕获日志消息的py.test插件。这是pytest capturelog的分支。

安装

pytest-catchlog包可以与pip或easy-install一起安装:

pip install pytest-catchlog
easy_install pytest-catchlog

用法

如果插件已安装,则默认情况下会捕获日志消息 每个失败的测试将以与捕获的标准输出相同的方式显示,并且 >

无选项运行:

py.test test_pytest_catchlog.py

显示失败的测试,如so:

----------------------- Captured stdlog call ----------------------
test_pytest_catchlog.py    26 INFO     text going to logger
----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

默认情况下,每个捕获的日志消息都显示模块、行号, 日志级别和消息。显示准确的模块和行号是 对测试和调试有用。如果需要,日志格式和日期 格式可以指定为日志模块支持的任何内容。

运行pytest指定格式化选项:

py.test --log-format="%(asctime)s %(levelname)s %(message)s" \
        --log-date-format="%Y-%m-%d %H:%M:%S" test_pytest_catchlog.py

显示失败的测试,如so:

----------------------- Captured stdlog call ----------------------
2010-04-10 14:48:44 INFO text going to logger
----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

这些选项也可以通过配置文件进行自定义:

[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

尽管可以通过 addopts 设置实现相同的效果, 应该首选使用专用选项,因为后者没有 强制其他开发人员安装pytest catchlog 同时, addopts 方法将失败,并出现"无法识别的参数" 错误)。命令行参数优先。

此外,还可以禁用失败测试的报告日志 完全使用:

py.test --no-print-logs test_pytest_catchlog.py

以正常方式显示失败的测试,因为未捕获日志:

----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

内部测试可以更改捕获的 记录消息。这是由 caplog funcarg:

def test_foo(caplog):
    caplog.set_level(logging.INFO)
    pass

默认情况下,该级别设置在用于捕获日志的处理程序上 不过,为了方便起见,也可以设置日志 任何记录器的级别:

pip install pytest-catchlog
easy_install pytest-catchlog
0

也可以使用上下文管理器临时更改 日志级别:

pip install pytest-catchlog
easy_install pytest-catchlog
1

同样,默认情况下,处理程序的级别会受到影响,但是 任何记录器的更改都可以使用:

pip install pytest-catchlog
easy_install pytest-catchlog
2

最后,在测试运行期间发送给记录器的所有日志都被生成 在函数arg上以两个logrecord实例的形式提供 以及最后的日志文本。当您想要断言 消息的内容:

pip install pytest-catchlog
easy_install pytest-catchlog
3

有关日志记录的所有可用属性,请参见 日志记录 类。

如果您只想确保, 某些消息已用给定的记录器名称和 给定严重性和消息:

pip install pytest-catchlog
easy_install pytest-catchlog
4

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何加载JNI。具有依赖项的动态库文件,但未获得未满足的链接错误?   java ResultSet元数据未返回第一行   无法从java xml解析器中的当前节点获取节点列表   java如何从for循环值创建单个数组   java如何在没有arrayList的情况下查找对象   java ItemizedOverlayWithFocus在聚焦osmdroid时显示另一个覆盖   java使用迭代器进行“向下”迭代的优雅方式   java如何使用OnListItemClick为listview和AlertDialog使用单独的arraylist?   arraylist读取txt文件Java中存储的数组   javascript IE 11与RicoLive Grid 2.2框架不兼容   java我能得到水豚的maven依赖性吗?   简单Escp Java:PrintPreviewPane在JDK7中不起作用   java取出给定字符串的一部分,并将所取部分作为变量返回?   java如何替换“instanceof”函数中的变量   来自url的Java utf8编码   java方法获取类Gregorianalendar?   安卓如何在java 7中使用AndroidStudio和DDMS监视器   java测试在JUnit4下运行,但不是在JUnit5下运行-编译干净,但执行0测试   java无法使用Azure服务总线发送消息