Skip to main content

Pytest plugin to simplify running shell commands against the system

Project description

CI Docs Codecov Python Versions Python Wheel Code Style: black PyPI - License

What is Pytest Shell Utilities

“When in doubt, shell out”

—Thomas S. Hatch

This pytest plugin was extracted from pytest-salt-factories. If provides a basic fixture shell which basically uses subprocess.Popen to run commands against the running system on a shell while providing a nice assert’able return class.

Install

Installing pytest-shell-utilities is as simple as:

python -m pip install pytest-shell-utilities

And, that’s honestly it.

Usage

Once installed, you can now use the shell fixture to run some commands and assert against the outcome.

def test_assert_good_exitcode(shell):

    ret = shell.run("exit", "0")
    assert ret.returncode == 0


def test_assert_bad_exitcode(shell):

    ret = shell.run("exit", "1")
    assert ret.returncode == 1

If the command outputs parseable JSON, the shell fixture can attempt loading that output as JSON which allows for asserting against the JSON loaded object.

def test_against_json_output(shell):
    d = {"a": "a", "b": "b"}
    ret = shell.run("echo", json.dumps(d))
    assert ret.data == d

Additionally, the return object’s .stdout and .stderr can be line matched using pytest.pytester.LineMatcher:

MARY_HAD_A_LITTLE_LAMB = """\
Mary had a little lamb,
Its fleece was white as snow;
And everywhere that Mary went
The lamb was sure to go.
"""


def test_matcher_attribute(shell):
    ret = shell.run("echo", MARY_HAD_A_LITTLE_LAMB)
    ret.stdout.matcher.fnmatch_lines_random(
        [
            "*had a little*",
            "Its fleece was white*",
            "*Mary went",
            "The lamb was sure to go.",
        ]
    )

Documentation

The full documentation can be seen here.

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

pytest-shell-utilities-1.0.5.tar.gz (90.4 kB view details)

Uploaded Source

Built Distribution

pytest_shell_utilities-1.0.5-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest-shell-utilities-1.0.5.tar.gz.

File metadata

  • Download URL: pytest-shell-utilities-1.0.5.tar.gz
  • Upload date:
  • Size: 90.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytest-shell-utilities-1.0.5.tar.gz
Algorithm Hash digest
SHA256 b617bee689f9f964b711e5a9c49ffb9f04aca9013fffbd29c0e3c37dbf1addeb
MD5 7a00608abf381efe5babade3cbdd46f6
BLAKE2b-256 5394308bf33d17935c2b3aade542bf417c9454db1d47b7dd5ebe820c0436f7e7

See more details on using hashes here.

File details

Details for the file pytest_shell_utilities-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pytest_shell_utilities-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytest_shell_utilities-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a4a8352c8fffa66e281c70615babdcc3e311d2b5fd2cd63d3eb60b10d8cade53
MD5 5e921be451d4ef5055c8eaf8a707b669
BLAKE2b-256 826f163ea661503751d9415268f2f54b7cba52507e3c5b11f0c66bb078f3f672

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