Testing Click Applications

Click provides the click.testing module to help you invoke command line applications and check their behavior.

These tools should only be used for testing since they change the entire interpreter state for simplicity. They are not thread-safe!

The examples use pytest style tests.

Basic Example

The key pieces are:

  • CliRunner - used to invoke commands as command line scripts.

  • Result - returned from CliRunner.invoke(). Captures output data, exit code, optional exception, and captures the output as bytes and binary data.

hello.py
import click

@click.command()
@click.argument('name')
def hello(name):
   click.echo(f'Hello {name}!')
test_hello.py
from click.testing import CliRunner
from 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'

Subcommands

A subcommand name must be specified in the args parameter CliRunner.invoke():

sync.py
import click

@click.group()
@click.option('--debug/--no-debug', default=False)
def cli(debug):
   click.echo(f"Debug mode is {'on' if debug else 'off'}")

@cli.command()
def sync():
   click.echo('Syncing')
test_sync.py
from click.testing import CliRunner
from sync import cli

def test_sync():
  runner = CliRunner()
  result = runner.invoke(cli, ['--debug', 'sync'])
  assert result.exit_code == 0
  assert 'Debug mode is on' in result.output
  assert 'Syncing' in result.output

Context Settings

Additional keyword arguments passed to CliRunner.invoke() will be used to construct the initial Context object. For example, setting a fixed terminal width equal to 60:

sync.py
import click

@click.group()
def cli():
   pass

@cli.command()
def sync():
   click.echo('Syncing')
test_sync.py
from click.testing import CliRunner
from sync import cli

def test_sync():
  runner = CliRunner()
  result = runner.invoke(cli, ['sync'], terminal_width=60)
  assert result.exit_code == 0
  assert 'Debug mode is on' in result.output
  assert 'Syncing' in result.output

File System Isolation

The CliRunner.isolated_filesystem() context manager sets the current working directory to a new, empty folder.

cat.py
import click

@click.command()
@click.argument('f', type=click.File())
def cat(f):
   click.echo(f.read())
test_cat.py
from click.testing import CliRunner
from cat import cat

def test_cat():
   runner = CliRunner()
   with runner.isolated_filesystem():
      with open('hello.txt', 'w') as f:
          f.write('Hello World!')

      result = runner.invoke(cat, ['hello.txt'])
      assert result.exit_code == 0
      assert result.output == 'Hello World!\n'

Pass in a path to control where the temporary directory is created. In this case, the directory will not be removed by Click. Its useful to integrate with a framework like Pytest that manages temporary files.

test_cat.py
from click.testing import CliRunner
from cat import cat

def test_cat_with_path_specified():
   runner = CliRunner()
   with runner.isolated_filesystem('~/test_folder'):
      with open('hello.txt', 'w') as f:
          f.write('Hello World!')

      result = runner.invoke(cat, ['hello.txt'])
      assert result.exit_code == 0
      assert result.output == 'Hello World!\n'

Input Streams

The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts.

prompt.py
import click

@click.command()
@click.option('--foo', prompt=True)
def prompt(foo):
   click.echo(f"foo={foo}")
test_prompt.py
from click.testing import CliRunner
from prompt import prompt

def test_prompts():
   runner = CliRunner()
   result = runner.invoke(prompt, input='wau wau\n')
   assert not result.exception
   assert result.output == 'Foo: wau wau\nfoo=wau wau\n'

Prompts will be emulated so they write the input data to the output stream as well. If hidden input is expected then this does not happen.

File Descriptors and Low-Level I/O

CliRunner captures output by replacing sys.stdout and sys.stderr with in-memory BytesIO-backed wrappers. This is Python-level redirection: calls to echo(), print(), or sys.stdout.write() are captured, but the wrappers have no OS-level file descriptor.

Code that calls fileno() on sys.stdout or sys.stderr, like faulthandler, subprocess, or C extensions, would normally crash with io.UnsupportedOperation inside CliRunner.

To avoid this, CliRunner preserves the original stream’s file descriptor and exposes it via fileno() on the replacement wrapper.

This means:

  • Python-level writes (print(), click.echo(), …) are captured as usual.

  • fd-level writes (C code writing directly to the file descriptor) go to the original terminal and are not captured.

This is the same trade-off that pytest makes with its two capture modes:

  • capsys, which captures Python-level output, where fileno() raises UnsupportedOperation and fd-level writes are not captured.

  • capfd, which captures fd-level output via os.dup2(), where fileno() works and fd-level writes are captured.

Rather than implementing a full capfd-style mechanism, CliRunner takes the simpler path: expose the original fd so that standard library helpers keep working, while accepting that their output is not captured.

Changed in version 8.3.3: fileno() on the redirected streams now returns the original stream’s file descriptor instead of raising.