Polish 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 614085d · Browse files · 1 file·+23 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+23 added−16 removed
- other:+23 added−16 removed
Provenance
- Commit
17040b9- Tree
942c6cc- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
614085d
1 file changed+23 added−16 removed
modified:docs/testing.rst+23 added-16 removed
16 unmodified lines17 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.21+ * :class:`CliRunner` - used to invoke commands as command line scripts.22+ * :class:`Result` - returned from :meth:`CliRunner.invoke`. Captures output data, exit code, optional exception, and captures the output as bytes and binary data.23 24 .. code-block:: python25 :caption: hello.py26 18 unmodified lines45 46 Subcommands47 ------------48 49-A subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:49+A subcommand name must be specified in the `args` parameter :meth:`CliRunner.invoke`:50 51 .. code-block:: python52 :caption: sync.py53 23 unmodified lines77 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:81+For example, setting a fixed terminal width equal to 60:82 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'}")89+ def cli():90+ pass91 92 @cli.command()93 def sync():94 click.echo('Syncing')5 unmodified lines100 from sync import cli101 102 def test_sync():103 runner = CliRunner()105- result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)104+ result = runner.invoke(cli, ['sync'], terminal_width=60)105 assert result.exit_code == 0106 assert 'Debug mode is on' in result.output107 assert 'Syncing' in result.output108 109 File System Isolation110 ---------------------111 113-The :meth:`CliRunner.isolated_filesystem` method sets the current working directory to a new, empty folder.112+The :meth:`CliRunner.isolated_filesystem` context manager sets the current working directory to a new, empty folder.113 114 .. code-block:: python115 :caption: cat.py116 19 unmodified lines136 result = runner.invoke(cat, ['hello.txt'])137 assert result.exit_code == 0138 assert result.output == 'Hello World!\n'139 141-Pass ``temp_dir`` to control where the temporary directory is created.142-The directory will not be removed by Click in this case. This is useful140+Pass in a path to control where the temporary directory is created.141+In this case, the directory will not be removed by Click. Its useful142 to integrate with a framework like Pytest that manages temporary files.143 144 .. code-block:: python145+ :caption: test_cat.py146 147- def test_keep_dir(tmp_path):148- runner = CliRunner()147+ from click.testing import CliRunner148+ from cat import cat149 150- with runner.isolated_filesystem(temp_dir=tmp_path) as td:151- ...150+ def test_cat_with_path_specified():151+ runner = CliRunner()152+ with runner.isolated_filesystem('~/test_folder'):153+ with open('hello.txt', 'w') as f:154+ f.write('Hello World!')155 156+ result = runner.invoke(cat, ['hello.txt'])157+ assert result.exit_code == 0158+ assert result.output == 'Hello World!\n'159 160+161 Input Streams162 -------------163 164 The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:19 unmodified lines184 result = runner.invoke(prompt, input='wau wau\n')185 assert not result.exception186 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'187 181-Note that prompts will be emulated so that they write the input data to188+Prompts will be emulated so they write the input data to189 the output stream as well. If hidden input is expected then this190 does not happen.