<font class="fixed_width" face="Courier, Monospaced">Hello! <br> </font><p><font class="fixed_width" face="Courier, Monospaced">I use mock library <a target="_blank" rel="nofollow" href="http://www.google.com/url?sa=D&q=http://www.voidspace.org.uk/python/mock/&usg=AFQjCNESuRzqmFZL9VJ0F-gIZbCJA8bZGw">http://www.voidspace.org.uk/python/mock/</a>. There is <br>
no user group for the library, so I post in comp.lang.python and hope <br> that people who use it will help me. <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">The library allows to patch objects, using patch decorator. Patching <br>
is done only within the scope of the function. So if I have a lot of <br> tests that need specific object to be patched I have to specify the <br> same decorator for each test method: <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">class TestSomething(unittest.TestCase): <br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced"> @patch('module.Class', spec = True) <br> def test_method1(self, MockClass): <br> Class() <br> self.assertTrue(MockClass.called) <br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced"> @patch('module.Class', spec = True) <br> def test_method2(self, MockClass): <br> Class() <br> MockClass.assert_called_with('foo') <br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced"> @patch('module.Class', spec = True) <br> def test_method3(self, MockClass): <br> foo = Class() <br> self.assertRaises(AttributeError, foo.some_method) <br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced"> # and more ... <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">So for every test method I always do the same patching! How can I <br>
avoid this? <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">Thanks in advance. <br> Sorry if my English isn't proper enough. <br> </font></p><font class="fixed_width" face="Courier, Monospaced">With regards, <br>
Maxim. </font>