[TIP] Using mock library (written by Michael Foord)

Максим Lacrima lacrima.maxim at gmail.com
Thu Feb 25 07:26:44 PST 2010


Sorry, there might be strange formatting in my message above. I copied it
from comp.lang.python and then pasted it to this list, and forgot to rectify
it.

On 25 February 2010 17:06, Максим Lacrima <lacrima.maxim at gmail.com> wrote:

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


More information about the testing-in-python mailing list