title: flask testing can't set flask.g by appcontext_pushed.connected_to
body: <p>My code is wrong.. So the question is over.Thanks tmt so much.</p>
  <p>=================== origin question ⬇️ =================</p>
  <p>I try to test my flask app(python3.8.1) follow flask-doc</p>
  <p><a href="https://flask.palletsprojects.com/en/1.1.x/testing/" rel="nofollow noreferrer">flask-doc</a></p>
  <p>I want to set flask.g for my app before request in testing, so I use <strong>appcontext_pushed.connected_to</strong> func, but it not work...</p>
  <p>I build a small demo down here. You can have a try by copy it in demo.py</p>
  <blockquote>
  <p>pip install flask, binker, pytest</p>
  <p>pytest demo.py</p>
  </blockquote>
  <p>You will get a AttributeError: '_AppCtxGlobals' object has no attribute 'name'.......</p>
  <p>Why...</p>
  <pre class="lang-py prettyprint-override"><code>from contextlib import contextmanager
  from flask import appcontext_pushed, g, json, jsonify, Flask
  
  
  app = Flask(__name__)
  
  
  def fetch_current_user_from_database():
      return {'name': 'aa'}
  
  
  def get_user():
      user = getattr(g, 'user', None)
      if user is None:
          user = fetch_current_user_from_database()
          g.name = user['name']
      return user
  
  
  @app.route('/users/me')
  def users_me():
      return jsonify(username=g.name)
  
  
  @contextmanager
  def user_set(app, user):
      def handler(sender, **kwargs):
          g.user = user
      with appcontext_pushed.connected_to(handler, app):
          yield
  
  
  def test_1():
      username = '123'
      with user_set(app, username):
          with app.test_client() as c:
              resp = c.get('/users/me')
              data = json.loads(resp.data)
              assert data['username'] == username
  
  </code></pre>