我为我的flask应用程序使用了一个定制的manage.py脚本,它是用工厂模式创建的。
此脚本需要能够运行服务器和运行我的测试。我使用外部工具来运行我的测试,因此我不需要创建应用程序来运行测试。如何在运行某些命令时才能创建应用程序?
我的脚本当前如下所示:
import subprocess
import sys
from flask import current_app
from flask.cli import FlaskGroup
import click
from quizApp import create_app
@click.pass_context
def get_app(ctx, _):
"""Create an app with the correct config.
"""
return create_app(ctx.find_root().params["config"])
@click.option("-c", "--config", default="development",
help="Name of config to use for the app")
@click.group(cls=FlaskGroup, create_app=get_app)
def cli(**_):
"""Define the top level group.
"""
pass
@cli.command("test")
def test():
"""Set the app config to testing and run pytest, passing along command
line args.
"""
# This looks stupid, but see
# https://github.com/pytest-dev/pytest/issues/1357
sys.exit(subprocess.call(['py.test',
'--cov=quizApp',
'--flake8',
'--pylint',
'./']))
if __name__ == '__main__':
cli()我尝试创建第二个组,但似乎一次只有一个组可以“运行”,所以我不太确定如何解决这个问题。
发布于 2017-07-01 11:55:50
对其他命令使用with_appcontext参数:
@click.group(cls=FlaskGroup, create_app=get_app)
def cli(**_):
pass
@cli.command(with_appcontext=false)
def extra():
pass发布于 2016-09-30 15:52:51
您需要添加:
cli.add_command(test)之前:
if __name__ == '__main__':
cli()我不确定这是不是你唯一需要做的事情。我也在研究这个,觉得很困惑。但是,如果您想在FlaskGroup中添加命令,则必须执行此操作。当我想在flask项目中使用Factory和CLI时,我感到困惑。也许最后你需要尝试一下Flask-Script扩展,至少它是有效的。
https://stackoverflow.com/questions/38927996
复制相似问题