user1292656
user1292656

Reputation: 2560

Django test how to check if function was Not called

I want to write a test in order to check that a specific function was not called. Below is a pseudo example of my code:

Code

TestFunctionA():
  if a > b:
    TestFunctionB()

In order to check if it is called i do the following which is working

        with mock.patch('TestFunctionB') as mock_TestFunctionB:
           TestFunctionA()
           mock_TestFunctionB.assert_called_once_with()

If i want to check if the function TestFunctionB was not called i tried the following but is not working

        with mock.patch('TestFunctionB') as mock_TestFunctionB:
           TestFunctionA()
           assert not mock_TestFunctionB.assert_called_once_with()

Upvotes: 1

Views: 760

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599638

There's a method specifically for that.

mock_TestFunctionB.assert_not_called()

Upvotes: 2

Related Questions