1
我有一个脚本,使用点击获取输入参数。根据他们的documentation CliRunner可以用来做单元测试:如何使用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'
这是为写入在线测试中的微小你好世界函数来完成。 我的伫立是:
如何对不同文件中的脚本执行相同的测试?脚本的
示例使用点击:
(从 click documentation)编辑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()
:
如果我尝试运行它在丹的回答表明,后一对夫妇小时显示此错误:
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)
请参阅编辑 –