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
Provenance
- Commit
614085d- Tree
a0172a5- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
5dd6288
2 files changed+56 added−25 removed
modified:docs/api.rst+6 added
4 unmodified lines5 6 This part of the documentation lists the full API reference of all public7 classes and functions.8 9+.. contents::10+ :depth: 111+ :local:12+13 Decorators14 ----------15 16 .. autofunction:: command185 unmodified lines202 203 .. autofunction:: add_completion_class204 205 206+.. _testing:207+208 Testing209 -------210 211 .. currentmodule:: click.testing6 unmodified linesmodified:docs/testing.rst+50 added-25 removed
1 unmodified line2 ==========================3 4 .. currentmodule:: click.testing5 6-For basic testing, Click provides the :mod:`click.testing` module which7-provides test functionality that helps you invoke command line8-applications and check their behavior.6+Click provides the :ref:`click.testing <testing>` module to help you invoke command line applications and check their behavior.7 10-These tools should really only be used for testing as they change11-the entire interpreter state for simplicity and are not in any way12-thread-safe!8+These tools should only be used for testing since they change9+the entire interpreter state for simplicity. They are not thread-safe!10 14-Basic Testing15--------------11+The examples use `pytest <https://docs.pytest.org/en/stable/>`_ style tests.12 17-The 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 isolation20-and captures the output as both bytes and binary data.13+.. contents::14+ :depth: 115+ :local:16 22-The return value is a :class:`Result` object, which has the captured output23-data, exit code, and optional exception attached:17+Basic Example18+-------------19 20+The 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.23+24 .. code-block:: python25 :caption: hello.py26 27 import click14 unmodified lines42 result = runner.invoke(hello, ['Peter'])43 assert result.exit_code == 044 assert result.output == 'Hello Peter!\n'45 47-For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:46+Subcommands47+------------48 49+A subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:50+51 .. code-block:: python52 :caption: sync.py53 54 import click19 unmodified lines74 assert result.exit_code == 075 assert 'Debug mode is on' in result.output76 assert 'Syncing' in result.output77 76-Additional keyword arguments passed to ``.invoke()`` will be used to construct the initial Context object.77-For example, if you want to run your tests against a fixed terminal width you can use the following::78+Context Settings79+-----------------80+Additional keyword arguments passed to :meth:`CliRunner.invoke` will be used to construct the initial :class:`Context object <click.Context>`.81+For example, to run your tests against a fixed terminal width equal to 60:82 79- runner = CliRunner()80- result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)83+.. code-block:: python84+ :caption: sync.py85 86+ import click87+88+ @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'}")92+93+ @cli.command()94+ def sync():95+ click.echo('Syncing')96+97+.. code-block:: python98+ :caption: test_sync.py99+100+ from click.testing import CliRunner101+ from sync import cli102+103+ 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.output109+110 File System Isolation111 ---------------------112 85-For basic command line tools with file system operations, the86-:meth:`CliRunner.isolated_filesystem` method is useful for setting the87-current working directory to a new, empty folder.113+The :meth:`CliRunner.isolated_filesystem` method sets the current working directory to a new, empty folder.114 115 .. code-block:: python116 :caption: cat.py117 35 unmodified lines153 154 Input Streams155 -------------156 131-The test wrapper can also be used to provide input data for the input132-stream (stdin). This is very useful for testing prompts, for instance:157+The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:158 159 .. code-block:: python160 :caption: prompt.py161 17 unmodified lines179 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'180 181 Note that prompts will be emulated so that they write the input data to182 the output stream as well. If hidden input is expected then this158-obviously does not happen.183+does not happen.