Pytest輸出打印時(shí)遇到的問(wèn)題:
用例執(zhí)行完成后,才輸出日志派敷,期望能實(shí)時(shí)輸出日志
添加loging代碼片段
import logging
logging.info("請(qǐng)求URL:" +request.url)
解決方法
- 在當(dāng)前文件夾下創(chuàng)建pytest.ini文件,然后將日志相關(guān)寫(xiě)在里面篮愉,如下:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
- 直接用pytest -o方式重寫(xiě)试躏,這個(gè)功能在pytest 3.4之后才實(shí)現(xiàn),如下
pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO
此外Pytest運(yùn)行時(shí)需要加上-s
pytest.main(['-qs', __file__.split('/')[-1])