Skip to main content
 首页 » 编程设计

python之如何使用 unittest 测试我的 flask 应用程序

2025年05月04日12kevingrace

我正在尝试使用 unittest 测试我的 Flask 应用程序。我不想进行 flask 测试,因为我不想超越自己。

我现在真的一直在为这个单元测试而苦苦挣扎。这很令人困惑,因为存在请求上下文和应用程序上下文,我不知道在调用 db.create_all() 时我需要在哪一个中。

似乎当我添加到数据库时,它会将我的模型添加到我的应用程序模块 (init.py) 文件中指定的数据库,而不是 setUp(self ) 方法。

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

如何将我的数据库指向正确的路径?

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','xxxxx@gmail.com','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

还有 flask 文档: http://flask.pocoo.org/docs/0.10/testing/

请您参考如下方法:

您遇到的一个问题是 flask 上下文的局限性,这是我在将 flask 扩展纳入我的项目之前长期而努力思考的主要原因,而 flask-sqlalchemy 是最大的违规者之一。我这样说是因为在大多数情况下,在处理数据库时完全没有必要依赖 Flask 应用程序上下文。当然这很好,特别是因为 flask-sqlalchemy 在幕后为你做了很多,主要是你不必手动管理你的 session 、元数据或引擎,但请记住,这些事情可以很容易地自己完成,并且为此,您可以获得不受限制地访问数据库的好处,而不必担心 flask 上下文。这是一个如何手动设置数据库的示例,首先我将展示 flask-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 
    

如果您像这样设置您的应用程序,那么您遇到的任何上下文问题都应该消失。