Skip to content

Instantly share code, notes, and snippets.

@kenessajr
Forked from lbillingham/split_into_2_files.py
Created December 5, 2018 16:02
Show Gist options
  • Save kenessajr/921999c3a6d93e4ca6fd329f5f393094 to your computer and use it in GitHub Desktop.
Save kenessajr/921999c3a6d93e4ca6fd329f5f393094 to your computer and use it in GitHub Desktop.
python click + pytest example
# 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