Skip to content

client

TmuxClient(request, pytestconfig, tmpdir_factory=None, server=None, server_cfg_fixture=None, session_cfg_fixture=None, assertion_cfg_fixture=None)

When instantiated
  • create/stores link to :
    • libtmux.server.Server instance
    • libtmux.session.Session instance
    • libtmux.window.Window instance
    • libtmux.pane.Pane instance
  • create some methods specific to pytest-tmux
  • create a pytest_tmux.config.TmuxConfig instance

Parameters:

Name Type Description Default
request pytest.FixtureRequest

a pytest request fixture object

required
pytestconfig pytest.Config

a pytest pytestconfig fixture object

required
tmpdir_factory Optional[pytest.TempdirFactory]

a pytest tmpdir_factory fixture object

None
server Optional[TmuxServer]

a libtmux.server.server object

None
server_cfg_fixture Optional[Dict[str, Union[str, int]]]

a server config dictionary

None
session_cfg_fixture Optional[Dict[str, Union[str, int]]]

a session config dictionary

None
assertion_cfg_fixture Optional[Dict[str, Union[str, int]]]

a assertion config dictionary

None
Source code in pytest_tmux/client.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(
    self,
    request: pytest.FixtureRequest,
    pytestconfig: pytest.Config,
    tmpdir_factory: Optional[pytest.TempdirFactory] = None,
    server: Optional[TmuxServer] = None,
    server_cfg_fixture: Optional[Dict[str, Union[str, int]]] = None,
    session_cfg_fixture: Optional[Dict[str, Union[str, int]]] = None,
    assertion_cfg_fixture: Optional[Dict[str, Union[str, int]]] = None,
) -> None:
    """State"""
    self._server = server
    self._session = None  # type: Optional[ libtmux.session.Session ]
    self._window = None  # type: Optional[ libtmux.window.Window ]
    self._pane = None  # type: Optional[ libtmux.pane.Pane ]
    self._debug = None  # type: Optional[ bool ]
    self._interrupted = False
    self.sessions = 0

    if server is None and tmpdir_factory is None:
        raise ValueError("tmpdir_factory is requiered if server is not provided")

    """ Configuration """
    self._request = request
    self._pytestconfig = pytestconfig
    self._tmpdir_factory = tmpdir_factory
    self._server_cfg_fixture = server_cfg_fixture
    self._session_cfg_fixture = session_cfg_fixture
    self._assertion_cfg_fixture = assertion_cfg_fixture

    self.config = TmuxConfig(
        request=self._request,
        pytestconfig=self._pytestconfig,
        tmpdir_factory=self._tmpdir_factory,
        server_cfg_fixture=self._server_cfg_fixture,
        session_cfg_fixture=self._session_cfg_fixture,
        assertion_cfg_fixture=self._assertion_cfg_fixture,
    )

pane: Optional[libtmux.pane.Pane] property

A direct link to libtmux.pane.Pane created for the actual test.

The object is created on the first call who need it.

Returns:

Type Description
Optional[libtmux.pane.Pane]

a libtmux.pane.Pane object

server: libtmux.server.Server property

A direct link to libtmux.server.Server created for the actual test session.

The object is created on the first call who need it.

Returns:

Type Description
libtmux.server.Server

a libtmux.server.Server object

session: libtmux.session.Session property

A direct link to libtmux.session.Session created for the actual test.

The object is created on the first call who need it.

Returns:

Type Description
libtmux.session.Session

a libtmux.session.Session object

window: libtmux.window.Window property

A direct link to libtmux.window.Window created for the actual test.

The object is created on the first call who need it.

Returns:

Type Description
libtmux.window.Window

a libtmux.window.Window object

clear()

Shortcut for libtmux.pane.Pane.clear()

Source code in pytest_tmux/client.py
225
226
227
228
229
230
def clear(self) -> None:
    """
    Shortcut for libtmux.pane.Pane.clear()
    """
    assert isinstance(self.pane, TmuxPane)
    self.pane.clear()

debug(msg)

Display a message and ask to press enter when pytest-tmux debug is activated.

On first call, display a command who let the user been attached to the test session.

Parameters:

Name Type Description Default
msg str

The message to display

