title: Properly Unit test around a Flask App and Mocking Flask.request.json?
body: <p>I'm attempting to write unit tests for a file that is included inside of a flask app (microservice to handle some RESTful endpoints). Some things I'm running into: All the documentation and questions I seem to find involve invoking the APIs themselves, this isn't proper I need to invoke the post function directly with a mocked fake request that I've setup as I need to test functionality.</p>
  
  <p>I'm able to attempt to mock the request.json object however i'm always met with "RuntimeError: Working outside of request context".I've attempted to use test_request_context() but that leads to the same issue. Then I started diving deeper into Flask and attempting to us app.test_client() however this has it's own problems alongside of calling the endpoint directly and doesn't let me unit test my function properly, they start moving into the realm of being integration tests.  </p>
  
  <p>This is the function i'm attempting to test:</p>
  
  <pre><code>@api.route...
  class HandleRoute(Resource):
      @authentication #&lt;-- I want to Mock True or False response
      def post(self): #&lt;-- I want to call directly 
            try:
                if not request.json #&lt;-- Value I want to mock, from flask.request
  </code></pre>
  
  <p>How should I be going about this? I'm trying to avoid app.test_client() and other Flask related things as again, the point of these unit tests is to check my code paths as a sanity check, not just what should happen. </p>