如何使用CliRunner测试脚本?

时间:2017-07-21 10:14:58

标签: python python-unittest python-click

我有一个使用click获取输入参数的脚本。根据他们的documentation CliRunner可用于进行单元测试:

import click
from click.testing import CliRunner

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

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

这是针对在测试中在线编写的微小hello-world函数完成的。 我的任务是:

如何对不同文件中的脚本执行相同的测试?

使用点击的脚本示例:

import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()

(来自click documentation

修改

如果我尝试按照Dan的回答中的说法运行它,几个小时后会显示此错误:

test_hello_world (__main__.TestRasterCalc) ... ERROR

======================================================================
ERROR: test_hello_world (__main__.TestRasterCalc)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/src/HelloClickUnitTest.py", line 35, in test_hello_world
    result = runner.invoke(hello, ['Peter'])
  File "/usr/local/lib/python2.7/dist-packages/click/testing.py", line 299, in invoke
    output = out.getvalue()
MemoryError

----------------------------------------------------------------------
Ran 1 test in 9385.931s

FAILED (errors=1)

3 个答案:

答案 0 :(得分:0)

在您的测试文件中执行类似的操作。

import click
from click.testing import CliRunner
from hello_module import hello  # Import the function to test

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

答案 1 :(得分:0)

您的测试有几个挑战。

  1. 您的计划希望通过name--name指定为选项。

    要修复测试,请将['--name', 'Peter']传递给invoke()

  2. 此外,如果未指定该选项,则会提示您。 MemoryError是由于点击不断尝试提示不存在的用户。

    要修复测试,请通过input='Peter\n'进行调用。这将表现为用户在提示符下键入:Peter

  3. 代码:

    import click
    from click.testing import CliRunner
    
    
    @click.command()
    @click.option('--count', default=1, help='Number of greetings.')
    @click.option('--name', prompt='Your name', help='The person to greet.')
    def hello(count, name):
        """Simple program that greets NAME for a total of COUNT times."""
        for x in range(count):
            click.echo('Hello %s!' % name)
    
    
    def test_hello_world():
    
        runner = CliRunner()
        result = runner.invoke(hello, ['--name', 'Peter'])
        assert result.exit_code == 0
        assert result.output == 'Hello Peter!\n'
    
        result = runner.invoke(hello, [], input='Peter\n')
        assert result.exit_code == 0
        assert result.output == 'Your name: Peter\nHello Peter!\n'
    
    test_hello_world()
    

答案 2 :(得分:0)

我为此奋斗了很久。原来答案比您想像的要简单。这一切都在Runner.invoke()函数中。就您而言:

runner.invoke(hello, '--count 3 --name Peter')