I am trying to unit test the compare_vals method below:
class Class:
def compare_vals(self, input_value):
if input_value > 3:
return True
out_value = self.do_something()
return out_value
def do_something(self):
return False
More specifically, I want to test if self.do_something()
is being called in case the if-statement evaluates False.
I have written the following unit test:
import pytest
from unittest import mock
import costa_test.genericfunctions.simplefunctions as sf
@mock.patch("costa_test.genericfunctions.simplefunctions.Class.do_something")
def test_compare_vals(mock_do_something):
compare_obj = sf.Class()
out_value = compare_obj.compare_vals(2)
assert out_value is True
assert mock_do_something.call_count == 1
I expected that mock_do_something.call_count
would return the value 1 in this case, since the if-statement evaluates False and do_something
is called once.
However, when I run pytest, I get the following:
FAILED tests/test_simplefunctions.py::test_compare_vals - AssertionError: assert <MagicMock name='do_something()' id='140474257049040'> is True
How can I correctly check if the do_something
method is being called?