Skip to content
Myr

Added testing docs

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

authored by Armin Ronacher · committed by Armin Ronacher · parent c265355 · Browse files · 3 files+82 added−6 removed
This commit

State

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

Diff stats

3 files+82 added−6 removed

  • other:+82 added−6 removed

3 files+82 added−6 removed

modified:click/testing.py+15 added-6 removed
41 unmodified lines
4243class Result(object):44 """Holds the captured result of an invoked CLI script."""4546 def __init__(self, output, exit_code, exception):47 #: The output as unicode string.48 self.output = output46 def __init__(self, runner, output_bytes, exit_code, exception):47 #: The runner that created the result48 self.runner = runner49 #: The output as bytes.50 self.output_bytes = output_bytes51 #: The exit code as integer.52 self.exit_code = exit_code53 #: The exception that happend if one did.54 self.exception = exception5556 @property57 def output(self):58 """The output as unicode string."""59 return self.output_bytes.decode(self.runner.charset, 'replace') \60 .replace('\r\n', '\n')6162 def __repr__(self):63 return '<Result %s>' % (64 self.exception and repr(self.exception) or 'okay',65 )
133 unmodified lines
199 exit_code = e.code200 except Exception as e:201 exception = e202 exit_code = -1195 output = out.getvalue().decode(self.charset).replace('\r\n', '\n')203 output = out.getvalue()204197 return Result(output=output,205 return Result(runner=self,206 output_bytes=output,207 exit_code=exit_code,208 exception=exception)209210 @contextlib.contextmanager
4 unmodified lines
215 cwd = os.getcwd()216 t = tempfile.mkdtemp()217 os.chdir(t)218 try:210 yield219 yield t220 finally:221 os.chdir(cwd)222 try:223 shutil.rmtree(t)
2 unmodified lines
modified:docs/index.rst+1 added
54 unmodified lines
55 documentation56 setuptools57 complex58 advanced59 testing60 python36162API Reference63-------------
8 unmodified lines
added:docs/testing.rst+66 added
1Testing Click Applications2==========================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.910These tools really only should be used for testing as they are changing11the whole interpreter state for simplicity and they are not in any way12thread safe!1314Basic Testing15-------------1617The 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.2122The return value is a :class:`Result` object which has the captured output23data, exit code and optional exception attached.2425Example::2627 import click28 from click.testing import CliRunner2930 def test_hello_world():31 @click.command()32 @click.argument('name')33 def hello(name):34 click.echo('Hello %s!' % name)3536 runner = CliRunner()37 result = runner.invoke(hello, ['Peter'])38 assert result.exit_code == 039 assert result.output == 'Hello Peter!\n'4041File System Isolation42---------------------4344For basic command line tools that want to operate with the file system the45:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up46an empty folder and changes the current working directory to.4748Example::4950 import click51 from click.testing import CliRunner5253 def test_hello_world():54 @click.command()55 @click.argument('f', type=click.File())56 def cat(f):57 click.echo(f.read())5859 runner = CliRunner()60 with runner.isolated_filesystem():61 with open('hello.txt') as f:62 f.write('Hello World!')6364 result = runner.invoke(cat, ['hello.txt'])65 assert result.exit_code == 066 assert result.output == 'Hello World!\n'