Python - 从命令行函数单击命令行单元测试用例以获取输入提示

时间:2016-07-01 10:42:14

标签: python unit-testing click

我正在使用click库在python中编写命令行应用程序的单元测试用例。

我尝试了以下示例,这很好用:

def test_hello_world():
    @click.command()
    @click.argument('name')
    def hello(name):
        click.echo('Hello %s!' % name)

    runner = CliRunner()
    result = runner.invoke(hello, ['Yash'])
    assert result.exit_code == 0
    assert result.output == 'Hello Yash!\n'

但现在我想从我的功能中输入提示。

像这样:

def test_name_prompt(self):
    @click.command()
    @click.option('-name', default=False)
    def username():
        fname = click.prompt("What's your first name?")
        lname = click.prompt("what's your last name?")
        click.echo("%s %s" % (fname, lname))

    runner = CliRunner()
    result = runner.invoke(username, ['-name'])
    assert result.exit_code == 0      
    assert result.output == 'Yash Lodha'

1 个答案:

答案 0 :(得分:0)

点击显示“输入”'用于此目的的参数(http://click.pocoo.org/5/testing/),可用于向stdin提供输入数据以满足提示。

a={1,2,3,4,5,5,5,4,3,2}

  {1,2,3,4,5,5,5,4,3,2}

uniques = DeleteDuplicates[a]

  {1,2,3,4,5}