Usage Guide¶
libtmux provides pytest fixtures for tmux. The plugin automatically manages setup and teardown of an independent tmux server.
See also
Using the pytest plugin?
If the fixture defaults do not fit your test suite, start a discussion with the use case before depending on undocumented behavior.
Usage¶
Install libtmux via the python package manager of your choosing, e.g.
$ pip install libtmux
The plugin is automatically detected by pytest, and the fixtures are added.
Real world usage¶
View libtmux’s own tests as well as tmuxp’s tests.
libtmux’s tests autouse the Recommended fixtures above to ensure stable test execution, assertions and
object lookups in the test grid.
pytest-driven tmux tests¶
pytest-tmux also works through pytest fixtures, so the same fixture concepts apply.
Request server or session as a test parameter to receive a fresh, headless
tmux(1) server or session. Derive windows and panes from the session;
the plugin does not register window or pane fixtures.
Recommended fixtures¶
Requesting server or session also creates their filesystem dependencies:
Creating temporary, test directories for:
Default
.tmux.confconfiguration with these settings (config_file):base-index -g 1
To make tmux read this configuration, set the temporary home directory as shown in Setting a temporary home directory, or pass
config_fileto a server you create.
Setting a tmux configuration¶
If you would like session to automatically use a configuration, you have a few
options:
Pass a
config_fileintoServerSet the
HOMEdirectory to a local or temporary pytest path with a configuration file
You could also read the code and override server in your own doctest.
Custom session parameters¶
You can override session_params to customize the session fixture. The
dictionary will directly pass into
Server.new_session() keyword arguments.
>>> import pytest
>>> @pytest.fixture
... def session_params() -> dict[str, int]:
... return {"x": 800, "y": 600}
The above will assure the libtmux session launches with -x 800 -y 600.
Creating temporary servers¶
If you need multiple independent tmux servers in your tests, the TestServer provides a factory that creates servers with unique socket names. Each server is automatically cleaned up when the test completes.
>>> temp_server = Server()
>>> temp_session = temp_server.new_session()
>>> temp_server.is_alive()
True
>>> temp_server.kill()
You can also use it with custom configurations, similar to the server fixture:
>>> config_path = request.getfixturevalue("tmp_path") / "tmux.conf"
>>> _ = config_path.write_text("set -g status off")
>>> configured_server = Server(config_file=str(config_path))
>>> _ = configured_server.new_session()
>>> configured_server.is_alive()
True
>>> configured_server.kill()
This is particularly useful when testing interactions between multiple tmux servers or when you need to verify behavior across server restarts.
Socket paths and the UNIX socket limit¶
A tmux socket is a UNIX domain socket, so its path is capped by sockaddr_un
— 107 bytes on Linux, 103 on macOS. pytest’s tmp_path is nested
deep by design (/tmp/pytest-of-<user>/pytest-<n>/<test-name><n>), so putting
a socket under it — directly, or by pointing TMUX_TMPDIR at it — can overrun
the limit on a long test name or a long temporary root. Server
measures an explicit socket_path as soon as it is passed and raises
SocketPathTooLong with the byte count, rather than letting
tmux report File name too long with only the path to go on:
>>> from libtmux import exc
>>> from libtmux.server import Server as TmuxServer
>>> deep_socket = "/tmp/" + "d" * 120 + "/sock"
>>> try:
... TmuxServer(socket_path=deep_socket)
... except exc.SocketPathTooLong as e:
... print(e.length)
130
A socket_name is different: tmux resolves it against $TMUX_TMPDIR when it
runs, so the length is only knowable at dispatch. Building the object is safe,
and a test that only asks whether a server is there gets an answer instead of an
exception — an unbindable address is one more way of not being alive:
The directory has to exist for that to be the socket tmux would bind: tmux takes
the first of $TMUX_TMPDIR and /tmp that resolves.
>>> from libtmux.server import Server as TmuxServer
>>> deep = request.getfixturevalue("tmp_path") / ("d" * 120)
>>> deep.mkdir()
>>> with monkeypatch.context() as m:
... m.delenv("TMUX", raising=False)
... m.setenv("TMUX_TMPDIR", str(deep))
... TmuxServer(socket_name="deep").is_alive()
False
The fixtures in this plugin sidestep it: server and TestServer name their sockets with socket_name, which
tmux resolves under its own short socket directory. In your own tests, keep
tmp_path for files and reach for tempfile.mkdtemp() — which gives a
short /tmp/<random> — when you need a socket path of your own, or point
TMUX_TMPDIR somewhere short.
Setting a temporary home directory¶
>>> import pathlib
>>> import pytest
>>> @pytest.fixture(autouse=True, scope="function")
... def set_home(
... monkeypatch: pytest.MonkeyPatch,
... user_path: pathlib.Path,
... ) -> None:
... monkeypatch.setenv("HOME", str(user_path))