pytest-shell
A plugin for testing shell scripts and line-based processes with pytest.
You could use it to test shell scripts, or other commands that can be run through the shell that you want to test the usage of.
Not especially feature-complete or even well-tested, but works for what I wanted it for. If you use it please feel free to file bug reports or feature requests.
This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.
Features
Easy access to a bash shell through a pytest fixture.
Set and check environment variables through Python code.
Automatically fail test on nonzero return codes by default.
Helpers for running shell scripts.
Mostly, all the great stuff pytest gives you with a few helpers to make it work for bash.
Installation
You can install “pytest-shell” via pip from PyPI:
$ pip install pytest-shell
Usage
You can use a fixture called ‘bash’ to get a shell process you can interact with.
Test a bash function:
def test_something(bash):
assert bash.run_function('test') == 'expected output'
Set environment variables, run a .sh file and check results:
def test_something(bash):
with bash(envvars={'SOMEDIR': '/home/blah'}) as s:
s.run_script('dostuff.sh', ['arg1', 'arg2'])
assert s.path_exists('/home/blah/newdir')
assert s.file_contents('/home/blah/newdir/test.txt') == 'test text'
Run some inline script, check an environment variable was set:
def test_something(bash):
bash.run_script_inline(['touch /tmp/blah.txt', './another_script.sh'])
assert bash.envvars.get('AVAR') == 'success'
Use context manager to set environment variables:
def test_something(bash):
with bash(envvars={'BLAH2': 'something'}):
assert bash.envvars['BLAH2'] == 'something'
You can run things other than bash (ssh for example), but there aren’t specific fixtures and the communication with the process is very bash-specific.
Creating file and directory structures
pytest_shell.fs.create_files() is a helper to assemble a structure of files and directories. It is best used with the tmpdir pytest fixture so you don’t have to clean up. It is used like so:
structure = ['/a/directory',
{'/a/directory/and/a/file.txt': {'content': 'blah'}},
{'/a/directory/and': {'mode': 0o600}]
create_files(structure)
which should create something like this:
|
+ a
\
+ directory
\
+ and # mode 600
\
+ a
\
file.txt # content equal to 'blah'
TODO
Helpers for piping, streaming.
Fixtures and helpers for docker and ssh.
Support for non-bash shells.
Shell instance in setup for e.g. basepath.
Refactoring TODO
Make Connection class just handle bytes, move line-based stuff into an intermediary.
Make pattern stuff work line-based or on multiline streams (in a more obvious way than just crafting the right regexes).
Make pattern stuff work on part of line if desired, leaving the rest.
License
Distributed under the terms of the MIT license, “pytest-shell” is free and open source software
Metadata
Release files for pytest-shell 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest-shell-0.3.2.tar.gz | 11.7 kB | Without provenance | Details for pytest-shell-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_shell-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for pytest_shell-0.3.2-py3-none-any.whl |
Total release size: 22.8 kB
Release files / pytest-shell-0.3.2.tar.gz
| Download URL | pytest-shell-0.3.2.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e30cf518a5271328f25eaa9013e1639607f169d7396a4b85204a8b34dacbab1
|
|
BLAKE2b-256 checksum How to use checksums |
5de740d496aaa6a2cc1f95d88ee05d5837097c0ddf57c7c8297a1c6242940b9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.6 Linux/5.10.103
|
Release files / pytest_shell-0.3.2-py3-none-any.whl
| Download URL | pytest_shell-0.3.2-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f60716134fc30e2c1fe9facb9fe60121d18b4d7b95e692b2c6f29271350aa12b
|
|
BLAKE2b-256 checksum How to use checksums |
7787cc96d1964173108a7d78827db0a4806c59a923f8dbc146ef319fd7d19ae1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.6 Linux/5.10.103
|
Security for pytest-shell 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.