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.
Release files for pytest-shell-utilities 1.9.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_shell_utilities-1.9.7.tar.gz | 81.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_shell_utilities-1.9.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 110.0 kB
Release files / pytest_shell_utilities-1.9.7.tar.gz
| Download URL | pytest_shell_utilities-1.9.7.tar.gz |
|---|---|
| Size | 81.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
84a2283333925b43780509ccfd3d5be0d7db243b7414b89f1b63954bbcc9d3ec
|
|
BLAKE2b-256 checksum How to use checksums |
f9de6b4060bef2b650f87e01c3b5f2d0ee41f0b49f4bd903253941959fac08d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / pytest_shell_utilities-1.9.7-py3-none-any.whl
| Download URL | pytest_shell_utilities-1.9.7-py3-none-any.whl |
|---|---|
| Size | 28.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e9975718160d9d35c715e0b349ddb78afb43c810ac5c98a6d0714462c0456eb6
|
|
BLAKE2b-256 checksum How to use checksums |
df3d09b2335b10f6a292a8c99907634655acbe0054850934574c904cab172a86
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|