I have a test suite (using the nose, not unittest), and I want to fix a function to return a specific sequence of values for each test in the test class. My first attempt, using a simplified example, was as follows:
@patch('time.clock', MagicMock(side_effects=[1, 2]))
class Tests:
def test_1(self):
assert time.clock() == 1
assert time.clock() == 2
def test_2(self):
assert time.clock() == 1
assert time.clock() == 2
However, an instance of MagicMock is created only once, so the second test fails when the side effects end. I can fix each test method separately, but I don’t want to duplicate the patch decorator on top of all of them (there are much more tests than in this example!) Another way I could do this is to create a patch in the installation code:
class Tests:
def setup(self):
self.old_clock = time.clock
time.clock = MagicMock(side_effects=[1, 2])
def teardown(self):
time.clock = self.old_clock
def test_1(self):
assert time.clock() == 1
assert time.clock() == 2
def test_2(self):
assert time.clock() == 1
assert time.clock() == 2
-, Mock . , ? - ?