In python, why does mock_calls have an inconsistent number of call.__str__() calls in it when I run it in debug mode?

Viewed 859

I have created a python test where I am mocking a logging function. I then test to make sure that the number of loccking_mock.mock_calls is what I expected.

I'm using pycharm. When I run the code, the test passes, but when I debug it (with a breakpoint in the line with my assert statement), it doesn't pass, and logging_mock.mock_calls contains a lot of unexpected call.__str__() calls. The number of call.__str__() calls listed varies each time.

In the process of debugging, I commented out all of the functionality in my main method but I am still getting this problem. Here is the code that still gives me this problem:

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

    self.assertEqual(0, len(logging_mock.mock_calls))

And here's my empty main:

def main():
    pass

Why is there a varying number of call.__str__() calls when I run this in my debugger?

1 Answers

probably too late for you, but for anyone else stumbling here. Something in your debugger is calling string on the logging object in your app, this is getting recorded by the mock as call().str(). I've also seen this where I've forgotten to set the return_value on the mock correctly and logged the output of a mocked call.

Related