<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
On 25/02/2010 15:06, Максим Lacrima wrote:
<blockquote
 cite="mid:5bb76e241002250706q4b34015nb69af020b5cd4449@mail.gmail.com"
 type="cite"><font class="fixed_width" face="Courier, Monospaced">Hello!
  <br>
  </font>
  <p><font class="fixed_width" face="Courier, Monospaced">I use mock
library <a moz-do-not-send="true" target="_blank" rel="nofollow"
 href="http://www.google.com/url?sa=D&amp;q=http://www.voidspace.org.uk/python/mock/&amp;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>
</blockquote>
<br>
Hello Maxim,<br>
<br>
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...<br>
<br>
Anyway - patch can be used as a context manager, so one answer (if you
are using Python &gt;= 2.5) is to use the with statement:<br>
<br>
<font class="fixed_width" face="Courier, Monospaced">with
patch('module.Class', spec=True)</font>
as MockClass:<br>
<br>
    test1()<br>
    MockClass.reset_mock()<br>
<br>
    test2()<br>
    MockClass.reset_mock()<br>
<br>
etc....<br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote
 cite="mid:5bb76e241002250706q4b34015nb69af020b5cd4449@mail.gmail.com"
 type="cite">
  <p><font class="fixed_width" face="Courier, Monospaced"> </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>
  <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
  </pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.ironpythoninaction.com/">http://www.ironpythoninaction.com/</a>
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/blog">http://www.voidspace.org.uk/blog</a>

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.

</pre>
</body>
</html>