A pytest plugin to help with testing shell scripts / black box commands
pip install pytest-shell==0.3.2
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.
Distributed under the terms of the MIT license, "pytest-shell" is free and open source software