Skip to main content

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

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.


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.

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.1.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

pytest_shell-0.1.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-shell-0.1.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.15rc1

File hashes

Hashes for pytest-shell-0.1.1.tar.gz
Algorithm Hash digest
SHA256 13433a4fc8494518aa2dabb75d4a27e53bce6f9e2cfffa933e62627f27a1044b
MD5 5fc17b182d69a5e37df00c6182ac74b2
BLAKE2b-256 3475a560a6086ef5a73d8f7c2f3789f5483f7db5f62cf55b0e53d9fd3bb41196

See more details on using hashes here.

File details

Details for the file pytest_shell-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_shell-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.15rc1

File hashes

Hashes for pytest_shell-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f669bf6fd17055457bffb32a8ae17bf0eb787e926201b5b17215a8afb761ccc9
MD5 e5c817a5cdf87aa5c7f7fe2543b20be7
BLAKE2b-256 a871530e9b491ed3f29bcd35085d2e89b007c24cd78162814112ce9166d7ee78

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