2017-08-24 24 views
0

我的申请有效。只有doctests不起作用。如何用flask-sqlalchemy编写doctests?

在应用程序中,我使用一个模式

from models import db 

app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://..." 

db.init_app(app) 

... 

if __name__ == '__main__': 
    app.run(debug=True, host='0.0.0.0', threaded=True) 

我有几个非常简单的文档测试的。从瓶+ SQLAlchemy的切换到烧瓶的SQLAlchemy之前,它的工作:

>>> import database 
>>> files_insert(..., no_commit=True) # database stuff happens here 
>>> database.session.rollback() 

它可以移除插件测试(他们可能没有什么太大的意义无论如何),但我有一对夫妇的功能,这只是需要从数据库中选择元素。这应该肯定仍然有效。但我得到:

RuntimeError: application not registered on db instance and no applicationbound to current context 

是否有配置我的应用程序,以便文档字符串总是得到应用上下文的方法吗?

回答

0

您doctest缺少应用程序上下文,并且自使用flask-sqlalchemy时,您已绑定应用程序实例和数据库实例。请检查this issue

解决方法(真正的破解)是修补烧瓶会话,请求和current_app对象,查看this comment on the same issue,它为pytest修补它。

但恕我直言,当你开始黑客这种方式,是时候切换到常规测试方法,unittest或pytest。

虽然doctest是简单的代码不错,当你有破解和/或建立一个完整的上下文中运行文档测试,你将失去它的简单反正...