pytest-pexpect

Pytest pexpect plugin.


License
Other
Install
pip install pytest-pexpect==0.1.0

Documentation

pytest-pexpect

Introduction

The pytest-pexpect plugin aims to streamline CLI testing, a common practice in network and system-related software development and administration tasks. CLI testing encompasses various types of tests, including unit, integration, system, and acceptance tests.

Pytest is a well-known Python test framework with rich features and flexibility.

Pexpect is a Python Expect-like library specifically designed to simulate user CLI interactions, facilitating automated testing of command-line interfaces.

Combining Pytest with Pexpect brings significant advantages to testing CLI applications. With the pytest-pexpect plugin, you can harness the power of Pytest’s assertions, fixtures, and parametrization alongside Pexpect’s CLI interaction capabilities for seamless and efficient testing of CLI applications. In addition to pexpect, the plugin adds support for forbidden patterns.

Distributed under the terms of the MIT license, pytest-pexpect is free and open source software, see LICNESE file.

For history and releases see CHANGES.adoc file.

Installation

pip3 installation

You can install pytest-pexpect plugin with pip3. Currently, the package is not hosted at https://pypi.org/ and you have to pass directory of the repository.

Clone repository and install the package.
git clone https://github.com/novakmi/pytest-pexpect.git
pip3 install pytest-pexpect/
ℹ️
Or pip3 install . from pytest-pexpect directory.
Uninstall plugin.
pip3 uninstall pytest-pexpect

PYTHONPATH installation

Set PYTHONPATH to pytest-pexpect/src directory (e.g. export PYTHONPATH=~/pytest-pexpect/src:$PYTHONPATH).

Add into your conftest.py line

pytest_plugins: str = ("pytest_pexpect")

or add into pytest.ini lines

[pytest]
addopts = -p pytest_pexpect

Verify installation

pytest --trace-config`

Parameters

--pexpect-dry-run - if added to command line, pexpect commands (sendline, expect ) are not performed

Quick start

Use pexpect_shell fixture
from pytest_pexpect import ShellParams

@pytest.mark.parametrize("shell", [ShellParams()])
def test_pexpect_object(pexpect_shell):
    pexpect_shell.sendline('echo "Hello pexpect"')
    pexpect_shell.expect('Hello pexpect', timeout=2)
    pexpect_shell.expect_prompt()

The log directory is created with output log files.

User guide

TODO (See src and tests directory for now)

Plugin tests

Run full tox tests

tox

Run full tox flake8 tests

tox -e flake8

Run pytest tests

pytest -o log_cli=true -o log_cli_level=WARNING  tests/

Planned features

  • improve code documentation

  • examples

  • pipy.org package

  • support for test documentation (record sendline and expect commands)

Similar projects

Lux (LUcid eXpect scripting) https://github.com/hawk/lux