在Python中,为什么当我在调试模式下运行mock_calls时,其中的call.__str__()调用数量不一致?

Pre*_*han 4 python unit-testing mocking

我创建了一个 python 测试,在其中模拟日志记录功能。然后我进行测试以确保 lockking_mock.mock_calls 的数量符合我的预期。

我用的是pycharm。当我运行代码时,测试通过,但是当我调试它时(在断言语句所在的行中使用断点),它没有通过,并且logging_mock.mock_calls包含许多意外的call.__str__()调用。每次列出的 call.__str__() 调用次数都不同。

在调试过程中,我注释掉了主方法中的所有功能,但仍然遇到这个问题。这是仍然给我带来这个问题的代码:

@mock.patch('app.helpers.logging')
def test_main(self, logging_mock):
    """
    Test the main method
    """
    main()

    self.assertEqual(0, len(logging_mock.mock_calls))
Run Code Online (Sandbox Code Playgroud)

这是我的空主:

def main():
    pass
Run Code Online (Sandbox Code Playgroud)

当我在调试器中运行此命令时,为什么会出现不同数量的 call.__str__() 调用?

sga*_*gan 5

对你来说可能为时已晚,但对于其他在这里绊倒的人来说。调试器中的某些内容正在调用应用程序中日志记录对象上的字符串,这由模拟记录为 call()。str ()。我还看到了这一点,我忘记了正确设置模拟上的 return_value 并记录了模拟调用的输出。