我剛開始使用python mocking框架。我只想計算一個方法被調用的次數,而不會去除實際調用該方法的效果。使用python mock來計算方法調用次數
例如,在這個簡單的反例,我想都增量計數器和跟蹤,它被稱爲:
import unittest
import mock
class Counter(object):
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
class CounterTest(unittest.TestCase):
def test_increment(self):
c = Counter()
c.increment()
self.assertEquals(1, c.count)
def test_call_count(self):
with mock.patch.object(Counter, 'increment') as fake_increment:
c = Counter()
self.assertEquals(0, fake_increment.call_count)
c.increment()
self.assertEquals(1, fake_increment.call_count)
# increment() didn't actually get called.
self.assertEquals(1, c.count) # Fails.
if __name__ == '__main__':
unittest.main()
是否有可能迫使mock
後調用模擬的方法註冊了電話,還是隻是表示我想保留模擬功能的效果?