
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
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.
You can install "pytest-shell" via pip
_ from PyPI
_::
$ pip install pytest-shell
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.
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'
Distributed under the terms of the MIT
_ license, "pytest-shell" is free and
open source software
.. _Cookiecutter
: https://github.com/audreyr/cookiecutter
.. _@hackebrot
: https://github.com/hackebrot
.. _MIT
: http://opensource.org/licenses/MIT
.. _BSD-3
: http://opensource.org/licenses/BSD-3-Clause
.. _GNU GPL v3.0
: http://www.gnu.org/licenses/gpl-3.0.txt
.. _Apache Software License 2.0
: http://www.apache.org/licenses/LICENSE-2.0
.. _cookiecutter-pytest-plugin
: https://github.com/pytest-dev/cookiecutter-pytest-plugin
.. _file an issue
: https://github.com/{{cookiecutter.github_username}}/pytest-{{cookiecutter.plugin_name}}/issues
.. _pytest
: https://github.com/pytest-dev/pytest
.. _tox
: https://tox.readthedocs.io/en/latest/
.. _pip
: https://pypi.org/project/pip/
.. _PyPI
: https://pypi.org/project
FAQs
A pytest plugin to help with testing shell scripts / black box commands
We found that pytest-shell demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.