LoginSignup
4

More than 5 years have passed since last update.

【Python】コマンドラインパーサーclickのテスト方法

Posted at

なにこれ

Pythonでclickをつかったコンソールアプリを作った時に、パラメータがちゃんと渡ってるかテストしたい時ありますよね。CliRunnerを使うと、clickで作ったアプリケーションのテストが出来ます。

基本的な例

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'

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4