一、在pytest.ini配置如下配置
cli live log 部分配置是实时日志,即自动化脚本执行的过程中实时输出,方便调试脚本使用,capture log则是当脚本失败了,会在执行日志的最后将失败的用例中的日志打印输出,便于定位失败的脚本,尤其在执行大量的脚本时,实时日志很难找到报错的位置,capture log则把失败的日志在最后重新打印一遍,如此则非常方便问题定位
[pytest]
# cli live log
log_cli = True
log_cli_level = info
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
# capture log
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_level = info
二、脚本中使用方法
直接导入logging,然后直接调用logging的info,error,warnning等方法写日志即可,如下演示一个成功的测试函数,一个失败的测试函数,capture log只会捕获失败的用例的日志
import logging
def test_func1():
logging.info("info log in test_func1...")
logging.error("error log in test_func1...")
logging.warning("warnning log in test_func1...")
def test_func2():
logging.info("info log in test_func2...")
logging.error("error log in test_func2...")
logging.warning("warnning log in test_func2...")
assert 1==2
执行结果如下:
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items
test_demo.py::test_func1
---------------------------------------------------------------------------- live log call -----------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func1...
2021-12-28 10:21:51 ERROR error log in test_func1...
2021-12-28 10:21:51 WARNING warnning log in test_func1...
PASSED [ 50%]
test_demo.py::test_func2
---------------------------------------------------------------------------- live log call -----------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func2...
2021-12-28 10:21:51 ERROR error log in test_func2...
2021-12-28 10:21:51 WARNING warnning log in test_func2...
FAILED [100%]
=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_func2 ______________________________________________________________________________
def test_func2():
logging.info("info log in test_func2...")
logging.error("error log in test_func2...")
logging.warning("warnning log in test_func2...")
> assert 1==2
E assert 1 == 2
E +1
E -2
test_demo.py:12: AssertionError
-------------------------------------------------------------------------- Captured log call ---------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func2...
2021-12-28 10:21:51 ERROR error log in test_func2...
2021-12-28 10:21:51 WARNING warnning log in test_func2...
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_func2 - assert 1 == 2
===================================================================== 1 failed, 1 passed in 0.12s ======================================================================