Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest can't see logs from function being tested

I have a flask app like this

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)

When I run it, I see the WARNING in the logs

WARNING:root:I'm a warning

I then create a test, like this

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')

When I run the test using pytest, I can't see the log message from my function under test. I found How can I see normal print output created during pytest run?, which sounds similar, but the pytest -s option doesn't work and I think it's actually talking about output from the test function, not the function under test.

How can I see the logs from the function under test?

like image 642
Daniel Watrous Avatar asked Dec 21 '18 02:12

Daniel Watrous


1 Answers

This may be the cause. Just create pytest.ini with settings.

[pytest]
log_cli=true
log_level=NOTSET

Works fine:

pytest test_my.py
# ...
-------------------------------- live log call ---------------------------------
fulfillment.py                  37 WARNING  I'm a warning
PASSED                                                                   [100%]

=========================== 1 passed in 0.09 seconds ===========================

Also you can set log_format, log_date_format and other options.

Hope this helps.

like image 115
Danila Ganchar Avatar answered Nov 16 '22 11:11

Danila Ganchar