Simplified code: I have a function that expects either a number or None, and returns True
if it's None
, and False
if it's a number, like:
def function(var):
return var is None
I want to pass a mocked object and test it with the is
operator like this:
from unittest.mock import Mock
mock = Mock(return_value=None)
assert function()
This will fail although I mocked the value to be None
. According to the documentation, there are many supported magic methods, but none let me implement the behavior for this case.
Changing to assert mock == None
would let me use the __eq__
operator, but I don't want to touch the code (and apparently using is
is faster than ==
according to this answer)