I have a script that uses click to get input arguments. According to their documentation CliRunner can be used to make unit testing:
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'
This is done for a tiny hello-world function that is written in-line in the test. My queation is:
How do I perform the same test for a script in a different file??
Example of script that uses click:
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()
(from click documentation)
EDIT:
If I try to run it as suggested in Dan's answer, after a couple of hours it shows this error:
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)