Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用日志、pytest fixture和capsys?_Python_Unit Testing_Logging_Pytest - Fatal编程技术网

Python 如何使用日志、pytest fixture和capsys?

Python 如何使用日志、pytest fixture和capsys?,python,unit-testing,logging,pytest,Python,Unit Testing,Logging,Pytest,我试图对一些使用日志库的算法进行单元测试 我有一个创建记录器的装置 在我的第一个测试用例中,我不使用此夹具,而是使用打印记录到标准输出。此测试用例通过 在我的第二个测试用例中,我使用了这个夹具,但不是像pytest文档中记录的那样。我只是在测试中调用相关函数来获取记录器。然后,我使用日志记录器登录到标准输出。此测试用例通过 在我的第三个测试用例中,我使用pytest文档中记录的这个夹具。夹具作为参数传递给测试函数。然后,我使用日志记录器登录到标准输出。此测试用例失败!它在stdout中找不到任何

我试图对一些使用日志库的算法进行单元测试

我有一个创建记录器的装置

在我的第一个测试用例中,我不使用此夹具,而是使用打印记录到标准输出。此测试用例通过

在我的第二个测试用例中,我使用了这个夹具,但不是像pytest文档中记录的那样。我只是在测试中调用相关函数来获取记录器。然后,我使用日志记录器登录到标准输出。此测试用例通过

在我的第三个测试用例中,我使用pytest文档中记录的这个夹具。夹具作为参数传递给测试函数。然后,我使用日志记录器登录到标准输出。此测试用例失败!它在stdout中找不到任何东西。但是在错误消息中,它说我的日志在捕获的stdout调用中

我做错了什么

import pytest

import logging
import sys

@pytest.fixture()
def logger():

    logger = logging.getLogger('Some.Logger')
    logger.setLevel(logging.INFO)
    stdout = logging.StreamHandler(sys.stdout)
    logger.addHandler(stdout)

    return logger

def test_print(capsys):

    print 'Bouyaka!'

    stdout, stderr = capsys.readouterr()
    assert 'Bouyaka!' in stdout

    # passes

def test_logger_without_fixture(capsys):

    logger().info('Bouyaka!')

    stdout, stderr = capsys.readouterr()
    assert 'Bouyaka!' in stdout

    # passes

def test_logger_with_fixture(logger, capsys):

    logger.info('Bouyaka!')

    stdout, stderr = capsys.readouterr()
    assert 'Bouyaka!' in stdout

    # fails with this error:
    # >       assert 'Bouyaka!' in stdout
    # E       assert 'Bouyaka!' in ''
    #
    # tests/test_logging.py:21: AssertionError
    # ---- Captured stdout call ----
    # Bouyaka!

顺便说一下,如果我对测试用例重新排序,则不会有任何变化。

我猜在设置
capsys
fixture之前会创建记录器(通过fixture)

一些想法:

  • 使用插件
  • 可能是反向
    记录器,capsys
  • 使
    logger
    请求
    capsys
    夹具
  • 使用
    capfd
    ,它在不改变
    sys的情况下进行更低级的捕获

    • 非常感谢您的想法

      反转
      logger,capsys
      ,使
      logger
      请求
      capsys
      夹具并使用
      capfd
      不要更改任何内容

      我尝试了插件,它运行得很好

      import pytest
      
      import logging
      
      @pytest.fixture()
      def logger():
      
          logger = logging.getLogger('Some.Logger')
          logger.setLevel(logging.INFO)
      
          return logger
      
      def test_logger_with_fixture(logger, caplog):
      
          logger.info('Bouyaka!')
      
          assert 'Bouyaka!' in caplog.text
      
          # passes!
      
      在我最初的测试中,我登录到stdout和stderr并捕获它们。 这是一个更好的解决方案,因为我不需要这个调整来检查我的日志是否正常工作

      好吧,现在我只需要重新编写所有测试以使用caplog,但这是我自己的事;)


      现在我有了一个更好的解决方案,剩下的唯一一件事就是了解我的原始测试用例
      def test\u logger\u with_fixture(logger,capsys)

      从pytest 3.3开始,捕获日志消息的功能被添加到pytest核心。这由caplog夹具支持:

      def test_baz(caplog):
          func_under_test()
          for record in caplog.records:
              assert record.levelname != 'CRITICAL'
          assert 'wally' not in caplog.text
      
      有关更多信息,请访问