How can I see log messages when unit testing in PyCharm?

后端 未结 6 1284
忘掉有多难
忘掉有多难 2021-02-05 06:35

I\'m sure this is a simple fix, but I\'d like to view log messages in the PyCharm console while running a unit test. The modules I\'m testing have their own loggers, an

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 07:19

    My problem was similar, I only saw messages with level WARNING or higher while running tests in PyCharm. A colleague suggested to configure the logger in __init__.py which is in the directory of my tests.

    # in tests/__init__.py
    import logging
    import sys
    
    # Reconfiguring the logger here will also affect test running in the PyCharm IDE
    log_format = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format)
    

    Then I can simply log in the test code like:

    import logging
    logging.info('whatever')
    

提交回复
热议问题