inveniosoftware / troubleshooting

DEPRECATED - Use the forum instead:
https://invenio-talk.web.cern.ch
5 stars 4 forks source link

flask 0.13/1.0 no logging output in tests with click #35

Open lnielsen opened 6 years ago

lnielsen commented 6 years ago

Problem

Flask development version (future 0.13 or 1.0) simplifies their logging handling (see pallets/flask#2436). This causes troubles when testing output from click commands with pytest:

Hence, a click command which uses the logger to output errors to the console will thus not work during tests.

I.e. given a Click command similar to this:

@cli.command(...)
def mycmd():
    current_app.logger.error('My output')

and a test similar to this:

test_mycmd():
    res = cli_runner(mycmd)
    assert 'My output' in res.output 

Above test will work in Flask 0.12, but not Flask 0.13/1.0.

Solution

Adding the default handler in your application fixture will fix the issue:

Pseudo code:

try:
    from flask.logging import default_handler
except ImportError:
    default_handler = None

@pytest.fixture()
def app():
     if default_handler:
           app_.logger.addHandler(default_handler)