Skip to main content

Pytest plugin for testing console scripts

Project description

See Build Status on Travis CI

Pytest-console-scripts is a Pytest plugin for testing python scripts installed via console_scripts entry point of setup.py. It can run the scripts under test in a separate process or using the interpreter that’s running the test suite. The former mode ensures that the script will run in an environment that is identical to normal execution whereas the latter one allows much quicker test runs during development while simulating the real runs as much as possible.

Requirements

  • Python 2.7 or 3.4+, or PyPy,

  • Pytest 3.0 or newer.

Installation

You can install “pytest-console-scripts” via pip from PyPI:

$ pip install pytest-console-scripts

Usage

Imagine we have a python package foo with the following setup.py:

setup(
    name='foo',
    version='0.0.1',
    py_modules=['foo'],
    entry_points={
        'console_scripts': ['foobar=foo:bar']
    },
)

We could use pytest-console-scripts to test the foo script:

def test_foo_bar(script_runner):
    ret = script_runner.run('foo', 'bar')
    assert ret.success
    assert ret.stdout == 'bar\n'
    assert ret.stderr == ''

This would use the script_runner fixture provided by the plugin to run the script and capture it’s output.

The arguments of script_runner.run are the command name of the script and any command line arguments that should be passed to it. Additionally the following keyword arguments can be used:

  • cwd - set the working directory of the script under test.

  • stdin - a file-like object that will be piped to standard input of the script.

Configuring script execution mode

In the example above the foobar script would run in in-process mode. This is fast and good for quick iteration during development. After we’re happy with the functionality, it’s time to run the script in subprocess mode to simulate real invocation more closely. There are several ways to do this. We can configure it via pytest configuration (for example in tox.ini):

[pytest]
script_launch_mode = subprocess

We can give a command line option to pytest (this will override the configuration file):

$ py.test --script-launch-mode=subprocess test_foobar.py

We can also mark individual tests to run in a specific mode:

@pytest.mark.script_launch_mode('subprocess')
def test_foobar(script_runner):
    ...

Between these three methods the marking of the tests has priority before the command line option that in turn overrides the configuration setting. All three can take three possible values: “inprocess” (which is the default), “subprocess”, and “both” (which will cause the test to be run twice: in inprocess and in subprocess modes).

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-console-scripts” is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.


Pytest-console-scripts was initially generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.

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-console-scripts-0.1.3.tar.gz (5.2 kB view hashes)

Uploaded Source

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