Debian Codegraph @

docs/api/testing/pytest-plugin/usage.md

raw symbols SCIP
1 (pytest_plugin_usage)=
2
3 # Usage Guide
4
5 libtmux provides [pytest] fixtures for tmux. The plugin automatically manages
6 setup and teardown of an independent tmux server.
7
8 ```{seealso} Using the pytest plugin?
9
10 If the fixture defaults do not fit your test suite, [start a discussion] with
11 the use case before depending on undocumented behavior.
12
13 [start a discussion]: https://github.com/tmux-python/libtmux/discussions
14 ```
15
16 ## Usage
17
18 Install `libtmux` via the python package manager of your choosing, e.g.
19
20 ```console
21 $ pip install libtmux
22 ```
23
24 The plugin is automatically detected by [pytest], and the fixtures are added.
25
26 ### Real world usage
27
28 View libtmux's own [tests](https://github.com/tmux-python/libtmux/tree/master/tests)
29 as well as [tmuxp]'s
30 [tests](https://github.com/tmux-python/tmuxp/tree/master/tests).
31
32 libtmux's tests `autouse` the {ref}`recommended-fixtures` above to ensure stable test execution, assertions and
33 object lookups in the test grid.
34
35 ## pytest-driven tmux tests
36
37 [pytest-tmux] also works through {ref}`pytest fixtures <pytest:fixtures-api>`,
38 so the same fixture concepts apply.
39
40 The plugin's fixtures guarantee a fresh, headless {command}`tmux(1)` server, session, window, or pane is
41 passed into your test.
42
43 (recommended-fixtures)=
44
45 ## Recommended fixtures
46
47 These fixtures are automatically used when the plugin is enabled and `pytest` is run.
48
49 - Creating temporary, test directories for:
50 - `/home/` ({fixture}`home_path`)
51 - `/home/${user}` ({fixture}`user_path`)
52 - Default `.tmux.conf` configuration with these settings ({fixture}`config_file`):
53
54 - `base-index -g 1`
55
56 These are set to ensure panes and windows can be reliably referenced and asserted.
57
58 (setting_a_tmux_configuration)=
59
60 ## Setting a tmux configuration
61
62 If you would like {fixture}`session <libtmux.pytest_plugin.session>` to automatically use a configuration, you have a few
63 options:
64
65 - Pass a `config_file` into {class}`~libtmux.Server`
66 - Set the `HOME` directory to a local or temporary pytest path with a configuration file
67
68 You could also read the code and override {fixture}`server <libtmux.pytest_plugin.server>` in your own doctest.
69
70 (custom_session_params)=
71
72 ### Custom session parameters
73
74 You can override {fixture}`session_params` to customize the `session` fixture. The
75 dictionary will directly pass into
76 {meth}`Server.new_session() <libtmux.Server.new_session>` keyword arguments.
77
78 ```python
79 >>> import pytest
80 >>> @pytest.fixture
81 ... def session_params() -> dict[str, int]:
82 ... return {"x": 800, "y": 600}
83 ```
84
85 The above will assure the libtmux session launches with `-x 800 -y 600`.
86
87 (temp_server)=
88
89 ### Creating temporary servers
90
91 If you need multiple independent tmux servers in your tests, the {fixture}`TestServer <libtmux.pytest_plugin.TestServer>` provides a factory that creates servers with unique socket names. Each server is automatically cleaned up when the test completes.
92
93 ```python
94 >>> temp_server = Server()
95 >>> temp_session = temp_server.new_session()
96 >>> temp_server.is_alive()
97 True
98 >>> temp_server.kill()
99 ```
100
101 You can also use it with custom configurations, similar to the {ref}`server fixture <setting_a_tmux_configuration>`:
102
103 ```python
104 >>> config_path = request.getfixturevalue("tmp_path") / "tmux.conf"
105 >>> _ = config_path.write_text("set -g status off")
106 >>> configured_server = Server(config_file=str(config_path))
107 >>> _ = configured_server.new_session()
108 >>> configured_server.is_alive()
109 True
110 >>> configured_server.kill()
111 ```
112
113 This is particularly useful when testing interactions between multiple tmux servers or when you need to verify behavior across server restarts.
114
115 (set_home)=
116
117 ### Setting a temporary home directory
118
119 ```python
120 >>> import pathlib
121 >>> import pytest
122 >>> @pytest.fixture(autouse=True, scope="function")
123 ... def set_home(
124 ... monkeypatch: pytest.MonkeyPatch,
125 ... user_path: pathlib.Path,
126 ... ) -> None:
127 ... monkeypatch.setenv("HOME", str(user_path))
128 ```
129
130 [pytest]: https://docs.pytest.org/en/stable/
131 [pytest-tmux]: https://pytest-tmux.readthedocs.io/
132 [tmuxp]: https://tmuxp.git-pull.com/