title: Can I patch an object method using a decorator
body: <p>Challenge: Patching/mocking a method call in a remote class to return a known piece of data.</p>
  
  <p>I have a whole series of tests that look like:</p>
  
  <pre><code>@pytest.mark.gen_test
  def test_assignment5(app):
      with patch.object(BaseHandler, 'get_current_user') as mock_user:
          mock_user.return_value = {"name": '1_kiz'}
          with patch.object(BaseHandler, 'get_auth_state') as mock_state:
              mock_state.return_value = { "course_id": "course_2",
                                          "course_role": "Instructor",
                                          "course_title": "A title",
                                      }
              r = yield async_requests.get(app.url + "/assignment?course_id=course_2&amp;assignment_id=assign_a")
      assert r.status_code == 200
      response_data = r.json()   
      assert response_data["success"] == False
      assert response_data["note"] == "Assignment assign_a does not exist"   
  </code></pre>
  
  <p>(<code>app</code> is the core method for my application, and <code>get_current_user</code> &amp; <code>get_auth_state</code> use information outside the scope of the app to get a value, so need <em>fudged</em>)</p>
  
  <p>..... the repeating <code>with</code> segments offend my sensibility of good-looking code.</p>
  
  <p>I know I could pull the return-value dictionaries to top-level variables, and that would reduce <em>some</em> of the repeated code, however I'm still repeating the <code>with patch.object</code> stuff every time.</p>
  
  <p>Reading <a href="https://docs.python.org/dev/library/unittest.mock.html#unittest.mock.patch" rel="nofollow noreferrer">https://docs.python.org/dev/library/unittest.mock.html#unittest.mock.patch</a> I know I could simply decorate each test, however</p>
  
  <pre><code>user_kiz = { ... }
  auth_inst = { ... }
  
  ....
  
  @pytest.mark.gen_test
  @patch('BaseHandler.get_current_user', return_value = user_kiz)
  @patch('BaseHandler.get_auth_state', return_value = auth_inst)
  def test_assignment5a(app, kiz):
      r = yield async_requests.get(app.url + "/assignment?course_id=course_2&amp;assignment_id=assign_a")
      assert r.status_code == 200
      response_data = r.json()   
      assert response_data["success"] == False
      assert response_data["note"] == "Assignment assign_a does not exist"
  </code></pre>
  
  <p>just gives me a <code>ModuleNotFoundError: No module named 'BaseHandler'</code> error.</p>
  
  <p><strong>Is</strong> there a way to patch/mock a method call in a remote class, allowing me to set the <code>current_user</code> &amp; <code>auth_state</code> dictionaries for each call?</p>
  
  <p>(ultimately, I will also need to test for different users, and different auth_states)</p>