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.0.2.tar.gz
.
File metadata
- Download URL: pytest-shell-utilities-1.0.2.tar.gz
- Upload date:
- Size: 25.7 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cc0c3f9a8efb8e79f247e959d6a5dbb6e07469c1a8bfe8ba90de98a0b22e473 |
|
MD5 | 89e1ca328805f99ed842bf9b8997b3b3 |
|
BLAKE2b-256 | f6f6af5ca7baeb5e3cf1784a78b4b4b0d211ba8cecf2940299db70a73627f306 |
File details
Details for the file pytest_shell_utilities-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: pytest_shell_utilities-1.0.2-py3-none-any.whl
- Upload date:
- Size: 45.0 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b0d4f65e3b17e36afea4f172b92a1c9cb6dcde67a98602649b26344a029d5ad |
|
MD5 | 8d9fba22f40fd1134ae78f5920f8f4b0 |
|
BLAKE2b-256 | 4dba89772039a296b41dadb4b81133a90c1bb2006221c3a997bde418becbc255 |