Skip to main content

A Doctest-stype Command Line Application Tester

Project description

https://img.shields.io/pypi/v/clatter.svg https://travis-ci.org/delgadom/clatter.svg?branch=master https://coveralls.io/repos/github/delgadom/clatter/badge.svg?branch=master Documentation Status Updates https://api.codacy.com/project/badge/Grade/2c2af36490c04543b925edafc0d66842

clatter is a doctest-style testing tool for command-line applications. It wraps other testing suites and allows them to be tested in docstrings.

Features

  • Bring testing best practices to your command line apps

  • Extensible - subclassing CommandValidator is trivial using any cli testing suite

Downsides

  • Security. CLI commands are dangerous, and we make no attempt to protect you. Use at your own risk.

Usage

Test command line utilities and applications by whitelisting them with app-specific testing engines:

>>> teststr = r'''
...
... .. code-block:: bash
...
...     $ echo 'Pining for the fjords'
...     Pining for the fjords
...     <BLANKLINE>
... '''
>>>
>>> tester = Runner()
>>> tester.call_engines['echo'] = SubprocessValidator()
>>> tester.validate(teststr)

Click applications

Integrate your command line app:

>>> @click.command()
... @click.argument('name')
... def hello(name):
...     click.echo('Hello %s!' % name)

This can now be tested in docstrings:

>>> teststr = '''
...
... .. code-block:: bash
...
...     $ hello Polly
...     Hello Polly!
...     <BLANKLINE>
...
...     $ hello Polly Parrot
...     Usage: hello [OPTIONS] NAME
...     <BLANKLINE>
...     Error: Got unexpected extra argument (Parrot)
...     <BLANKLINE>
...
...     $ hello 'Polly Parrot' # clitest: +NORMALIZE_WHITESPACE
...     Hello Polly Parrot!
...
... '''

Click applications can be tested with a ClickValidator engine:

>>> tester = Runner()
>>> tester.call_engines['hello'] = ClickValidator(hello)

>>> tester.validate(teststr)

Mixed applications

Your app can be combined with other command-line utilities by adding multiple engines:

>>> teststr = r'''
...
... .. code-block:: bash
...
...     $ hello Polly
...     Hello Polly!
...     <BLANKLINE>
...
...     $ echo 'Pining for the fjords'
...     Pining for the fjords
...     <BLANKLINE>
...
... Pipes/redirects don't work, so we can't redirect this value into a file.
... But we can write a file with python:
...
... .. code-block:: bash
...
...     $ python -c \
...     >     "with open('tmp.txt', 'w+') as f: f.write('Pushing up daisies')"
...
...     $ cat tmp.txt
...     Pushing up daisies
...
... '''

>>> tester.call_engines['echo'] = SubprocessValidator()
>>> tester.call_engines['python'] = SubprocessValidator()
>>> tester.call_engines['cat'] = SubprocessValidator()

>>> tester.validate(teststr)

Suppressing commands

Commands can be skipped altogether with a SkipValidator:

>>> skipstr = '''
... .. code-block:: bash
...
...     $ aws storage buckets list
...
... '''

>>> tester = Runner()
>>> tester.call_engines['aws'] = SkipValidator()

Illegal commands

Errors are raised when using an application you haven’t whitelisted:

>>> badstr = '''
...
... The following block of code should cause an error:
...
... .. code-block:: bash
...
...     $ rm tmp.txt
...
... '''

>>> tester.validate(badstr)
Traceback (most recent call last):
...
ValueError: Command "rm" not allowed. Add command caller to call_engines to whitelist.

Unrecognized commands will raise an error, even if +SKIP is specified

>>> noskip = '''
... .. code-block:: bash
...
...     $ nmake all # clitest: +SKIP
...
... '''
>>> tester.validate(badstr)
Traceback (most recent call last):
...
ValueError: Command "nmake" not allowed. Add command caller to call_engines to whitelist.

Error handling

Lines failing to match the command’s output will raise an error

>>> teststr = r'''
... .. code-block:: bash
...
...     $ echo "There, it moved!"
...     "No it didn't!"
...     <BLANKLINE>
...
... '''

>>> tester = Runner()
>>> tester.call_engines['echo'] = SubprocessValidator()

>>> tester.validate(teststr)
Traceback (most recent call last):
...
ValueError: Clatter test failed. There, it moved! != No it didn't!

+ There, it moved!

- No it didn't!

Installation

pip install clatter

Requirements

  • pytest

Todo

See issues to see and add to our todos.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clatter-0.0.3.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

clatter-0.0.3-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file clatter-0.0.3.tar.gz.

File metadata

  • Download URL: clatter-0.0.3.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for clatter-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0896cb4c561ef28ff1ce3e55d217be89847bb197fdc9122cf10b2a23a4aadd67
MD5 1ad17b00cec7f2c3c8091ba8b61760db
BLAKE2b-256 b4802041d48bb62648b8e052462a55522297e3f0de10e391e9add9287b6883a6

See more details on using hashes here.

File details

Details for the file clatter-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for clatter-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 492d933ec9aa6f77fff1e65139f86cf7e1063eee4c034978f0e36dea001f700b
MD5 9006bd325c8035d6c8c30593832ff11b
BLAKE2b-256 409db23d5b42185f8973e2ca4373324f8b88669530c909429b05a0bdde76705e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page