Using mock library (written by Michael Foord)

Lacrima lacrima.maxim at
Thu Feb 25 11:56:41 CET 2010


I use mock library 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):

    @patch('module.Class', spec = True)
    def test_method2(self, MockClass):

    @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,

More information about the Python-list mailing list