Skip to main content

A pytest plugin that enables tmux driven tests

Project description

pytest-tmux

PyPI version

Python versions

See Build Status on AppVeyor

A pytest plugin that enables tmux driven tests


Features

  • Enable tmux driven tests
  • Enable screen assertion
  • Enable row assertion

Requirements

  • python >= 3.7
  • python libtmux==0.16
  • pytest
  • tmux

Installation

You can install "pytest-tmux" via pip from PyPI:

$ pip install pytest-tmux

Purpose and design

This plugin is intend to help users who whant to test interrative cli. When using tmux fixture it basically :

  • create a tmux server (socket create in tmux tmpdir)
  • create a session automatically when requested with name based on the name of test file
  • create a window automatically when requested with name based on the name of the test

Configuration could be set on different level :

  • cli args (see --tmux-* switch with pytest --help)
  • at the test level with tmux_cfg marker
  • dynamically inside test with tmux.set()
  • using tmux.screen() / tmux.row() (timeout / delay)

Usage

Basic example

Success

Code

import pytest
from inspect import cleandoc

def test_assert(tmux):
    # Set some options before session / windows is started
    tmux.set(window_command='env -i PS1="$ " TERM="xterm-256color" /usr/bin/env bash --norc --noprofile')
    assert tmux.screen() == '$'
    tmux.send_keys(r'printf "  Hello World  .\n\n"')
    expected=r"""
    $ printf "  Hello World  .\n\n"
      Hello World  .

    $
    """
    assert tmux.screen() == cleandoc(expected)

Failure

Code

import pytest
from inspect import cleandoc

def test_assert(tmux):
    # Set some options before session / windows is started
    tmux.set(window_command='env -i PS1="# " TERM="xterm-256color" /usr/bin/env bash --norc --noprofile')
    assert tmux.row(0) == '$'

Output

>       assert tmux.row(0) == '$'
E       assert failed
E         > Common line
E         - Left
E         + Right
E         -------------
E         - #
E         + $
E         -------------

Waiting for a long process

import pytest
from inspect import cleandoc

def test_assert(tmux):
    # Set some options before session / windows is started
    tmux.set(window_command='env -i PS1="$ " TERM="xterm-256color" /usr/bin/env bash --norc --noprofile')
    assert tmux.row(0) == '$'
    tmux.send_keys('sleep 5')
    assert tmux.row(0) == '$ sleep 5'
    expected = """
    $ sleep 5
    $
    """
    assert tmux.screen(timeout=6, delay=0.5) == cleandoc(expected)

Debug

If needed, a debug mode is available with --tmux-debug. It will prompt you to :

  • open the tmux session for the current test
  • press enter to continue on :
    • send_keys
    • kill_session

Contributing

Contributions are very welcome.

Dev requirements

  • minor python3 versions present in .python-version
  • pyenv > v2.3.9 ( optional but recommended )
  • virtualenv (will be installed by make venv if not available)

Start hacking

Install python versions used for tests ( recommended )

pyenv install

/!\ Install multiple versions in a single command only available in pyenv > 2.3.9

Create the project virtualenv

$ make venv

This target does the following:

  • installs virtualenv if not found
  • creates a venv in .venv for dev-requirements.txt
  • installs dev-requirements.txt
  • creates a .python-venv symlink to .venv/bin/activate

Run tests

Tests are driven by tox / make

Run with make

Example:

$ make test

Run with tox or free commands

Example:

$ source .python-venv
$ tox
$ poetry run pytest tests/

Formater

black and isort run in check mode by default

check mode could be removed by running the env as :

$ source .python-venv
$ tox -e black --
$ tox -e isort --

Update tox.ini / pyproject.toml

  • Update tox.ini.j2 to update tox.ini
  • Update pyproject.ini.j2 to update pyproject.ini
  • Update .python-version to add a new python version (order is important)

License

Distributed under the terms of the MIT license, "pytest-tmux" is free and open source software

Issues

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

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_tmux-0.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_tmux-0.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_tmux-0.0.1.tar.gz.

File metadata

  • Download URL: pytest_tmux-0.0.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.7.16 Linux/4.15.0-197-generic

File hashes

Hashes for pytest_tmux-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1b44d72cf05432c8fa5d0be6d2fe8f286168653c823eb0eba2b2d2bb54216064
MD5 a7f60da25cd15417b136d3d07d235c3e
BLAKE2b-256 91e0e82b52798b8dc180ad17fc5987b3badb9a521b06d9df6df3e6ce65f383b3

See more details on using hashes here.

File details

Details for the file pytest_tmux-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_tmux-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.7.16 Linux/4.15.0-197-generic

File hashes

Hashes for pytest_tmux-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d77ea38f821c8410c02ce05fa5ad35c9ca51eb0c1802c1e13b93881aff038e88
MD5 8ca20df5692d30719e55f0cd274b66c9
BLAKE2b-256 7f887aff157234220cdbbb1ace4149e0c23ad31efa03360a69c7ad573623d1c8

See more details on using hashes here.

Supported by

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