pythonflasktestingpytestflask-cli

Test flask command when using click.confirm("sometext") with abort option


I want to write a test to the command when the user aborts it, but I have a problem with the abort option in click.confirm

when i write full command in console flask questions deleteall i got prompt y/N. When i select N option user cancel command and message are displayed in the console Aborted!.

It looks like below:

Do you want to continue to delete all questions and answers? [y/N]: n

Aborted!

The command code is below:

bp = Blueprint("questions", __name__)
bp.cli.short_help = "Questions utilities"



@bp.cli.command("deleteall")
@with_appcontext
def deleteall():
    """
    Delete all examples questions and answers from database
    """

    click.confirm('Do you want to continue to delete all questions and answers?', abort=True)
   
    """
    code for command when writing yes in click.confirm - y
    """

i have written test below:

# full command: flask questions deleteall
# y - select 'y' to confirm deleting  
def test_cancel_deleteall_command(runner, app_with_db):
    result = runner.invoke(args=['questions', 'deleteall', ['N']])
    assert result.exit_code == 1
    print(result.output)
    assert 'Aborted!' in str(result.output)

I have a problem with a test that keeps failing because the results are empty.

test_cancel_deleteall_command ____________________________________________________________________________________

runner = <flask.testing.FlaskCliRunner object at 0x7fbcf01a5b10>, app_with_db = <async_generator object app_with_db at 0x7fbcf06625c0>

    def test_cancel_deleteall_command(runner, app_with_db):
        # full command: flask question deleteall
        # y - select 'y' to confirm deleting
        result = runner.invoke(args=['questions', 'deleteall', ['N']])
        assert result.exit_code == 1
        print(result.output)
>       assert 'Aborted!' in str(result.output)
E       assert 'Aborted!' in ''
E        +  where '' = str('')
E        +    where '' = <Result TypeError("unhashable type: 'list'")>.output

tests/functional/test_cli_commands.py:28: AssertionError
---------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------

======================================================================================= short test summary info =======================================================================================
FAILED tests/functional/test_cli_commands.py::test_cancel_deleteall_command - assert 'Aborted!' in ''

Does anyone have an idea how to get "Aborted!" command message? I tried with result.output but it doesn't work..


Solution

  • I resolve this problem with following code:

    def test_deleteall_command_when_canceled(runner, app_with_db):
        # full command: flask question deleteall
        # y - select 'y' to confirm deleting
        result = runner.invoke(args=['questions', 'deleteall'], input="n")
        assert result.exit_code == 1
        assert result.exception
        assert 'Aborted!' in result.output
    

    i just needed to add input="n" because i wanted to simulate user to type n

    How i get solve this problem?

    i was check the reference method in the Click library. I was found this line:

    "code"
        except Abort:
                if not standalone_mode:
                    raise
                echo(_("Aborted!"), file=sys.stderr)
                sys.exit(1)
    "code"
    

    In addition, from the documentation on the Click page in version 8.1.3 I read that:

    If an Abort exception is raised print the string Aborted! to standard error and exit the program with exit code 1.

    It turns out that runner.invoke() was badly written.