testing.md
This file
Path
docs/testing.md
283 lines · 7.7 KiB · Markdown
Last changed by
Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284# Testing Click Applications
```{eval-rst}.. currentmodule:: click.testing```
Click provides the {ref}`click.testing <testing>` module to help you invokecommand line applications and check their behavior.
```{warning}These tools should only be used for testing since they change the entireinterpreter state for simplicity. They are not thread-safe: see[Running tests in parallel](#running-tests-in-parallel) for the supportedway to run CLI tests concurrently.```
The examples use [pytest](https://docs.pytest.org/en/stable/) style tests.
```{contents}:depth: 1:local: true```
## Basic Example
The key pieces are: - {class}`CliRunner` - used to invoke commands as command line scripts. - {class}`Result` - returned from {meth}`CliRunner.invoke`. Captures output data, exit code, optional exception, and captures the output as bytes and binary data.
```{code-block} python:caption: hello.py
import click
@click.command()@click.argument('name')def hello(name): click.echo(f'Hello {name}!')```
```{code-block} python:caption: test_hello.py
from click.testing import CliRunnerfrom 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{meth}`CliRunner.invoke`:
```{code-block} python:caption: 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')```
```{code-block} python:caption: test_sync.py
from click.testing import CliRunnerfrom 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 {meth}`CliRunner.invoke` will be used toconstruct the initial {class}`Context object <click.Context>`.For example, setting a fixed terminal width equal to 60:
```{code-block} python:caption: sync.py
import click
@click.group()def cli(): pass
@cli.command()def sync(): click.echo('Syncing')```
```{code-block} python:caption: test_sync.py
from click.testing import CliRunnerfrom 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
To test a command that reads or writes files, give each test its owndirectory and pass absolute paths to the command. Pytest's[`tmp_path`](https://docs.pytest.org/en/stable/how-to/tmp_path.html) fixtureprovides a fresh directory per test; {class}`tempfile.TemporaryDirectory`works without pytest.
```{code-block} python:caption: cat.py
import click
@click.command()@click.argument('f', type=click.File())def cat(f): click.echo(f.read())```
```{code-block} python:caption: test_cat.py
from click.testing import CliRunnerfrom cat import cat
def test_cat(tmp_path): hello = tmp_path / 'hello.txt' hello.write_text('Hello World!')
runner = CliRunner() result = runner.invoke(cat, [str(hello)]) assert result.exit_code == 0 assert result.output == 'Hello World!\n'```
Because the directory is unique to the test and the command receives anabsolute path, nothing leaks between tests and the working directory is neverchanged.
### Running tests in parallel
Isolating the file system with pytest is thread-safe: each test, or each thread,works in its own directory through absolute paths, with no shared state.
{meth}`CliRunner.invoke` is different. While a command runs it swaps severalpieces of process-global state into place (like `sys.stdout`, `sys.stderr`,`sys.stdin`, etc). Two invocations active at the same time in one interpreteroverwrite each other's state, so the runner cannot be driven from severalthreads at once.
```{warning}Do not parallelize CLI tests with threads. Use process-based isolationinstead, where each worker has its own interpreter and its own copy of thoseglobals. [pytest-xdist](https://pytest-xdist.readthedocs.io/) does this with`pytest --numprocesses=auto`.```
Click's own test suite uses exactly this setup: the `tests-random` dependencygroup and the `random` tox environment in[`pyproject.toml`](https://github.com/pallets/click/blob/main/pyproject.toml)run the suite under `pytest-randomly` and `pytest-xdist` to surface orderingbugs and races.
```{deprecated} 8.5.0{meth}`CliRunner.isolated_filesystem` is deprecated and will be removed inClick 9.0. Use a temporary directory with absolute paths, as shown above.```
## Input Streams
The test wrapper can provide input data for the input stream (stdin). This isvery useful for testing prompts.
```{code-block} python:caption: prompt.py
import click
@click.command()@click.option('--foo', prompt=True)def prompt(foo): click.echo(f"foo={foo}")```
```{code-block} python:caption: test_prompt.py
from click.testing import CliRunnerfrom 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 tothe output stream as well. If hidden input is expected then thisdoes not happen.
## Capture modes
{class}`CliRunner` captures output by replacing `sys.stdout` and `sys.stderr`with in-memory wrappers. The `capture` parameter controls which strategy isused.
### `capture="sys"` (default)
Captures Python-level writes (`print()`, `click.echo()`, `sys.stdout.write()`).It is fast and sufficient for most Click applications.
Code that holds a reference to the original `sys.stdout` (like a library thatdoes `from sys import stdout` at import time) bypasses the capture and itsoutput is lost.
In this mode `sys.stdout.fileno()` and `sys.stderr.fileno()` raise{exc}`io.UnsupportedOperation`, matching the pre-`8.3.3` behavior. C-levelconsumers ({mod}`faulthandler`, {mod}`subprocess`, C extensions) that expect areal file descriptor must opt into the `capture="fd"` mode.
### `capture="fd"`
Redirects OS file descriptors `1` and `2` to a temporary file via{func}`os.dup2`, inspired by [Pytest's`capfd`](https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html).This catches output that bypasses `sys.stdout`, including:
- Stale references to the original `sys.stdout` and `sys.stderr`.- Logging frameworks that cache the original stream (like `structlog` or the stdlib's `logging` module).- C extensions and subprocesses that write directly to `fd 1` or `fd 2`.
```pythonfrom click.testing import CliRunnerfrom myapp import cli
def test_captures_everything(): runner = CliRunner(capture="fd") result = runner.invoke(cli) # result.stdout contains both Python-level and fd-level output assert "expected output" in result.stdout```
In this mode `sys.stdout.fileno()` returns the saved (pre-redirection) `fd`, so{mod}`faulthandler` and similar consumers keep working. Writes to `fd 1` and`fd 2` land in the capture tmpfile, so `os.dup2()` calls inside the CLI nolonger leak into the host runner's stdout.
```{note}`capture="fd"` is not available on Windows.```
```{versionchanged} 8.4.0Added the `capture` parameter. The default `sys` mode no longer exposes theoriginal `fd` through `fileno()`, reverting the change introduced in `8.3.3`that broke Pytest's `fd`-level capture teardown. Use `capture="fd"` to restorethat behavior with proper isolation.```