Created
September 13, 2016 11:52
-
-
Save lbillingham/6412cdc19f9a44901b03841e0443fbf7 to your computer and use it in GitHub Desktop.
python click + pytest example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hello.py | |
import click | |
@click.command() | |
@click.option( | |
'--name', default='world', | |
prompt='greet whom?', | |
help='who should i greet?' | |
) | |
def main(name): | |
click.echo('Hello {}!'.format(name)) | |
# test_hello.py | |
from click.testing import CliRunner | |
import pytest | |
from hello import hello as hll | |
@pytest.fixture(scope="module") | |
def runner(): | |
return CliRunner() | |
def test_named_hello(runner): | |
result = runner.invoke(hll.main, ['--name','Amy']) | |
assert result.exit_code == 0 | |
assert result.output == 'Hello Amy!\n' | |
def test_default_hello(runner): | |
result = runner.invoke(hll.main, input='\n') | |
assert result.exit_code == 0 | |
print(result.output) | |
expected = 'greet whom? [world]: \nHello world!\n' | |
assert result.output == expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is now a flag, which says to
trapcatch or not exceptions