|
from click.testing import CliRunner |
|
import pytest |
|
|
|
import greet |
|
|
|
|
|
def test_greet_no_options(): |
|
"""greet should prompt the user for their name given no options.""" |
|
runner = CliRunner() |
|
result = runner.invoke(greet.cli, input='John Cleese') |
|
assert not result.exception |
|
assert result.output == 'Your name: John Cleese\nHello, John Cleese!\n' |
|
|
|
|
|
@pytest.mark.parametrize('name', [ |
|
'', |
|
'John Cleese', |
|
'松本 人志', |
|
'New\nLine', |
|
]) |
|
def test_greet_with_name(name): |
|
"""greet --name should not prompt for a name and use the given value.""" |
|
runner = CliRunner() |
|
result = runner.invoke(greet.cli, ['--name', name]) |
|
assert result.output == f"Hello, {name}!\n" |
|
|
|
|
|
@pytest.mark.parametrize('count', [ |
|
'-1', |
|
'0', |
|
'1', |
|
'2', |
|
'3', |
|
'100', |
|
]) |
|
def test_greet_with_count(count): |
|
"""greet --count should output the greeting count times.""" |
|
runner = CliRunner() |
|
result = runner.invoke(greet.cli, ['--count', count], input='John Cleese') |
|
greetings = "Hello, John Cleese!\n" * int(count) |
|
assert result.output == f"Your name: John Cleese\n{greetings}" |
|
|
|
|
|
@pytest.mark.parametrize('name, count', [ |
|
('John Cleese', '5'), |
|
('松本 人志', '10'), |
|
]) |
|
def test_greet_with_name_and_count(name, count): |
|
"""greet should work when given both --name and --count.""" |
|
runner = CliRunner() |
|
result = runner.invoke(greet.cli, ['--name', name, '--count', count]) |
|
assert result.output == f"Hello, {name}!\n" * int(count) |
|
|
|
def test_greet_shows_debug_info_in_debug_mode(): |
|
runner = CliRunner() |
|
result = runner.invoke(greet.cli, ['--name', 'John Cleese', '--debug']) |
|
assert result.output == ('[DEBUG] Printing greeting 1 times for John ' |
|
'Cleese.\nHello, John Cleese!\n') |