Added runner fixture

This commit is contained in:
Kiss György 2016-06-25 13:24:43 +02:00
parent b8e826c16b
commit 1b764cff93

View file

@ -25,6 +25,11 @@ from flask.cli import cli, AppGroup, FlaskGroup, NoAppException, ScriptInfo, \
find_default_import_path find_default_import_path
@pytest.fixture
def runner():
return CliRunner()
def test_cli_name(test_apps): def test_cli_name(test_apps):
"""Make sure the CLI object's name is the app's name and not the app itself""" """Make sure the CLI object's name is the app's name and not the app itself"""
from cliapp.app import testapp from cliapp.app import testapp
@ -108,7 +113,7 @@ def test_scriptinfo(test_apps):
assert obj.load_app() == app assert obj.load_app() == app
def test_with_appcontext(): def test_with_appcontext(runner):
"""Test of with_appcontext.""" """Test of with_appcontext."""
@click.command() @click.command()
@with_appcontext @with_appcontext
@ -117,13 +122,12 @@ def test_with_appcontext():
obj = ScriptInfo(create_app=lambda info: Flask("testapp")) obj = ScriptInfo(create_app=lambda info: Flask("testapp"))
runner = CliRunner()
result = runner.invoke(testcmd, obj=obj) result = runner.invoke(testcmd, obj=obj)
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == 'testapp\n' assert result.output == 'testapp\n'
def test_appgroup(): def test_appgroup(runner):
"""Test of with_appcontext.""" """Test of with_appcontext."""
@click.group(cls=AppGroup) @click.group(cls=AppGroup)
def cli(): def cli():
@ -143,7 +147,6 @@ def test_appgroup():
obj = ScriptInfo(create_app=lambda info: Flask("testappgroup")) obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))
runner = CliRunner()
result = runner.invoke(cli, ['test'], obj=obj) result = runner.invoke(cli, ['test'], obj=obj)
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == 'testappgroup\n' assert result.output == 'testappgroup\n'
@ -153,7 +156,7 @@ def test_appgroup():
assert result.output == 'testappgroup\n' assert result.output == 'testappgroup\n'
def test_flaskgroup(): def test_flaskgroup(runner):
"""Test FlaskGroup.""" """Test FlaskGroup."""
def create_app(info): def create_app(info):
return Flask("flaskgroup") return Flask("flaskgroup")
@ -166,16 +169,14 @@ def test_flaskgroup():
def test(): def test():
click.echo(current_app.name) click.echo(current_app.name)
runner = CliRunner()
result = runner.invoke(cli, ['test']) result = runner.invoke(cli, ['test'])
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == 'flaskgroup\n' assert result.output == 'flaskgroup\n'
class TestRoutes: class TestRoutes:
def test_no_route(self, monkeypatch): def test_no_route(self, runner, monkeypatch):
monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:noroute_app') monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:noroute_app')
runner = CliRunner()
result = runner.invoke(cli, ['routes'], catch_exceptions=False) result = runner.invoke(cli, ['routes'], catch_exceptions=False)
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == """\ assert result.output == """\
@ -184,9 +185,8 @@ Route Endpoint Methods
/static/<path:filename> static HEAD, OPTIONS, GET /static/<path:filename> static HEAD, OPTIONS, GET
""" """
def test_simple_route(self, monkeypatch): def test_simple_route(self, runner, monkeypatch):
monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:simpleroute_app') monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:simpleroute_app')
runner = CliRunner()
result = runner.invoke(cli, ['routes'], catch_exceptions=False) result = runner.invoke(cli, ['routes'], catch_exceptions=False)
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == """\ assert result.output == """\
@ -196,9 +196,8 @@ Route Endpoint Methods
/static/<path:filename> static HEAD, OPTIONS, GET /static/<path:filename> static HEAD, OPTIONS, GET
""" """
def test_only_POST_route(self, monkeypatch): def test_only_POST_route(self, runner, monkeypatch):
monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:only_POST_route_app') monkeypatch.setitem(os.environ, 'FLASK_APP', 'cliapp.routesapp:only_POST_route_app')
runner = CliRunner()
result = runner.invoke(cli, ['routes'], catch_exceptions=False) result = runner.invoke(cli, ['routes'], catch_exceptions=False)
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == """\ assert result.output == """\