title: External library mock patch used in all tests doesnt work when tests run together
body: <p>I am using Python's mock library along with unittest. I am writing unit tests for a class that uses a function of an external library in one of its methods. Depending on the case, this function returns different values.</p>
  
  <p>So let's say I wanna test class A:</p>
  
  <pre><code>from external_library import function_foo
  
  class A(object):
  ...
  </code></pre>
  
  <p>In my test class, in order to use the values returned by the function from the external library, I create a patch, and only import class A after defining the patch. However, I need to use this function in all my test methods, and in each method it returns different values.</p>
  
  <p>My test class is as follows:</p>
  
  <pre><code>class TestA(TestCase):
  
      @patch('external_library.function_foo', side_effect=[1, 2, 3])    
      def test_1(self, *patches):
  
         from module import class A
         obj = A()
         ...
  
      @patch('external_library.function_foo', side_effect=[1, 1, 2, 2, 3, 3])    
      def test_2(self, *patches):
  
         from module import class A
         obj = A()
         ...
  
      ...
  </code></pre>
  
  <p>I have 10 tests and only 1 (the first one) passes when I run all of them together, for the rest, I get <code>StopIteration</code> error. However, <strong>if I run each one of them individually, they all pass</strong>.</p>
  
  <p>I have tried using <code>with patch('external_library.function_foo', side_effect=[...])</code> in each method, but the outcome was the same. I also tried creating only once the patch in the <code>setUp</code> method, starting it, reassigning the side_effect within each method, and stopping in <code>tearDown</code>, but it didn't work.</p>
  
  <p>Any ideas on what might work in this case?</p>
  
  <p>Thanks!</p>