def setUp(self): self.db_fd, self.db_path = tempfile.mkstemp() self.app = create_app({ 'TESTING': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite:///{}'.format(self.db_path) }) self.client = self.app.test_client() self.app_context = self.app.app_context() self.app_context.push() InitDB().run()
def app(): print("\nCalling app fixture...\n") app = create_app(testing=True) with app.app_context(): db.drop_all() db.create_all() db.session.add_all((User(username='******', password='******'), User(username='******', password='******'), Post(title='test title', body='test\nbody', author_id=1, created=datetime.date(2021, 2, 28)))) db.session.commit() yield app
import time from flask_blog.models import Post, User, Account from flask_blog import create_app, bcrypt, db app = create_app() app.app_context().push() posts = [ 'C++', 'C#', 'C', 'Python', 'Java', 'go', 'ruby', 'shell', 'JavaScript', 'html5', 'css5', 'php', 'R', 'MATLAB', 'Perl', 'Objective-C', 'VB', 'sql', 'Swift', 'Lisp', 'Pascal', 'Ruby', 'SAS', 'Erlang', 'OpenCL' ] users = [{ 'username': '******', 'email': '*****@*****.**', 'password': '******' }, { 'username': '******', 'email': '*****@*****.**', 'password': '******' }, { 'username': '******', 'email': '*****@*****.**', 'password': '******' }] def populte(): print('drop database') db.drop_all() print('drop done') print('create database')
# when working with packages, this will import from the the __init__.py file within this package(flask_blog). The 'app' variable has to exist within __init__.py file from flask_blog import create_app app = create_app() # we don't pass anaything because we're using 'Config' as the default input if __name__=='__main__': app.run(host='0.0.0.0')
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def test_config(): assert not create_app().testing assert create_app(testing=True).testing
# if os.environ.get('FLASK_COVERAGE'): # 存在环境变量'FLASK_COVERAGE'则执行 # import coverage # COV = coverage.coverage(branch=True, include='flask_blog/*') # branch=True选项开启分支覆盖分析,检查每个条件语句的 True 分支和 False 分支是否都执测试行了 # include 选项用来限制程序包中文件的分析范围,不指定会包含虚拟环境等其他一下杂项的检查 # COV.start() from flask_script import Manager, Shell # 命令行选项 from flask_migrate import Migrate, MigrateCommand # from flask_blog import create_app, db from flask_blog.models import User,Article, Role, Permission, \ Follow, Comment, Tag, Category from flask_blog.auth.forms import RegistrationForm app = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(app) migrate = Migrate(app, db) # 做些配置,让 Flask-Script 的 shell 命令自动导入特定的对象 # make_shell_context() 函数注册了程序、数据库实例以及模型,因此这些对象能直接导入 shell def make_shell_context(): return dict(app=app, db=db, User=User, Article=Article, Role=Role, Tag=Tag, Permission=Permission, Follow=Follow,
from flask_blog import create_app app = create_app() # could pass a config here if __name__ == '__main__': app.run( debug=True ) # This allows us to refresh directly the site without shuting it down