Separates source from test code in test examples
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Posada · committed by Daniel Posada · parent 3932d80 · Browse files · 1 file·+89 added−62 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+89 added−62 removed
- other:+89 added−62 removed
Provenance
- Commit
83489d9- Tree
a7cf44d- Author
Daniel Posada <daniel.posada@gmail.com>- Committer
Daniel Posada <daniel.posada@gmail.com>- Parent
3932d80
1 file changed+89 added−62 removed
modified:docs/testing.rst+89 added-62 removed
18 unmodified lines19 :meth:`CliRunner.invoke` method runs the command line script in isolation20 and captures the output as both bytes and binary data.21 22 The return value is a :class:`Result` object, which has the captured output23-data, exit code, and optional exception attached.23+data, exit code, and optional exception attached:24 25-Example::25+.. code-block:: python26+ :caption: hello.py27 27- import click28- from click.testing import CliRunner28+ import click29 30- def test_hello_world():31- @click.command()32- @click.argument('name')33- def hello(name):34- click.echo('Hello %s!' % name)30+ @click.command()31+ @click.argument('name')32+ def hello(name):33+ click.echo('Hello %s!' % name)34 36- runner = CliRunner()37- result = runner.invoke(hello, ['Peter'])38- assert result.exit_code == 039- assert result.output == 'Hello Peter!\n'35+.. code-block:: python36+ :caption: test_hello.py37 41-For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method.38+ from click.testing import CliRunner39+ from hello import hello40 43-Example::41+ def test_hello_world():42+ runner = CliRunner()43+ result = runner.invoke(hello, ['Peter'])44+ assert result.exit_code == 045+ assert result.output == 'Hello Peter!\n'46 45- import click46- from click.testing import CliRunner47- 48- def test_sync():49- @click.group()50- @click.option('--debug/--no-debug', default=False)51- def cli(debug):52- click.echo('Debug mode is %s' % ('on' if debug else 'off')) 53- 54- @cli.command()55- def sync():56- click.echo('Syncing')57- 58- runner = CliRunner()59- result = runner.invoke(cli, ['--debug', 'sync'])60- assert result.exit_code == 061- assert 'Debug mode is on' in result.output62- assert 'Syncing' in result.output47+For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:48 64-Additional keyword arguments passed to ``.invoke()`` will be used to construct the initial Context object. For example, if you want to run your tests against a fixed terminal width you can use the following::49+.. code-block:: python50+ :caption: sync.py51 52+ import click53+54+ @click.group()55+ @click.option('--debug/--no-debug', default=False)56+ def cli(debug):57+ click.echo('Debug mode is %s' % ('on' if debug else 'off'))58+59+ @cli.command()60+ def sync():61+ click.echo('Syncing')62+63+.. code-block:: python64+ :caption: test_sync.py65+66+ from click.testing import CliRunner67+ from sync import cli68+69+ def test_sync():70+ runner = CliRunner()71+ result = runner.invoke(cli, ['--debug', 'sync'])72+ assert result.exit_code == 073+ assert 'Debug mode is on' in result.output74+ assert 'Syncing' in result.output75+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+79 runner = CliRunner()80 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)81 82 File System Isolation83 ---------------------84 85 For basic command line tools that want to operate with the file system, the86 :meth:`CliRunner.isolated_filesystem` method comes in useful which sets up74-an empty folder and changes the current working directory to.87+an empty folder and changes the current working directory to:88 76-Example::89+.. code-block:: python90+ :caption: cat.py91 78- import click79- from click.testing import CliRunner92+ import click93 81- def test_cat():82- @click.command()83- @click.argument('f', type=click.File())84- def cat(f):85- click.echo(f.read())94+ @click.command()95+ @click.argument('f', type=click.File())96+ def cat(f):97+ click.echo(f.read())98 87- runner = CliRunner()88- with runner.isolated_filesystem():89- with open('hello.txt', 'w') as f:90- f.write('Hello World!')99+.. code-block:: python100+ :caption: test_cat.py101 92- result = runner.invoke(cat, ['hello.txt'])93- assert result.exit_code == 094- assert result.output == 'Hello World!\n'102+ from click.testing import CliRunner103+ from cat import cat104 105+ def test_cat():106+ runner = CliRunner()107+ with runner.isolated_filesystem():108+ with open('hello.txt', 'w') as f:109+ f.write('Hello World!')110+111+ result = runner.invoke(cat, ['hello.txt'])112+ assert result.exit_code == 0113+ assert result.output == 'Hello World!\n'114+115 Input Streams116 -------------117 118 The test wrapper can also be used to provide input data for the input100-stream (stdin). This is very useful for testing prompts, for instance::119+stream (stdin). This is very useful for testing prompts, for instance:120 102- import click103- from click.testing import CliRunner121+.. code-block:: python122+ :caption: prompt.py123 105- def test_prompts():106- @click.command()107- @click.option('--foo', prompt=True)108- def test(foo):109- click.echo('foo=%s' % foo)124+ import click125 111- runner = CliRunner()112- result = runner.invoke(test, input='wau wau\n')113- assert not result.exception114- assert result.output == 'Foo: wau wau\nfoo=wau wau\n'126+ @click.command()127+ @click.option('--foo', prompt=True)128+ def prompt(foo):129+ click.echo('foo=%s' % foo)130 131+.. code-block:: python132+ :caption: test_prompt.py133+134+ from click.testing import CliRunner135+ from prompt import prompt136+137+ def test_prompts():138+ runner = CliRunner()139+ result = runner.invoke(prompt, input='wau wau\n')140+ assert not result.exception141+ assert result.output == 'Foo: wau wau\nfoo=wau wau\n'142+143 Note that prompts will be emulated so that they write the input data to144 the output stream as well. If hidden input is expected then this145 obviously does not happen.