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

Максим Lacrima lacrima.maxim at gmail.com
Tue Mar 2 00:58:20 PST 2010


Hello Michael!

Thanks for your reply! Yes, this approach works great for me. And actually I
think there is another approach, which also can be used:

class TestSomething(object):
    @patch('module.attribute', spec = True)
    def setup(self, mock_atrribute):
        self.mock_attribute = mock_attribute
        attribute('cool')

    def test1(self):
        assert self.mock_attribute.called

    def test2(self):
        self.mock_attribute.assert_called_with('cool')

    # more tests

Nose successfully discovers and runs these tests.

With regards,
Maxim

On 28 February 2010 20:46, Michael Foord <fuzzyman at voidspace.org.uk> wrote:

>  On 25/02/2010 15:06, Максим Lacrima 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?
>
>
> Hello Maxim,
>
> I thought I saw that you got a response to this question, but I can't find
> it in the archives or on comp.lang.python...
>
> Anyway - patch can be used as a context manager, so one answer (if you are
> using Python >= 2.5) is to use the with statement:
>
> with patch('module.Class', spec=True) as MockClass:
>
>     test1()
>     MockClass.reset_mock()
>
>     test2()
>     MockClass.reset_mock()
>
> etc....
>
> All the best,
>
> Michael Foord
>
>   Thanks in advance.
> Sorry if my English isn't proper enough.
>  With regards,
> Maxim.
>
>
> _______________________________________________
> testing-in-python mailing listtesting-in-python at lists.idyll.orghttp://lists.idyll.org/listinfo/testing-in-python
>
>
>
> -- http://www.ironpythoninaction.com/http://www.voidspace.org.uk/blog
>
> READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100302/fc1c9bbe/attachment.htm>


More information about the testing-in-python mailing list