2016-11-24 150 views
0

我在燒瓶應用程序中看到了一個相當奇怪的行爲:嵌套的app_contexts不能正常工作,因爲它們應該放在我的測試套件中,所以current_app localproxy沒有指向正確的應用程序。它完全同步代碼,所以沒有線程,corroutines或任何東西。燒瓶內的current_app錯誤app_context

¿某人能提供任何指導嗎?

> /.../browsepy/tests/deprecated/test_plugins.py(173)test_register_plugin() 
    171   self.manager = self.manager_module.PluginManager(self.app) 
    172   with self.app.app_context(): 
--> 173    self.manager.load_plugin('player') 
    174   self.assertIn(self.player_module.player, self.app.blueprints.values()) 
    175 

ipdb> self.app 
<Flask 'TestIntegration'> 
ipdb> type(self.app) 
<class 'flask.app.Flask'> 
ipdb> d 
> /.../browsepy/manager.py(151)load_plugin() 
    149   module = super(RegistrablePluginManager, self).load_plugin(plugin) 
    150   if hasattr(module, 'register_plugin'): 
--> 151    module.register_plugin(self) 
    152   return module 
    153 

ipdb> current_app 
<Flask 'browsepy'> 
ipdb> type(current_app) 
<class 'werkzeug.local.LocalProxy'> 

回答

1

問題在別處,pdb與燒瓶current_app不能很好地發揮作用。

編輯

我正在運行測試套件沒有快速失敗的選項,這樣其他測試注射驗屍調試器之前跑了。

無論如何,瓶子的行爲仍然是非常有問題的:在使用app.test_client方法後,它不會清理它們的上下文全局變量,這是我嘗試調試的錯誤以及我發現的調試問題的來源。

這裏是我不得不用清洗瓶中的環境,以保持燒瓶本身從不同的應用程序上的測試混合料的功能:

import flask 


def clear_localstack(stack): 
    ''' 
    Clear given werkzeug LocalStack instance. 

    :param ctx: local stack instance 
    :type ctx: werkzeug.local.LocalStack 
    ''' 
    while stack.pop(): 
     pass 


def clear_flask_context(): 
    ''' 
    Clear flask current_app and request globals. 

    When using :meth:`flask.Flask.test_client`, even as context manager, 
    the flask's globals :attr:`flask.current_app` and :attr:`flask.request` 
    are left dirty, so testing code relying on them will probably fail. 

    This function clean said globals, and should be called after testing 
    with :meth:`flask.Flask.test_client`. 
    ''' 
    clear_localstack(flask._app_ctx_stack) 
    clear_localstack(flask._request_ctx_stack)