Skip to content

Simple pytest(pytest.org) plugin which provides fixtures and code to help with running shell commands on tests

License

Notifications You must be signed in to change notification settings

s0undt3ch/pytest-shell-utilities

 
 

Repository files navigation

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.

About

Simple pytest(pytest.org) plugin which provides fixtures and code to help with running shell commands on tests

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%