在PyCharm中进行单元测试时,如何查看日志消息?

时间:2022-03-20 00:11:39

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, and normally I'd set a root logger to catch the debugging messages at a certain level, and pipe the other logs to a file. But I can't figure out how this works with unit tests.

我确定这是一个简单的修复,但我想在运行单元测试时在PyCharm控制台中查看日志消息。我正在测试的模块有自己的记录器,通常我会设置一个根记录器来捕获某个级别的调试消息,并将其他日志传递给一个文件。但我无法弄清楚它如何与单元测试一起工作。

I'm using the unittest2 module, and using PyCharm's automatic test discovery (which probably is based on nose, but I don't know).

我正在使用unittest2模块,并使用PyCharm的自动测试发现(可能是基于鼻子,但我不知道)。

I've tried fooling with the run configurations, but there doesn't seem to be a straightforward way to do this.

我试过愚弄运行配置,但似乎没有一种直接的方法来做到这一点。

The PyCharm documentation isn't particularly helpful here either, if any of you work there.

如果你们中的任何人在那里工作,那么PyCharm文档在这里也没有特别的帮助。


In edit: It DOES appear that the console catches critical level log messages. I want to know if there is a way to configure this to catch debug level messages.

在编辑中:它似乎表明控制台捕获了关键级别的日志消息。我想知道是否有办法配置它来捕获调试级别的消息。


This post (Pycharm unit test interactive debug command line doesn't work) suggests adding the -s option to the build configuration, which does not produce the desired result.

这篇文章(Pycharm单元测试交互式调试命令行不起作用)建议在构建配置中添加-s选项,这不会产生所需的结果。

4 个解决方案

#1


8  

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!).

我发现的唯一解决方案是在文件顶部进行常规日志记录设置,并在其中进行测试(假设您只运行一个测试或测试类):logging.basicConfig(level = logging.DEBUG)。确保在大多数import语句之前放置它,或者已经设置了那些模块的默认日志记录(这很难理解!)。

#2


3  

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

我需要在pycharm 2016.3.2中添加选项--nologcapture到nosetests作为param

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

运行>编辑配置>默认值> Python测试> Nosetests:激活检查Prams选项并添加--nologcapture

#3


1  

In Edit Configurations:

在编辑配置中:

  • delete old configurations
  • 删除旧配置
  • go to Defaults / Python tests / NoseTests
  • 转到默认值/ Python测试/ NoseTests
  • add --nologcapture to "additional Arguments"
  • 将--nologcapture添加到“附加参数”

worked like a "charm" for me in pyCharm 2017.2.3

在pyCharm 2017.2.3中,对我来说就像一个“魅力”

thanks bott

谢谢你

#4


-2  

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

添加流处理程序,如果doctest或pytest正在运行,则指向sys.stdout:

    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)

#1


8  

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!).

我发现的唯一解决方案是在文件顶部进行常规日志记录设置,并在其中进行测试(假设您只运行一个测试或测试类):logging.basicConfig(level = logging.DEBUG)。确保在大多数import语句之前放置它,或者已经设置了那些模块的默认日志记录(这很难理解!)。

#2


3  

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

我需要在pycharm 2016.3.2中添加选项--nologcapture到nosetests作为param

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

运行>编辑配置>默认值> Python测试> Nosetests:激活检查Prams选项并添加--nologcapture

#3


1  

In Edit Configurations:

在编辑配置中:

  • delete old configurations
  • 删除旧配置
  • go to Defaults / Python tests / NoseTests
  • 转到默认值/ Python测试/ NoseTests
  • add --nologcapture to "additional Arguments"
  • 将--nologcapture添加到“附加参数”

worked like a "charm" for me in pyCharm 2017.2.3

在pyCharm 2017.2.3中,对我来说就像一个“魅力”

thanks bott

谢谢你

#4


-2  

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

添加流处理程序,如果doctest或pytest正在运行,则指向sys.stdout:

    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)