title: Flask MongoDB error: "working outside of application context"
body: <p>I've been trying to test my Flask application that uses PyMongo. The application works fine, but when I execute unit tests, I constantly get an error message saying "working outside of application context". This message is thrown every time I run any unit test that requires access to the Mongo database.</p>
  
  <p>I've been following this guide for unit testing:
  <a href="http://flask.pocoo.org/docs/testing/" rel="nofollow">http://flask.pocoo.org/docs/testing/</a></p>
  
  <p>My application's design is straight forward, similar to the standard Flask tutorial.</p>
  
  <p>Did anyone already have the same issue?</p>
  
  <pre><code>class BonjourlaVilleTestCase(unittest.TestCase):
      container = {}
      def register(self, nickname, password, email, agerange):
          """Helper function to register a user"""
          return self.app.post('/doregister', data={
              'nickname' :    nickname,
              'agerange' :    agerange,
              'password':     password,
              'email':        email
          }, follow_redirects=True)
  
  
      def setUp(self):        
          app.config.from_envvar('app_settings_unittests', silent=True)
  
          for x in app.config.iterkeys():
              print "Conf Key=%s, Value=%s" % (x, app.config[x])
  
  
          self.app = app.test_client()
  
          self.container["mongo"] = PyMongo(app)
          self.container["mailer"] = Mailer(app)
          self.container["mongo"].safe = True
  
          app.container = self.container
  
      def tearDown(self):
          self.container["mongo"].db.drop()
          pass    
  
      def test_register(self):
          nickname = "test_nick"
          password = "test_password"
          email    = "test@email.com"
          agerange = "1"
          rv = self.register(nickname, password, email, agerange)
  
          assert "feed" in rv.data
  
  
  if __name__ == '__main__':    
      unittest.main()
  </code></pre>