Skip to main content

Useful helpers for writing tests for your Python CLI program.

Project description

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

Preferably, 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:

@patch('foobar.command.baz')
def test_cli_command(mock_command):
    """Is the correct code called when invoked via the CLI?"""
    with ArgvContext('foobar', 'baz'), pytest.raises(SystemExit):
        foobar.cli.main()

    assert mock_command.called

EnvironContext allows you to mimic the presence of environment values:

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

    with EnvironContext(SECRET=None):
        with pytest.raises(SystemExit, match=message_regex):
            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.

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

cli-test-helpers-1.0.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

cli_test_helpers-1.0.1-py2-none-any.whl (16.3 kB view details)

Uploaded Python 2

File details

Details for the file cli-test-helpers-1.0.1.tar.gz.

File metadata

  • Download URL: cli-test-helpers-1.0.1.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

File hashes

Hashes for cli-test-helpers-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a1b3e2d901e0eb356e051cb3d78d99c1f1590a3c6b20df9cde8564b8d4c4ee03
MD5 4e260bad09dee0a717130e57664df757
BLAKE2b-256 7cb51a372ea120928d379e0dc330683e5288cfce6c3f3c55ed5c56cee147401b

See more details on using hashes here.

File details

Details for the file cli_test_helpers-1.0.1-py2-none-any.whl.

File metadata

  • Download URL: cli_test_helpers-1.0.1-py2-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

File hashes

Hashes for cli_test_helpers-1.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 8a3f470c730d19f8092f074dda82f1cd47cf0114557e791757cb495a1bf5f3d2
MD5 b2e72abfc8179617a774e513cc713b5f
BLAKE2b-256 cf70fc64bbc59d281137e5fab0334d689120bdfd21dc06d1f5335feda7e5724f

See more details on using hashes here.

Supported by

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