我正在尝试使用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/