用于捕获日志消息的py.test插件。这是Pytest CaptureLog的分叉。
pytest-catchlog的Python项目详细描述
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-catchlog0
也可以使用上下文管理器临时更改 日志级别:
pip install pytest-catchlog easy_install pytest-catchlog1
同样,默认情况下,处理程序的级别会受到影响,但是 任何记录器的更改都可以使用:
pip install pytest-catchlog easy_install pytest-catchlog2
最后,在测试运行期间发送给记录器的所有日志都被生成 在函数arg上以两个logrecord实例的形式提供 以及最后的日志文本。当您想要断言 消息的内容:
pip install pytest-catchlog easy_install pytest-catchlog3
有关日志记录的所有可用属性,请参见 日志记录 类。
如果您只想确保, 某些消息已用给定的记录器名称和 给定严重性和消息:
pip install pytest-catchlog easy_install pytest-catchlog4