Skip to content
Myr

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

1 file+23 added−16 removed

modified:docs/testing.rst+23 added-16 removed
16 unmodified lines
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.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.2324.. code-block:: python25 :caption: hello.py26
18 unmodified lines
4546Subcommands47------------4849A subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:49A subcommand name must be specified in the `args` parameter :meth:`CliRunner.invoke`:5051.. code-block:: python52 :caption: sync.py53
23 unmodified lines
7778Context 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:81For example, setting a fixed terminal width equal to 60:8283.. 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'}")89 def cli():90 pass9192 @cli.command()93 def sync():94 click.echo('Syncing')
5 unmodified lines
100 from sync import cli101102 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.output108109File System Isolation110---------------------111113The :meth:`CliRunner.isolated_filesystem` method sets the current working directory to a new, empty folder.112The :meth:`CliRunner.isolated_filesystem` context manager sets the current working directory to a new, empty folder.113114.. code-block:: python115 :caption: cat.py116
19 unmodified lines
136 result = runner.invoke(cat, ['hello.txt'])137 assert result.exit_code == 0138 assert result.output == 'Hello World!\n'139141Pass ``temp_dir`` to control where the temporary directory is created.142The directory will not be removed by Click in this case. This is useful140Pass in a path to control where the temporary directory is created.141In this case, the directory will not be removed by Click. Its useful142to integrate with a framework like Pytest that manages temporary files.143144.. code-block:: python145 :caption: test_cat.py146147 def test_keep_dir(tmp_path):148 runner = CliRunner()147 from click.testing import CliRunner148 from cat import cat149150 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!')155156 result = runner.invoke(cat, ['hello.txt'])157 assert result.exit_code == 0158 assert result.output == 'Hello World!\n'159160161Input Streams162-------------163164The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:
19 unmodified lines
184 result = runner.invoke(prompt, input='wau wau\n')185 assert not result.exception186 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'187181Note that prompts will be emulated so that they write the input data to188Prompts will be emulated so they write the input data to189the output stream as well. If hidden input is expected then this190does not happen.