title: Flask click command unittests - how to use testing app with "with_appcontext" decorator?
body: <p>I don't know how to correctly use testing app version while unittesting (with pytest) flask cli command (with click) decorated with <code>with_app_context</code> decorator. This decorator replaces pytest fixture app with the "normal", development application. I use app factory pattern.</p>
  
  <p>My command in a simplified version looks like this (mind <code>@with_appcontext</code>):</p>
  
  <pre><code>@click.command()
  @click.option('--username', prompt='Username')
  @click.option('--email', prompt='User E-Mail')
  @click.option('--password', prompt='Password', confirmation_prompt=True, hide_input=True)
  @with_appcontext  # from flask.cli import with_appcontext
  def createsuperuser(username, email, password):
      user = User(
          username=username,
          email=email,
          password=password,
          active=True,
          is_admin=True,
      )
      user.save()
  </code></pre>
  
  <p>Without <code>@with_appcontext</code> unittests work just fine (they get the app injected by pytest), but the command itself does not, as it needs an app context.</p>
  
  <p>My extracted pytest code:</p>
  
  <pre><code># pytest fixtures
  @pytest.yield_fixture(scope='function')
  def app():
      """An application for the tests."""
      _app = create_app(TestConfig)
      ctx = _app.test_request_context()
      ctx.push()
  
      yield _app
  
      ctx.pop()
  
  
  @pytest.yield_fixture(scope='function')
  def db(app):
      """A database for the tests."""
      _db.app = app
      with app.app_context():
          _db.create_all()
  
      yield _db
  
      # Explicitly close DB connection
      _db.session.close()
      _db.drop_all()
  
  
  @pytest.mark.usefixtures('db')
  class TestCreateSuperUser:
      # db fixture uses app fixture, works the same if app was injected here as well
      def test_user_is_created(self, cli_runner, db):
          result = cli_runner.invoke(
              createsuperuser,
              input='johnytheuser\nemail@email.com\nsecretpass\nsecretpass'
          )
          assert result.exit_code == 0
          assert 'SUCCESS' in result.output
          # etc.
  </code></pre>
  
  <p>All my tests using <code>app</code> and <code>db</code> fixtures work just fine apart from these decorated ones. I'm not sure how I should workaround this <code>with_appcontext</code> decorator that sets the app itself.</p>
  
  <p>Thank you in advance for any hint.</p>