Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Test CLI Runner API

Flask Test CLI Runner API

FlaskCliRunner is based on Click's CliRunner and is used to test Flask CLI commands.

Create an instance via app.test_cli_runner().


Creation method

Method Description
app.test_cli_runner(**kwargs) Create a CLI test runner. kwargs are passed to the CliRunner constructor

Core Methods

Method Description
invoke(cli=None, args=None, **kwargs) Invoke a CLI command. Uses app.cli by default. Returns a click.testing.Result object

Result object attributes

property Description
exit_code The command's exit code. 0 indicates success
output The command's standard output content (str)
exception If the command raises an exception, the exception object is contained here

Code Example

Example

from

app

import

create_app

app

=

create_app

(

)

runner

=

app.

test_cli_runner

(

)

Test built-in commands

result

=

runner.

invoke

(

args

=

[

"routes"

]

)

assert

result.

exit_code

==

0

assert

"/api/posts"

in

result.

output

Test custom commands

result

=

runner.

invoke

(

args

=

[

"init-db"

]

)

assert

result.

exit_code

==

0

Test commands with arguments

result

=

runner.

invoke

(

args

=

[

"greet"

,

"--name"

,

"RUNOOB"

]

)

assert

"RUNOOB"

in

result.

output

Test the application CLI group directly with invoke

result

=

runner.

invoke

(

app.

cli

,

[

"run"

,

"--help"

]

)

assert

result.

exit_code

==

0