required
Source code in pytest_tmux/client.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def debug(self, msg: str) -> None:
    """
    Display a message and ask to press enter when pytest-tmux debug is
    activated.

    On first call, display a command who let the user been attached to the
    test session.

    Args:
        msg:    The message to display
    """
    if TYPE_CHECKING:
        assert isinstance(self.config, TmuxConfig)
        assert isinstance(self.config.plugin, TmuxConfigPlugin)
    if self.config.plugin.debug:
        try:
            if self._debug is None:
                with self.suspend_capture(self._request):
                    assert isinstance(self.pane, TmuxPane)
                    print("")
                    print("")
                    print(
                        cleandoc(
                            """
                        pytest-tmux started with DEBUG

                        ****************************************************************************************
                        * Open a new window terminal and use the bellow command to connect to the tmux session *
                        ****************************************************************************************

                        tmux -S "{}" attach -t "{}" \\; setw force-width {} \\; setw force-height {}

                        """.format(
                                self.server.socket_path,
                                self.config.session.session_name,
                                self.pane.display_message(
                                    "#{window_width}", get_text=True
                                )[0],
                                self.pane.display_message(
                                    "#{window_height}", get_text=True
                                )[0],
                            )
                        )
                    )
                    print("")
                    self._debug = True
            with self.suspend_capture(self._request, self._interrupted):
                print("")
                print(cleandoc(msg))
        except KeyboardInterrupt:
            self._interrupted = True
            Exit("CTRL+C detected.")

row(row, timeout=None, delay=None)

Get row content from pane with retry capability on operators

Parameters:

Name Type Description Default
row int

which row from libtmux.pane.Pane.capture_pane to set in TmuxOutput

required
timeout Optional[int]

how long to wait for the assertion to failed

None
delay Optional[Union[int, float]]

how long before retrying the assertion

None

Returns:

Type Description
TmuxOutput

a TmuxOutput instance

Source code in pytest_tmux/client.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
def row(
    self,
    row: int,
    timeout: Optional[int] = None,
    delay: Optional[Union[int, float]] = None,
) -> TmuxOutput:
    """
    Get row content from pane with retry capability on operators

    Args:
        row: which row from libtmux.pane.Pane.capture_pane to set in [TmuxOutput][pytest_tmux.output.TmuxOutput]
        timeout: how long to wait for the assertion to failed
        delay: how long before retrying the assertion

    Returns:
        a [TmuxOutput][pytest_tmux.output.TmuxOutput] instance
    """
    if TYPE_CHECKING:
        assert isinstance(self.config, TmuxConfig)
        assert isinstance(self.config.server, TmuxConfigAssert)

    if not isinstance(row, int):
        raise TypeError("row should be an integer")

    if timeout is None:
        timeout = self.config.assertion.timeout
    if delay is None:
        delay = self.config.assertion.delay

    self.debug(
        f"""
        Check tmux row {row}
        """
    )

    def _capture() -> str:
        assert isinstance(self.pane, TmuxPane)
        try:
            output = self.pane.capture_pane()[row]
        except IndexError:
            output = ""
        return output

    return TmuxOutput(_capture, timeout=timeout, delay=delay)

screen(timeout=None, delay=None)

Get screen content from pane with retry capability on operators

Parameters:

Name Type Description Default
timeout Optional[int]

how long to wait for the assertion to failed

None
delay Optional[Union[int, float]]

how long before retrying the assertion

None

Returns:

Type Description
TmuxOutput

a TmuxOutput instance

Source code in pytest_tmux/client.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def screen(
    self, timeout: Optional[int] = None, delay: Optional[Union[int, float]] = None
) -> TmuxOutput:
    """
    Get screen content from pane with retry capability on operators

    Args:
        timeout: how long to wait for the assertion to failed
        delay: how long before retrying the assertion

    Returns:
        a [TmuxOutput][pytest_tmux.output.TmuxOutput] instance
    """

    if TYPE_CHECKING:
        assert isinstance(self.config, TmuxConfig)
        assert isinstance(self.config.server, TmuxConfigAssert)
    if timeout is None:
        timeout = self.config.assertion.timeout
    if delay is None:
        delay = self.config.assertion.delay

    self.debug(
        """
        Check tmux screen
        """
    )

    def _capture() -> str:
        assert isinstance(self.pane, TmuxPane)
        return "\n".join(self.pane.capture_pane())

    return TmuxOutput(_capture, timeout=timeout, delay=delay)

send_keys(cmd, **kwargs)

Send commands to the actual pane

Parameters:

Name Type Description Default
cmd str

Text or input into pane

required
kwargs Any

every arguments accepted by libtmux.pane.Pane.send_keys()

{}
Source code in pytest_tmux/client.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def send_keys(self, cmd: str, **kwargs: Any) -> None:
    """
    Send commands to the actual pane

    Args:
        cmd: Text or input into pane
        kwargs: every arguments accepted by libtmux.pane.Pane.send_keys()
    """
    if "suppress_history" not in kwargs:
        kwargs["suppress_history"] = False
    self.debug(
        """
                Send "{}" to tmux session
                """.format(
            cmd
        )
    )
    assert isinstance(self.pane, TmuxPane)
    self.pane.send_keys(cmd, **kwargs)