Skip to content

Instantly share code, notes, and snippets.

@huaxlin
Last active February 20, 2023 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huaxlin/39839c1ebbd93f97311d1694ffad8e76 to your computer and use it in GitHub Desktop.
Save huaxlin/39839c1ebbd93f97311d1694ffad8e76 to your computer and use it in GitHub Desktop.
Basic Testing of Testing Click Applications

Basic Click Application

$ pip install click
$ python click_hello.py Jeff
Hello Jeff!
$ python click_hello.py Alice
Hello Alice!

Test It

$ pip install pytest
$ pytest test_click_hello.py
====== test session starts ========
platform linux -- Python 3.8.16, pytest-7.2.1, pluggy-1.0.0
rootdir: ...
collected 1 item

test_click_hello.py .    [100%]
======= 1 passed in 0.02s =======
import click
@click.command()
@click.argument('name')
def hello(name):
click.echo(f'Hello {name}!')
if __name__ == '__main__':
hello()
from click.testing import CliRunner
from click_hello import hello
def test_hello_world():
runner = CliRunner()
result = runner.invoke(hello, ['Peter'])
assert result.exit_code == 0
assert result.output == 'Hello Peter!\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment