|
| 1 | +--- |
| 2 | +description: Guidelines for using pytest with tmuxp, including libtmux fixtures |
| 3 | +globs: tests/**/test_*.py |
| 4 | +alwaysApply: true |
| 5 | +--- |
| 6 | + |
| 7 | +# tmuxp Pytest Guidelines |
| 8 | + |
| 9 | +## Leveraging libtmux fixtures |
| 10 | + |
| 11 | +tmuxp tests can utilize libtmux's pytest fixtures for fast, efficient tmux server, session, window, and pane setup/teardown. These fixtures automatically manage the lifecycle of tmux resources during tests. |
| 12 | + |
| 13 | +### Available fixtures |
| 14 | + |
| 15 | +The following fixtures are available through libtmux's pytest plugin: |
| 16 | + |
| 17 | +- `server`: Creates a temporary tmux server with isolated socket |
| 18 | +- `session`: Creates a temporary tmux session in the server |
| 19 | +- `window`: Creates a temporary tmux window in the session |
| 20 | +- `pane`: Creates a temporary tmux pane in the pane |
| 21 | +- `TestServer`: Factory for creating multiple independent servers with unique socket names |
| 22 | + |
| 23 | +### Usage in tests |
| 24 | + |
| 25 | +```python |
| 26 | +def test_something_with_server(server): |
| 27 | + # server is already running with proper configuration |
| 28 | + my_session = server.new_session("test-session") |
| 29 | + assert server.is_alive() |
| 30 | + assert my_session in server.sessions |
| 31 | + |
| 32 | +def test_something_with_session(session): |
| 33 | + # session is already created and configured |
| 34 | + new_window = session.new_window("test-window") |
| 35 | + assert new_window in session.windows |
| 36 | + |
| 37 | +def test_with_multiple_servers(TestServer): |
| 38 | + # Create independent servers for comparison tests |
| 39 | + Server1 = TestServer() |
| 40 | + Server2 = TestServer() |
| 41 | + |
| 42 | + server1 = Server1() |
| 43 | + server2 = Server2() |
| 44 | + |
| 45 | + session1 = server1.new_session() |
| 46 | + session2 = server2.new_session() |
| 47 | + |
| 48 | + assert server1.socket_path != server2.socket_path |
| 49 | +``` |
| 50 | + |
| 51 | +### Customizing session parameters |
| 52 | + |
| 53 | +You can override the `session_params` fixture to customize session creation: |
| 54 | + |
| 55 | +```python |
| 56 | +import pytest |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def session_params(): |
| 60 | + return { |
| 61 | + 'x': 800, |
| 62 | + 'y': 600, |
| 63 | + 'window_name': 'custom-window' |
| 64 | + } |
| 65 | +``` |
| 66 | + |
| 67 | +### Benefits |
| 68 | + |
| 69 | +- No need to manually set up and tear down tmux infrastructure |
| 70 | +- Tests run in isolated tmux environments |
| 71 | +- Faster test execution |
| 72 | +- Reliable test environment with predictable configuration |
| 73 | +- Multiple tests can run in parallel without tmux session conflicts |
| 74 | + |
| 75 | +For more details, see: |
| 76 | +- [libtmux pytest plugin documentation](https://libtmux.git-pull.com/pytest-plugin/index.html) |
| 77 | +- [libtmux pytest plugin code](https://github.com/tmux-python/libtmux/blob/master/src/libtmux/pytest_plugin.py) |
0 commit comments