在交互式会话中测试 click.prompt
Test click.prompt in an interactive session
我想知道如何连续测试 click.prompt
。
关于测试 python 点击输入提示的问题似乎集中在 . One example 集中在通过命令行给出的一个提示上。
这里github mwe我尝试测试连续提示
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')
类似
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
然而失败了:
$ 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
正如MRBean Bremen所指出的,输入语句必须在列表之外。之后最后两个断言需要更改以进行绿色测试。
import click
from click.testing import CliRunner
import cli
def test_sync():
runner = CliRunner()
result = runner.invoke(cli.cli, ['--option', 'function'], input='test\ntest')
assert result.exit_code == 0
assert "test\ntest" in result.output
assert "debug" not in result.output
我想知道如何连续测试 click.prompt
。
关于测试 python 点击输入提示的问题似乎集中在
这里github mwe我尝试测试连续提示
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')
类似
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
然而失败了:
$ 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
正如MRBean Bremen所指出的,输入语句必须在列表之外。之后最后两个断言需要更改以进行绿色测试。
import click
from click.testing import CliRunner
import cli
def test_sync():
runner = CliRunner()
result = runner.invoke(cli.cli, ['--option', 'function'], input='test\ntest')
assert result.exit_code == 0
assert "test\ntest" in result.output
assert "debug" not in result.output