pytest如何管理log
- 控制台命令运行修改格式:
--log-date-format="%Y-%m-%d %H:%M:%S"
- pytest.ini文件下添加以下代码,修改log输出:
[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
3.临时修改日志等级 caplog fixture
def test_foo(caplog):
caplog.set_level(logging.INFO)
pass
直到test_foo结束后再恢复到之前的设置;
4.capture.clear()清除自定义日志:
def test_something_with_clearing_records(caplog):
some_method_that_creates_log_records()
caplog.clear()
your_test_method()
assert ["Foo"] == [rec.message for rec in caplog.records]
清除捕获的记录和格式化的日志输出字符串
5.The full API is available at pytest.LogCaptureFixture:: pytest.LogCaptureFixture
- capturelog缺少向后兼容,如果是还是要使用,按么在pytest.ini中添加如下代码:
addopts=-p no:logging
tips:
392/5000
日志级别不再改变,除非log_level配置或——Log -level命令行选项明确要求。这允许用户自己配置记录器对象。设置log_level将设置全局捕获的级别,因此,如果某个特定测试需要的级别低于此级别,请使用caplog.set_level()功能,否则该测试将容易失败。
Live Logs现在默认是禁用的,可以将log_cli配置选项设置为true启用。启用后,详细程度会增加,因此每个测试的日志记录都是可见的。仍然在pytest.ini配置文件中添加
如果希望部分恢复3.3版的日志记录行为,可以将此选项添加到ini文件中:
[pytest]
log_cli=true
log_level=NOTSET