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
Provenance
- Commit
48f3e91- Tree
8e1292d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c265355
3 files changed+82 added−6 removed
modified:click/testing.py+15 added-6 removed
41 unmodified lines42 43 class Result(object):44 """Holds the captured result of an invoked CLI script."""45 46- 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 = exception55 56+ @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')61+62 def __repr__(self):63 return '<Result %s>' % (64 self.exception and repr(self.exception) or 'okay',65 )133 unmodified lines199 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()204 197- return Result(output=output,205+ return Result(runner=self,206+ output_bytes=output,207 exit_code=exit_code,208 exception=exception)209 210 @contextlib.contextmanager4 unmodified lines215 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 linesmodified:docs/index.rst+1 added
54 unmodified lines55 documentation56 setuptools57 complex58 advanced59+ testing60 python361 62 API Reference63 -------------8 unmodified linesadded:docs/testing.rst+66 added
1+Testing Click Applications2+==========================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.9+10+These tools really only should be used for testing as they are changing11+the whole interpreter state for simplicity and they are not in any way12+thread safe!13+14+Basic Testing15+-------------16+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.21+22+The return value is a :class:`Result` object which has the captured output23+data, exit code and optional exception attached.24+25+Example::26+27+ import click28+ from click.testing import CliRunner29+30+ def test_hello_world():31+ @click.command()32+ @click.argument('name')33+ def hello(name):34+ click.echo('Hello %s!' % name)35+36+ runner = CliRunner()37+ result = runner.invoke(hello, ['Peter'])38+ assert result.exit_code == 039+ assert result.output == 'Hello Peter!\n'40+41+File System Isolation42+---------------------43+44+For basic command line tools that want to operate with the file system the45+:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up46+an empty folder and changes the current working directory to.47+48+Example::49+50+ import click51+ from click.testing import CliRunner52+53+ def test_hello_world():54+ @click.command()55+ @click.argument('f', type=click.File())56+ def cat(f):57+ click.echo(f.read())58+59+ runner = CliRunner()60+ with runner.isolated_filesystem():61+ with open('hello.txt') as f:62+ f.write('Hello World!')63+64+ result = runner.invoke(cat, ['hello.txt'])65+ assert result.exit_code == 066+ assert result.output == 'Hello World!\n'