Skip to main content

cli-test-helpers Latest version on PyPI

Build status Python versions Software license

Useful helpers for writing tests for your Python CLI program.

Writing tests for a command line interface (CLI) tool may not seem strictly straight-forward when you think in terms of unit tests. Especially, when you use the argparse module or the click package, control of the application entry point is a bit taken away from you.

But it’s not all that bad. This package is here to help. The examples give you some guidance on how to get started, and the helpers allow you to deal with common cases, such as mocking CLI arguments and environment variable values.

Installation

pip install cli-test-helpers

Preferrably, though, you add cli-test-helpers as a dependency to your Tox environment (see example).

Usage

Let’s assume you use pytest for running your tests, which is certainly a good idea. Your CLI program is called foobar. You have prepared a setup.py with a CLI entrypoint. For the tests you have prepared a tests/ folder (outside of foobar/, because you don’t want your tests to be packaged up with your application code). Then your directory layout looks somewhat like our example.

Functional tests

Start with a simple set of functional tests:

  • Is the entrypoint script installed? (tests the configuration in your setup.py)

  • Can this package be run as a Python module? (i.e. without having to be installed)

  • Is command XYZ available? etc. Cover your entire CLI usage here!

This is almost a stupid exercise: Run the command as a shell command and inspect the status code of the exiting process (see example). The trick is that you run a non-destructive command, e.g. by using the usual --help option of every command. This should cover your entire CLI user interface definition.

Unit tests

Then you’re ready to take advantage of our helpers.

ArgvContext allows you to mimic the use of specific CLI arguments:

def test_cli_command(mock_command):
    """Is the correct code called when invoked via the CLI?"""
    with ArgvContext('foobar', 'baz'):
        foobar.command.baz()

    assert mock_command.call_count == 1

EnvironContext allows you to mimic the presence of environment values:

def test_fail_without_secret():
    """Must fail without a ``SECRET`` environment variable specified"""
    message = "Environment value SECRET not set."

    with EnvironContext(SECRET=None):
        with pytest.raises(SystemExit, match=message):
            foobar.command.baz()
            pytest.fail("CLI doesn't abort with missing SECRET")

See example.

TDD

Remember to stick to the test-driven mantra:

  1. Write one line of test code. Make the test fail.

  2. Write one line of application code. Make the test pass.

  3. Goto 1.

Release files for cli-test-helpers 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cli-test-helpers 1.0.0
File Size Uploaded
cli-test-helpers-1.0.0.tar.gz 17.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cli-test-helpers 1.0.0
File Interpreter ABI Platform
cli_test_helpers-1.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 33.5 kB

Release files / cli-test-helpers-1.0.0.tar.gz

Download URL cli-test-helpers-1.0.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
eedff2c4f754f250042702089219dc29827b2c8d53a7d2dd1c3d4ce3d05af8df
BLAKE2b-256 checksum
How to use checksums
b5bfa7326bb5b54c78ec67eed7c1e9341a67c24ef09e13814164f7b30f1b03a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

Release files / cli_test_helpers-1.0.0-py2-none-any.whl

Download URL cli_test_helpers-1.0.0-py2-none-any.whl
Size 16.3 kB
Tags Python 2
SHA-256 checksum
How to use checksums
ed1bbae7031f66339510076450ad4ccff918ffadf565f7dfa884f148cb674d00
BLAKE2b-256 checksum
How to use checksums
19692df351e8e099b562ff17bda3a8e9d9ba09d283165119d0a826335670d604
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

Release history Release notifications | RSS feed

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.1

2 release files

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page