Reputation: 2109
In python unittest it says mock.assert_called_once() will fail if it is called more than once. When patching I am not seeing that behavior.
ugh.py
def foo(*args):
pass
def bar():
foo(1)
foo(2)
tests.py
from unittest import TestCase, main
from unittest.mock import patch
from ugh import bar
class Test(TestCase):
@patch('ugh.foo')
def test_called_once(self, foo_mock):
bar()
foo_mock.assert_called_once()
@patch('ugh.foo')
def test_called_count_one(self, foo_mock):
bar()
self.assertEqual(foo_mock.call_count, 1)
if __name__ == '__main__':
main()
And test output.
razorclaw% python tests.py
F.
======================================================================
FAIL: test_called_count_one (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib64/python3.4/unittest/mock.py", line 1142, in patched
return func(*args, **keywargs)
File "tests.py", line 15, in test_called_count_one
self.assertEqual(foo_mock.call_count, 1)
AssertionError: 2 != 1
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1)
Using python 3.4.6 on linux
Upvotes: 2
Views: 2059
Reputation: 2109
assert_called_once
doesn't exist in 3.4. It didn't exist until 3.6. If you are on 3.4 you can use assert_called_once_with
Upvotes: 1