2016-07-01 2 views
2

私は、clickライブラリを使用してPythonでコマンドラインアプリケーション用のユニットテストケースを作成しています。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' 

答えて

0

クリックしてプロンプトを満たすために標準入力に入力データを提供するために使用することができ、この目的のための「入力」パラメータ(http://click.pocoo.org/5/testing/)を公開します。このような

import click 
from click.testing import CliRunner 

def test_prompts(): 
    @click.command() 
    @click.option('--foo', prompt=True) 

    def test(foo): 
     click.echo('foo=%s' % foo) 

     runner = CliRunner() 
     result = runner.invoke(test, input='wau wau\n') 
     assert not result.exception 
     assert result.output == 'Foo: wau wau\nfoo=wau wau\n' 
関連する問題