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

后端 未结 6 1282
忘掉有多难
忘掉有多难 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 06:55

    In Edit Configurations:

    • delete old configurations
    • go to Defaults / Python tests / NoseTests
    • add --nologcapture to "additional Arguments"

    worked like a "charm" for me in pyCharm 2017.2.3

    thanks bott

    0 讨论(0)
  • 2021-02-05 06:56

    I needed to add too the option --nologcapture to nosetests as param in pycharm 2016.3.2

    Run>Edit COnfigurations > Defaults > Python Tests > Nosetests : activate the check for Prams option and add --nologcapture

    0 讨论(0)
  • 2021-02-05 06:56

    I experienced this problem all of a sudden after updating Pycharm. I had been running my tests with the Unittest runner, and all of a sudden Pycharm decided the default should be the pytest runner. When I changed the default test runner back to Unittest the logs appeared as I expected they would.
    You can change the default test runner in Project Settings/Preferences >> Tools >> Python Integrated Tools

    I assume adding the -nologcapture flag as noted in other answers probably would have worked in my case as well, but I prefer a solution that's exposed by the IDE rather than a manual override.

    BTW choosing Unittest also solves an additional error that I got once when trying to run tests - No module named 'nose'

    0 讨论(0)
  • 2021-02-05 07:13

    The only solution I've found is to do the normal logging setup at the top of the file with tests in it (assuming you're just running one test or test class): logging.basicConfig(level=logging.DEBUG). Make sure you put that before most import statements or the default logging for those modules will have already been set (that was a hard one to figure out!).

    0 讨论(0)
  • 2021-02-05 07:15

    Add a stream handler, pointing to sys.stdout if doctest or pytest is running:

        import logging
        import sys
        logger = logging.getLogger()
    
        def setup_doctest_logger(log_level:int=logging.DEBUG):
            """
    
            :param log_level:
            :return:
    
            >>> logger.info('test')     # there is no output in pycharm by default
            >>> setup_doctest_logger()
            >>> logger.info('test')     # now we have the output we want
            test
    
            """
            if is_pycharm_running():
                logger_add_streamhandler_to_sys_stdout()
            logger.setLevel(log_level)
    
        def is_pycharm_running()->bool:
            if ('docrunner.py' in sys.argv[0]) or ('pytest_runner.py' in sys.argv[0]):
                return True
            else:
                return False
    
        def logger_add_streamhandler_to_sys_stdout():
            stream_handler=logging.StreamHandler(stream=sys.stdout)
            logger.addHandler(stream_handler)
    
    0 讨论(0)
  • 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')
    
    0 讨论(0)
提交回复
热议问题