def setUp(self): app = create_app('app.config.TestConfig') self.client = app.test_client() # 不在应用目录中运行会造成flask-sqlalchemy不能正确初始化,需要重新赋予应用实例 db.app = app db.create_all()
def app(tmp_path, mock_files_repo, mock_disk_storage_repo) -> Flask: temp_uploads_folder = tmp_path # Copy files in test uploads folder to temp directory filesToCopy = os.listdir(UPLOAD_FOLDER) for f in filesToCopy: with open(os.path.join(UPLOAD_FOLDER, f), "rb") as src: dest_file = temp_uploads_folder / f copyfile(src, dest_file) app = create_app( { "TESTING": True, "UPLOAD_FOLDER": temp_uploads_folder, } ) yield app
def app(tmp_path): temp_uploads_folder = tmp_path # Copy files in test uploads folder to temp directory filesToCopy = os.listdir(UPLOAD_FOLDER) for f in filesToCopy: with open(os.path.join(UPLOAD_FOLDER, f), "rb") as src: dest_file = temp_uploads_folder / f copyfile(src, dest_file) app_config = {"UPLOAD_FOLDER": temp_uploads_folder} app = create_app(app_config) # Setup with app.app_context(): pass # Provide resource yield app # Teardown pass
import os from web_server import create_app from celery import Celery from web_server.tasks import test, check_station def make_celery(app): celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'], backend=app.config['CELERY_BACKEND_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery env = os.environ.get('WEBAPP_ENV', 'dev') flask_app = create_app('webapp.config.%sConfig'.format(env.capitalize())) celery = make_celery(flask_app)
# coding=u8 from flask_script import Manager, Server from flask_migrate import Migrate, MigrateCommand from web_server import create_app from web_server.ext import db app = create_app() migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) manager.add_command( "server", Server(use_debugger=True, use_reloader=True, host='0.0.0.0', port=11000)) manager.add_command( "prod", Server(use_debugger=False, use_reloader=False, host='127.0.0.1', port=11000)) @manager.shell def make_shell_context(): return dict(app=app) if __name__ == "__main__": manager.run()
import os from flask_script import Manager, Server from flask_migrate import Migrate, MigrateCommand from web_server import create_app from web_server.models import * from web_server.ext import db, socketio env = os.environ.get('WEBAPP_ENV', 'dev') app = create_app('web_server.config.{}Config'.format(env.capitalize())) migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) manager.add_command( "server", Server(use_debugger=True, use_reloader=True, host='0.0.0.0', port=11000)) manager.add_command( "prod", Server(use_debugger=False, use_reloader=False, host='127.0.0.1', port=11000)) @manager.shell def make_shell_context(): return dict(app=app)
def test_config(): assert not create_app().testing assert create_app({"TESTING": True}).testing