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.2.0.tar.gz (90.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pytest-shell-utilities-1.2.0.tar.gz
Algorithm Hash digest
SHA256 feac4d7328b21f6efb6dc4ea9cf8f8b852466d1836417b2e784a086f30242297
MD5 9f3acba170d242e74e241471cb0f7d35
BLAKE2b-256 6b4403ff253789da809afd78a0fb835209c6fe6b7e01b3beefb2fa621afb4732

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_shell_utilities-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e43bdbc0833558a1653fb5b4356594d02d3242c3bca71c3d6d2435fc5ca14d3
MD5 bd62f078d35a4951dc27a979002bfa56
BLAKE2b-256 d3af9e535a09a8a5156a33996438e9aa0aa565b0af7707f493525651ef543927

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