Hello Michael!<br><br>Thanks for your reply! Yes, this approach works great for me. And actually I think there is another approach, which also can be used:<br><br>class TestSomething(object):<br>    @patch(&#39;module.attribute&#39;, spec = True)<br>
    def setup(self, mock_atrribute):<br>        self.mock_attribute = mock_attribute<br>        attribute(&#39;cool&#39;)<br><br>    def test1(self):<br>        assert self.mock_attribute.called<br><br>    def test2(self):<br>
        self.mock_attribute.assert_called_with(&#39;cool&#39;)<br><br>    # more tests<br><br>Nose successfully discovers and runs these tests.<br><br>With regards,<br>Maxim<br><br><div class="gmail_quote">On 28 February 2010 20:46, Michael Foord <span dir="ltr">&lt;<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">


  
  

<div bgcolor="#ffffff" text="#000000"><div><div></div><div class="h5">
On 25/02/2010 15:06, Максим Lacrima wrote:
<blockquote type="cite"><font face="Courier, Monospaced">Hello!
  <br>
  </font>
  <p><font face="Courier, Monospaced">I use mock
library <a rel="nofollow" href="http://www.google.com/url?sa=D&amp;q=http://www.voidspace.org.uk/python/mock/&amp;usg=AFQjCNESuRzqmFZL9VJ0F-gIZbCJA8bZGw" target="_blank">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 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 face="Courier, Monospaced">class
TestSomething(unittest.TestCase): <br>
  </font></p>
  <p><font face="Courier, Monospaced">   
@patch(&#39;module.Class&#39;, spec = True) <br>
    def test_method1(self, MockClass): <br>
        Class() <br>
        self.assertTrue(MockClass.called) <br>
  </font></p>
  <p><font face="Courier, Monospaced">   
@patch(&#39;module.Class&#39;, spec = True) <br>
    def test_method2(self, MockClass): <br>
        Class() <br>
        MockClass.assert_called_with(&#39;foo&#39;) <br>
  </font></p>
  <p><font face="Courier, Monospaced">   
@patch(&#39;module.Class&#39;, spec = True) <br>
     def test_method3(self, MockClass): <br>
        foo = Class() <br>
        self.assertRaises(AttributeError, foo.some_method) <br>
  </font></p>
  <p><font face="Courier, Monospaced">    # and
more ... <br>
  </font></p>
  <p><font 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></div></div>
Hello Maxim,<br>
<br>
I thought I saw that you got a response to this question, but I can&#39;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 face="Courier, Monospaced">with
patch(&#39;module.Class&#39;, 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 type="cite"><div class="im">
  <p><font face="Courier, Monospaced"> </font></p>
  <p><font face="Courier, Monospaced">Thanks in
advance. <br>
Sorry if my English isn&#39;t proper enough. <br>
  </font></p>
  <font face="Courier, Monospaced">With regards, <br>
Maxim. </font>
  </div><pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
  </pre>
</blockquote>
<br>
<br>
<pre cols="72">-- 
<a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a>
<a href="http://www.voidspace.org.uk/blog" target="_blank">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>
</div>

</blockquote></div><br>