[英]pytest can't see logs from function being tested
我有一個這樣的燒瓶應用程序
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 運行期間創建的正常打印輸出? ,聽起來很相似,但pytest -s
選項不起作用,我認為它實際上是在談論測試函數的輸出,而不是被測函數。
如何查看被測函數的日志?
這可能是原因。 只需使用設置創建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
和其他選項。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.