title: Generate URLs for Flask test client with url_for function
body: <p>I'm trying to write unit tests for a Flask app using pytest. I have an app factory:</p>
  
  <pre><code>def create_app():
      from flask import Flask
      app = Flask(__name__)
      app.config.from_object('config')
      import os
      app.secret_key = os.urandom(24)
      from models import db
      db.init_app(app)
      return app
  </code></pre>
  
  <p>And a test class:</p>
  
  <pre><code>class TestViews(object):
  
      @classmethod
      def setup_class(cls):
          cls.app = create_app()
          cls.app.testing = True
          cls.client = cls.app.test_client()
  
      @classmethod
      def teardown_class(cls):
          cls.app_context.pop()
  
      def test_create_user(self):
          """
          Tests the creation of a new user.
          """
          view = TestViews.client.get(url_for('create_users')).status_code == 200
  </code></pre>
  
  <p>but when I run my tests I get the following error:</p>
  
  <pre><code>RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
  </code></pre>
  
  <p>Googling this tells me (I think) that using the test client should create an automatic application context. What am I missing?</p>