Dan*_*ous 6 python logging pytest flask
我有一个这样的烧瓶应用程序
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)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我在日志中看到警告
WARNING:root:I'm a warning
Run Code Online (Sandbox Code Playgroud)
然后我创建一个测试,就像这样
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')
Run Code Online (Sandbox Code Playgroud)
当我使用 运行测试时pytest,我看不到测试中函数的日志消息。我发现如何查看在 pytest 运行期间创建的正常打印输出?,听起来很相似,但该pytest -s选项不起作用,我认为它实际上是在谈论测试函数的输出,而不是被测函数。
如何查看被测函数的日志?
这可能是原因。只需pytest.ini使用设置创建。
[pytest]
log_cli=true
log_level=NOTSET
Run Code Online (Sandbox Code Playgroud)
工作正常:
pytest test_my.py
# ...
-------------------------------- live log call ---------------------------------
fulfillment.py 37 WARNING I'm a warning
PASSED [100%]
=========================== 1 passed in 0.09 seconds ===========================
Run Code Online (Sandbox Code Playgroud)
您也可以设置log_format,log_date_format和其他选项。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
1990 次 |
| 最近记录: |