A pytest plugin that enables tmux driven tests
Project description
pytest-tmux
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
fordev-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 updatetox.ini
- Update
pyproject.ini.j2
to updatepyproject.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_tmux-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d77ea38f821c8410c02ce05fa5ad35c9ca51eb0c1802c1e13b93881aff038e88 |
|
MD5 | 8ca20df5692d30719e55f0cd274b66c9 |
|
BLAKE2b-256 | 7f887aff157234220cdbbb1ace4149e0c23ad31efa03360a69c7ad573623d1c8 |