Pytest plugin to simplify running shell commands against the system
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest_shell_utilities-1.9.6.tar.gz
.
File metadata
- Download URL: pytest_shell_utilities-1.9.6.tar.gz
- Upload date:
- Size: 81.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f9f5eafdd7e5c9953a1b5850c2aba26f3840b4a093a323338da687bfff027ab |
|
MD5 | 8d9f491c4faf2e8bd086ff85c27ff0ba |
|
BLAKE2b-256 | 9de39bb9ebf5f9566ea02e5e822b71fd7acf2dee5b68cc81329f30a73299d928 |
File details
Details for the file pytest_shell_utilities-1.9.6-py3-none-any.whl
.
File metadata
- Download URL: pytest_shell_utilities-1.9.6-py3-none-any.whl
- Upload date:
- Size: 28.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5691065bb7988d9f9cd7e40837cc70e70f58cb4306a71aee51872df592d4891 |
|
MD5 | dffb45483882aa87355d4272a931c9bf |
|
BLAKE2b-256 | 221c58bea3603cd108ca270e92920c6d6643bab1a92a5ea0692ceb2c3087169f |