|
| 1 | +(pytest_plugin)= |
| 2 | + |
| 3 | +# `pytest` plugin |
| 4 | + |
| 5 | +Testing tmux with libtmux |
| 6 | + |
| 7 | +```{seealso} Using libtmux? |
| 8 | +
|
| 9 | +Do you want more flexbility? Correctness? Power? Defaults changed? [Connect with us] on the tracker, we want to know |
| 10 | +your case, we won't stabilize APIs until we're sure everything is by the book. |
| 11 | +
|
| 12 | +[connect with us]: https://github.com/tmux-python/libtmux/discussions |
| 13 | +
|
| 14 | +``` |
| 15 | + |
| 16 | +```{module} libtmux.pytest_plugin |
| 17 | +
|
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Install `libtmux` via the python package manager of your choosing, e.g. |
| 23 | + |
| 24 | +```console |
| 25 | +$ pip install libtmux |
| 26 | +``` |
| 27 | + |
| 28 | +The pytest plugin will automatically be detected via pytest, and the fixtures will be added. |
| 29 | + |
| 30 | +## Fixtures |
| 31 | + |
| 32 | +`pytest-tmux` works through providing {ref}`pytest fixtures <pytest:fixtures-api>` - so read up on |
| 33 | +those! |
| 34 | + |
| 35 | +The plugin's fixtures guarantee a fresh, headless `tmux(1)` server, session, window, or pane is |
| 36 | +passed into your test. |
| 37 | + |
| 38 | +(recommended-fixtures)= |
| 39 | + |
| 40 | +## Recommended fixtures |
| 41 | + |
| 42 | +These are fixtures are automatically used when the plugin is enabled and `pytest` is ran. |
| 43 | + |
| 44 | +- Creating temporary, test directories for: |
| 45 | + - `/home/` ({func}`home_path`) |
| 46 | + - `/home/${user}` ({func}`user_path`) |
| 47 | +- Default `.tmux.conf` configuration with these settings ({func}`config_file`): |
| 48 | + |
| 49 | + - `base-index -g 1` |
| 50 | + |
| 51 | + These are set to ensure panes and windows can be reliably referenced and asserted. |
| 52 | + |
| 53 | +## Setting a tmux configuration |
| 54 | + |
| 55 | +If you would like :func:`session` fixture to automatically use a configuration, you have a few |
| 56 | +options: |
| 57 | + |
| 58 | +- Pass a `config_file` into :class:`libtmux.server.Server` |
| 59 | +- Set the `HOME` directory to a local or temporary pytest path with a configurat configuration file |
| 60 | + |
| 61 | +You could also read the code and override :func:`server`'s fixture in your own doctest. doctest. |
| 62 | + |
| 63 | +(set_home)= |
| 64 | + |
| 65 | +### Setting a temporary home directory |
| 66 | + |
| 67 | +```python |
| 68 | +import pathlib |
| 69 | +import pytest |
| 70 | + |
| 71 | +@pytest.fixture(autouse=True, scope="function") |
| 72 | +def set_home( |
| 73 | + monkeypatch: pytest.MonkeyPatch, |
| 74 | + user_path: pathlib.Path, |
| 75 | +): |
| 76 | + monkeypatch.setenv("HOME", str(user_path)) |
| 77 | +``` |
| 78 | + |
| 79 | +## See examples |
| 80 | + |
| 81 | +View libtmux's own [tests/](https://github.com/tmux-python/libtmux/tree/master/tests) as well as |
| 82 | +tmuxp's [tests/](https://github.com/tmux-python/tmuxp/tree/master/tests). |
| 83 | + |
| 84 | +libtmux's tests `autouse` the {ref}`recommended-fixtures` above to ensure stable, assertions and |
| 85 | +object lookups in the test grid. |
| 86 | + |
| 87 | +## API reference |
| 88 | + |
| 89 | +```{eval-rst} |
| 90 | +.. autoapimodule:: libtmux.pytest_plugin |
| 91 | + :members: |
| 92 | + :inherited-members: |
| 93 | + :private-members: |
| 94 | + :show-inheritance: |
| 95 | + :member-order: bysource |
| 96 | + :exclude-members: Server, TEST_SESSION_PREFIX, get_test_session_name, |
| 97 | + namer, logger |
| 98 | +``` |
0 commit comments