Skip to content
Myr

Rough out testing docs changes.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Edward G · committed by Edward G · parent 5dd6288 · Browse files · 2 files+56 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+56 added−25 removed

  • other:+56 added−25 removed

2 files+56 added−25 removed

modified:docs/api.rst+6 added
4 unmodified lines
56This part of the documentation lists the full API reference of all public7classes and functions.89.. contents::10 :depth: 111 :local:1213Decorators14----------1516.. autofunction:: command
185 unmodified lines
202203.. autofunction:: add_completion_class204205206.. _testing:207208Testing209-------210211.. currentmodule:: click.testing
6 unmodified lines
modified:docs/testing.rst+50 added-25 removed
1 unmodified line
2==========================34.. currentmodule:: click.testing56For basic testing, Click provides the :mod:`click.testing` module which7provides test functionality that helps you invoke command line8applications and check their behavior.6Click provides the :ref:`click.testing <testing>` module to help you invoke command line applications and check their behavior.710These tools should really only be used for testing as they change11the entire interpreter state for simplicity and are not in any way12thread-safe!8These tools should only be used for testing since they change9the entire interpreter state for simplicity. They are not thread-safe!1014Basic Testing15-------------11The examples use `pytest <https://docs.pytest.org/en/stable/>`_ style tests.1217The basic functionality for testing Click applications is the18:class:`CliRunner` which can invoke commands as command line scripts. The19:meth:`CliRunner.invoke` method runs the command line script in isolation20and captures the output as both bytes and binary data.13.. contents::14 :depth: 115 :local:1622The return value is a :class:`Result` object, which has the captured output23data, exit code, and optional exception attached:17Basic Example18-------------1920The key pieces are:21 * :class:`CliRunner` - to invoke commands as command line scripts.22 * :class:`Result` - returned from :meth:`CliRunner.invoke`, captures output data, exit code, and optional exception, captures the output as both bytes and binary data.2324.. code-block:: python25 :caption: hello.py2627 import click
14 unmodified lines
42 result = runner.invoke(hello, ['Peter'])43 assert result.exit_code == 044 assert result.output == 'Hello Peter!\n'4547For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:46Subcommands47------------4849A subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:5051.. code-block:: python52 :caption: sync.py5354 import click
19 unmodified lines
74 assert result.exit_code == 075 assert 'Debug mode is on' in result.output76 assert 'Syncing' in result.output7776Additional keyword arguments passed to ``.invoke()`` will be used to construct the initial Context object.77For example, if you want to run your tests against a fixed terminal width you can use the following::78Context Settings79-----------------80Additional keyword arguments passed to :meth:`CliRunner.invoke` will be used to construct the initial :class:`Context object <click.Context>`.81For example, to run your tests against a fixed terminal width equal to 60:8279 runner = CliRunner()80 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)83.. code-block:: python84 :caption: sync.py8586 import click8788 @click.group()89 @click.option('--debug/--no-debug', default=False)90 def cli(debug):91 click.echo(f"Debug mode is {'on' if debug else 'off'}")9293 @cli.command()94 def sync():95 click.echo('Syncing')9697.. code-block:: python98 :caption: test_sync.py99100 from click.testing import CliRunner101 from sync import cli102103 def test_sync():104 runner = CliRunner()105 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)106 assert result.exit_code == 0107 assert 'Debug mode is on' in result.output108 assert 'Syncing' in result.output109110File System Isolation111---------------------11285For basic command line tools with file system operations, the86:meth:`CliRunner.isolated_filesystem` method is useful for setting the87current working directory to a new, empty folder.113The :meth:`CliRunner.isolated_filesystem` method sets the current working directory to a new, empty folder.114115.. code-block:: python116 :caption: cat.py117
35 unmodified lines
153154Input Streams155-------------156131The test wrapper can also be used to provide input data for the input132stream (stdin). This is very useful for testing prompts, for instance:157The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:158159.. code-block:: python160 :caption: prompt.py161
17 unmodified lines
179 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'180181Note that prompts will be emulated so that they write the input data to182the output stream as well. If hidden input is expected then this158obviously does not happen.183does not happen.