'Test click.prompt in an interactive session

I wonder how I can test consecutive click.prompt.

Questions regarding testing input prompts of python click seem to focus on one prompt only. One example focusses on one prompt given via command line.

Here github mwe I try to test consecutive prompts.

import click

@click.group()
@click.option('--option/--no-option', default=False)
def cli(option):
    click.echo(click.prompt('Type something'))
    # Todo later: click.echo(click.prompt('Type something else'))
    click.echo('Option is %s' % ('on' if option else 'off'))

@cli.command()
def function():
    click.echo('Functioning')

Something like

import click
from click.testing import CliRunner

import cli

def test_sync():
    runner = CliRunner()
    result = runner.invoke(cli.cli, ['--debug', 'sync'], input='test')
    assert result.exit_code == 0
    assert 'Option is on' in result.output
    assert 'test' in result.output

however fails:

$ python -m pytest test_clickprompt.py   

    def test_sync():
        runner = CliRunner()
        result = runner.invoke(cli.cli, ['--debug', 'sync'], input='test\\n')
>       assert result.exit_code == 0
E       assert 2 == 0
E        +  where 2 = <Result SystemExit(2)>.exit_code

test_clickprompt.py:9: AssertionError
======================================================= short test summary info =======================================================
FAILED test_clickprompt.py::test_sync - assert 2 == 0                                


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source