title: Python how to reuse a Mock to avoid writing mock.patch multiple times?
body: <p>Given code like the following:</p>
  
  <pre><code>import flask
  import time
  
  app = flask.Flask(__name__)
  
  def authorize():
      print('starting authorize io')
      time.sleep(1)
      print('done authorize io')
  
  class BlockingIo():
      def __init__(self, n):
          self.n = n
      def do(self):
          print('starting blocking io')
          time.sleep(1)
          print('ending blocking io')
  
  @app.route('/', methods=['GET'])
  @app.route('/&lt;int:n&gt;/', methods=['GET'])
  def foo(n=1):
      authorize()
      b = BlockingIo(n)
      b.do()
      return str(n), 200
  
  #app.run(port=5000)
  </code></pre>
  
  <p>I want to be able to write several tests for <code>GET /n/</code>, each of which mocks <code>authorize</code> and <code>BlockingIO(n)</code>:</p>
  
  <pre><code>app.testing = True
  testapp = app.test_client()
  
  import unittest
  from unittest import mock
  
  mock.patch('__main__.authorize')
  
  class TestBlockingIo(unittest.TestCase):
      @mock.patch('__main__.authorize')
      @mock.patch('__main__.BlockingIo.do')
      def test_1(self, m, m2):
          r = testapp.get('/1/')
          self.assertEquals(r.data, b'1')
      @mock.patch('__main__.authorize')
      @mock.patch('__main__.BlockingIo.do')
      def test_2(self, m, m2):
          r = testapp.get('/2/')
          self.assertEquals(r.data, b'2')
  
  unittest.main()
  </code></pre>
  
  <p>However, I do not want to write out <code>@mock.patch</code> decorator over and over again.</p>
  
  <p>I know we can use a class decorator, and I can subclass for more reusability:</p>
  
  <pre><code>@mock.patch('__main__.authorize')
  @mock.patch('__main__.BlockingIo.do')
  class TestBlockingIo(unittest.TestCase):
      def test_1(self, m, m2):
          r = testapp.get('/1/')
          self.assertEquals(r.data, b'1')
      def test_2(self, m, m2):
          r = testapp.get('/2/')
          self.assertEquals(r.data, b'2')
  </code></pre>
  
  <p>But, this forces all the test functions in the class to take one extra argument for each mock. What if I have tests in this class that do not need mocks for <code>BlockingIo</code> or <code>authorize</code>?</p>
  
  <p>I suppose what I would like is a way to do the following:</p>
  
  <pre><code>m = mock.something('__main__.authorize')
  m2 = mock.something('__main__.BlockingIo.do')    
  
  class TestBlockingIo(unittest.TestCase):
      def test_1(self):
          r = testapp.get('/1/')
          self.assertEquals(r.data, b'1')
      def test_2(self):
          r = testapp.get('/2/')
          self.assertEquals(r.data, b'2')
  </code></pre>
  
  <p>How can I reuse my <code>@mock.patch('__main__.authorize')</code> and <code>@mock.patch('__main__.BlockingIo.do')</code> to avoid repeating myself through the tests?</p>