0

pytest won't show me the logger records that occur during testing. Checked that logging is enabled in config and that level is DEBUG, added log_cli = 1 to pytest.ini, still doesn't work.

It captures stdout and stderr just fine, so usual print()s are displayed.
Tried running with -r a as advised here, to no avail.

Added caplog to my test, caplog.records is [].

I know that code gets to the log emission point (through the use of prints).

EDIT: minimal reproducible examples:

class TestLogging:
    def test_logging(self):
        from app import logger
        logger.error("log test")  # nothing shows up
        print("print test")  # prints as expected
        assert False

    def test_caplog(self, caplog):
        from app import logger
        logger.error("log test")  # nothing shows up
        print(caplog.records)  # prints '[]'
        assert False

Running with
FLASK_ENV=testing pipenv run pytest controller/tests/test_user_routes.py -k test_logging
and
FLASK_ENV=testing pipenv run pytest controller/tests/test_user_routes.py -k test_caplog

EDIT2: settings and how logger is set up: https://pastebin.com/EG8g90m5

egor83
  • 1,199
  • 1
  • 13
  • 26
  • 3
    Please provide a minimal reproducible example! – Klaus D. Dec 28 '20 at 14:46
  • Working on it now. – egor83 Dec 28 '20 at 14:54
  • The next mystery is your app module - can you provide a minimal example of that? If I just use a dummy app with an index, import via `from app import app` and access the logger via `app.logger.info(...)`, the live log is emitted just fine. – hoefling Dec 28 '20 at 16:50
  • Do you use [`app.logger`](https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.logger) at all? Or do you configure the logger yourself? – hoefling Dec 28 '20 at 16:50
  • @hoefling added pastebin with some details about app. FTR, logger itself work fine in development environment, I see log records in server logs. – egor83 Dec 28 '20 at 19:42
  • Your example code works on my machine, logs are both captured and emitted live. – hoefling Dec 28 '20 at 20:08
  • @hoefling but are they caught and displayed by the test? On my setup, they are emitted as well, I can see them in server logs on live run, pytest just does not display them. – egor83 Dec 29 '20 at 14:14
  • 1
    Yes, both printed in the "live logs" section and captured via `caplog`. – hoefling Dec 29 '20 at 15:12

0 Answers0