2015-11-13 64 views
1

我正在尝试使用unittest测试我的烧瓶应用程序。我想避免进行烧瓶测试,因为我不喜欢超越自己。如何使用unittest测试烧瓶应用程序?

我真的一直在努力与这个单元测试的东西。这是令人困惑的,因为有请求上下文和应用程序上下文,我不知道在调用db.create_all()时需要使用哪一个。

好像当初我添加到数据库中,它增加了我的模型,在我的应用程序模块(初始化的.py)文件中指定的数据库,而不是在设置(个体经营)方法中指定的数据库。

我有一些方法必须在每个test_方法之前填充数据库。

我该如何将我的db指向正确的路径?

def setUp(self): 
    #self.db_gd, app.config['DATABASE'] = tempfile.mkstemp() 
    app.config['TESTING'] = True 
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE'] 
    basedir = os.path.abspath(os.path.dirname(__file__)) 
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \ 
     os.path.join(basedir, 'test.db') 
    db = SQLAlchemy(app) 
    db.create_all() 
    #self.app = app.test_client() 
    #self.app.testing = True 
    self.create_roles() 
    self.create_users() 
    self.create_buildings() 
    #with app.app_context(): 
    # db.create_all() 
    # self.create_roles() 
    # self.create_users() 
    # self.create_buildings() 

def tearDown(self): 
    #with app.app_context(): 
    #with app.request_context(): 
    db.session.remove() 
    db.drop_all() 
    #os.close(self.db_gd) 
    #os.unlink(app.config['DATABASE']) 

这里的方法之一是填充我的数据库:

def create_users(self): 
    #raise ValueError(User.query.all()) 
    new_user = User('Some User Name','[email protected]','admin') 
    new_user.role_id = 1 
    new_user.status = 1 
    new_user.password = generate_password_hash(new_user.password) 
    db.session.add(new_user) 

地方我已经看了:

http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling

,烧瓶文档: http://flask.pocoo.org/docs/0.10/testing/

回答

0

你打的一个问题就是烧瓶上下文的局限性,这是我在将烧瓶扩展插入到我的项目之前认真考虑的主要原因,而flask-sqlalchemy是最大的犯罪分子之一。我这样说是因为在大多数情况下,在处理数据库时完全没有必要依赖flask应用程序上下文。当然它可能很好,特别是因为flask-sqlalchemy为你做了很多幕后工作,主要是你不必手动管理你的会话,元数据或引擎,但记住这些事情可以轻易地自行完成,并且为此,您可以无限制地访问数据库,无需担心烧瓶环境。这里是如何设置你的分贝手动,第i个将显示烧瓶-SQLAlchemy的方式,则手动平原SQLAlchemy的方式的例子:

  1. flask-sqlalchemy方式:

    import flask 
    from flask_sqlalchemy import SQLAlchemy 
    
    app = flask.Flask(__name__) 
    db = SQLAlchemy(app) 
    
    # define your models using db.Model as base class 
    # and define columns using classes inside of db 
    # ie: db.Column(db.String(255),nullable=False) 
    # then create database 
    db.create_all() # <-- gives error if not currently running flask app 
    
  2. 标准SQLAlchemy的方式:

    import flask 
    import sqlalchemy as sa 
    from sqlalchemy.ext.declarative import declarative_base 
    
    # first we need our database engine for the connection 
    engine = sa.create_engine(MY_DB_URL,echo=True) 
    # the line above is part of the benefit of using flask-sqlalchemy, 
    # it passes your database uri to this function using the config value   
    # SQLALCHEMY_DATABASE_URI, but that config value is one reason we are 
    # tied to the application context 
    
    # now we need our session to create querys with 
    Session = sa.orm.scoped_session(sa.orm.sessionmaker()) 
    Session.configure(bind=engine) 
    session = Session() 
    
    # now we need a base class for our models to inherit from 
    Model = declarative_base() 
    # and we need to tie the engine to our base class 
    Model.metadata.bind = engine 
    
    # now define your models using Model as base class and 
    # anything that would have come from db, ie: db.Column 
    # will be in sa, ie: sa.Column 
    
    # then when your ready, to create your db just call 
    Model.metadata.create_all() 
    # no flask context management needed now 
    

,如果你设置你的应用程序像,在任何情况下ISSU你应该离开。