Skip to main content

A pytest plugin for testing shell scripts and line-based processes

Project description

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.

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'

chroot helper

A context manager that creates a chroot environment is available through the bash fixture:

with bash.chroot(tmpdir):
    bash.send('mkdir /blah')

The only reason to use this is if you need to test something that relies on a certain path structure anchored at root. It is fairly flimsy, and of course requires sudo. The best way to use it is with the tmpdir fixture provided by pytest as no cleanup is done.

/bin, /usr/bin, /lib and /lib64 are mounted into the chroot using a bind mount, otherwise most things don’t work. The chroot commands is called with –userspec for the current user so the commands you run in there won’t be run as root, but nevertheless be careful and aware that the directories mounted in there are your real system directories. There is no safety or security, it’s just a helper to test something that would be otherwise hard to test.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-shell-0.2.3.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

pytest_shell-0.2.3-py2.py3-none-any.whl (14.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-shell-0.2.3.tar.gz.

File metadata

  • Download URL: pytest-shell-0.2.3.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.3 Linux/5.0.0-37-generic

File hashes

Hashes for pytest-shell-0.2.3.tar.gz
Algorithm Hash digest
SHA256 535178a527450371defbc00e542511300b6a8e3199abe537b31aae6eb3c94ded
MD5 610ab1d5773b9dafb59e9b96b4793ffd
BLAKE2b-256 adae7f4dfcab9b74e272674315f4b9141185d2a9072569fa334dd1facebb2234

See more details on using hashes here.

File details

Details for the file pytest_shell-0.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_shell-0.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.3 Linux/5.0.0-37-generic

File hashes

Hashes for pytest_shell-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aa396d5e8f4b3faa20fc28b66abe088139bef1c02a5d0674a5a2f2aacf778b1a
MD5 74b44d6b16cb30c11855905cb004e6a1
BLAKE2b-256 ea2448bbd36e37f6c92ef81ab7cc4f4203688c9e1bd7ba66f94674faba6c22b2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page