title: in a Flask unit-test, how can I mock objects on the request-global `g` object?
body: <p>I have a flask application that is setting up a database connection in a <code>before_filter</code>, very similar to <a href="http://flask.pocoo.org/docs/tutorial/dbcon/" rel="noreferrer">this</a>:</p>
  
  <pre><code>@app.before_request
  def before_request():
      g.db = connect_db()
  </code></pre>
  
  <p>Now: I am writing some unit-tests and I do <em>not</em> want them to hit the database. I want to replace <code>g.db</code> with a mock object that I can set expectations on.</p>
  
  <p>My tests are using <code>app.test_client()</code>, as is demonstrated in the flask documentation <a href="http://flask.pocoo.org/docs/testing/" rel="noreferrer">here</a>. An example test looks something like</p>
  
  <pre><code>def test(self):
      response = app.test_client().post('/endpoint', data={..})
      self.assertEqual(response.status_code, 200)
      ...
  </code></pre>
  
  <p>The tests work and pass, but they are hitting the database and as I said I want to replace db access with mock objects. I do not see any way in <code>test_client</code> to access the <code>g</code> object or alter the before_filters.</p>