我有一个名为 download 的单击命令,它会在下载一系列文件之前提示用户输入用户名和密码:

$ python download.py
Username: jkarimi91
Password: 1234
Download complete!

要测试此命令,我需要能够分别将 usernamepassword 传递给 stdinCliRunner.invoke() 方法有一个 input 参数,但它不接受列表。是否可以将多个输入传递给 CliRunner.invoke()

最佳答案

您可以通过传递由换行符( \n )连接的字符串来传递多个输入:

import click
from click.testing import CliRunner


def test_prompts():
    @click.command()
    @click.option('--username', prompt=True)
    @click.option('--password', prompt=True)
    def test(username, password):
        # download ..
        click.echo('Download complete!')

    # OR
    #
    # @click.command()
    # def test():
    #     username = click.prompt('Username')
    #     password = click.prompt('Password', hide_input=True)
    #     # download ..
    #     click.echo('Download complete!')


    runner = CliRunner()
    result = runner.invoke(test, input='username\npassword\n') # <---
    assert not result.exception
    assert result.output.endswith('Download complete!\n')


if __name__ == '__main__':
    test_prompts()

关于python - 点击 : Is it possible to pass multiple inputs to CliRunner. 调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44350389/

10-12 17:01