"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.
Installing pytest-shell-utilities
is as simple as:
And, that's honestly it.
Once installed, you can now use the shell
fixture to run some commands and assert against the outcome.
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.
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.",
]
)
The full documentation can be seen here.