我正在尝试向FlaskGroup添加一些FlaskCLI例程。这是我的myapi/manage.py
文件的外观:
# -*- coding: utf-8 -*-
import click
from flask import current_app as app
from flask.cli import FlaskGroup
@click.group(cls=FlaskGroup)
def cli():
"""Management interface for myproject"""
pass
@cli.command("init")
def init():
"""Run app initialization routines"""
click.echo(f"{app.name} initialized for {app.config['ENV']} environment")
入口点在setup.py
中注册如下(为了简单起见,删除了其他KWARG):
setup(
entry_points={
"console_scripts": [
"myapi = myapi.manage:cli",
],
},
)
我更喜欢console_scripts
而不是flask.commands
,因为我在这个项目中使用了应用程序工厂方法,这更方便。当我运行该命令时,我得到了预期的结果:
(venv) [zobayer@hyperion myproject]$ myapi init
myproject initialized for development environment
但是,我对init
命令的测试失败,出现异常消息,并且实际的命令没有通过runner
调用
这里有一个测试:
def test_myapi_init(runner):
"""Test fails if `myapi init` command fails to execute"""
result = runner.invoke(args=["myapi", "init"])
print(result)
测试输出:
tests/test_cli.py <Result AttributeError("'AppGroup' object has no attribute 'cli'")>
作为参考,runner
是我在conftest.py
文件中定义的pytest装置:
# -*- coding: utf-8 -*-
import pytest
from myapi.app import create_app
@pytest.fixture(scope="module")
def app():
"""A flask app with testing configurations"""
return create_app("testing")
@pytest.fixture(scope="module")
def runner(app):
"""A CLI test client to test shell commands"""
return app.test_cli_runner()
我已经查找了有关如何为自定义脚本和FlaskGroup cli组编写测试的资源,但到目前为止还没有找到任何有希望的内容。为cli模块编写测试的正确方法是什么
目前没有回答
相关问题 更多 >
编程相关推荐