我有一个像这样的烧瓶应用程序
from flask import Flask
import logging
app = Flask(__name__)
@app.route('/')
def catch_all():
logging.warning("I'm a warning")
return "This is a REST API"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
当我运行它时,我在日志中看到警告
WARNING:root:I'm a warning
然后我创建一个测试,像这样
import fulfillment
def test_index():
fulfillment.app.testing = True
client = fulfillment.app.test_client()
r = client.get('/')
assert r.status_code == 200
assert 'This is a REST API' in r.data.decode('utf-8')
当我使用运行测试时pytest
,我看不到正在测试的函数的日志消息。我发现如何查看 pytest 运行期间创建的正常打印输出? https://stackoverflow.com/questions/14405063/how-can-i-see-normal-print-output-created-during-pytest-run,听起来很相似,但是pytest -s
选项不起作用,我认为它实际上是在谈论测试函数的输出,而不是正在测试的函数。
如何查看被测函数的日志?
This https://docs.pytest.org/en/latest/logging.html#incompatible-changes-in-pytest-3-4可能是原因。只需创建pytest.ini
与设置。
[pytest]
log_cli=true
log_level=NOTSET
工作正常:
pytest test_my.py
# ...
-------------------------------- live log call ---------------------------------
fulfillment.py 37 WARNING I'm a warning
PASSED [100%]
=========================== 1 passed in 0.09 seconds ===========================
还可以设置log_format
, log_date_format
和其他选项。
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)