|  | 
|  | 1 | +--- | 
|  | 2 | +myst: | 
|  | 3 | +  html_meta: | 
|  | 4 | +    description: "Core fixtures provided by the libtmux pytest plugin for tmux testing" | 
|  | 5 | +    keywords: "tmux, pytest, fixture, testing, server, session, window, pane" | 
|  | 6 | +--- | 
|  | 7 | + | 
|  | 8 | +(fixtures)= | 
|  | 9 | + | 
|  | 10 | +# Fixtures | 
|  | 11 | + | 
|  | 12 | +The libtmux pytest plugin provides several fixtures to help you test tmux-related functionality. These fixtures handle the setup and teardown of tmux resources automatically. | 
|  | 13 | + | 
|  | 14 | +## Core fixtures | 
|  | 15 | + | 
|  | 16 | +### Server fixture | 
|  | 17 | + | 
|  | 18 | +```{eval-rst} | 
|  | 19 | +.. autofunction:: libtmux.pytest_plugin.server | 
|  | 20 | +``` | 
|  | 21 | + | 
|  | 22 | +Example usage: | 
|  | 23 | + | 
|  | 24 | +```python | 
|  | 25 | +def test_server_functions(server): | 
|  | 26 | +    """Test basic server functions.""" | 
|  | 27 | +    assert server.is_alive() | 
|  | 28 | +     | 
|  | 29 | +    # Create a new session | 
|  | 30 | +    session = server.new_session(session_name="test-session") | 
|  | 31 | +    assert session.name == "test-session" | 
|  | 32 | +``` | 
|  | 33 | + | 
|  | 34 | +### Session fixture | 
|  | 35 | + | 
|  | 36 | +```{eval-rst} | 
|  | 37 | +.. autofunction:: libtmux.pytest_plugin.session | 
|  | 38 | +``` | 
|  | 39 | + | 
|  | 40 | +Example usage: | 
|  | 41 | + | 
|  | 42 | +```python | 
|  | 43 | +def test_session_functions(session): | 
|  | 44 | +    """Test basic session functions.""" | 
|  | 45 | +    assert session.is_alive() | 
|  | 46 | +     | 
|  | 47 | +    # Create a new window | 
|  | 48 | +    window = session.new_window(window_name="test-window") | 
|  | 49 | +    assert window.window_name == "test-window" | 
|  | 50 | +``` | 
|  | 51 | + | 
|  | 52 | +### Window fixture | 
|  | 53 | + | 
|  | 54 | +```{eval-rst} | 
|  | 55 | +.. autofunction:: libtmux.pytest_plugin.window | 
|  | 56 | +``` | 
|  | 57 | + | 
|  | 58 | +Example usage: | 
|  | 59 | + | 
|  | 60 | +```python | 
|  | 61 | +def test_window_functions(window): | 
|  | 62 | +    """Test basic window functions.""" | 
|  | 63 | +    # Get the active pane | 
|  | 64 | +    pane = window.active_pane | 
|  | 65 | +    assert pane is not None | 
|  | 66 | +     | 
|  | 67 | +    # Split the window | 
|  | 68 | +    new_pane = window.split_window() | 
|  | 69 | +    assert len(window.panes) == 2 | 
|  | 70 | +``` | 
|  | 71 | + | 
|  | 72 | +### Pane fixture | 
|  | 73 | + | 
|  | 74 | +```{eval-rst} | 
|  | 75 | +.. autofunction:: libtmux.pytest_plugin.pane | 
|  | 76 | +``` | 
|  | 77 | + | 
|  | 78 | +Example usage: | 
|  | 79 | + | 
|  | 80 | +```python | 
|  | 81 | +def test_pane_functions(pane): | 
|  | 82 | +    """Test basic pane functions.""" | 
|  | 83 | +    # Send a command to the pane | 
|  | 84 | +    pane.send_keys("echo 'Hello from pane'", enter=True) | 
|  | 85 | +     | 
|  | 86 | +    # Give the command time to execute | 
|  | 87 | +    import time | 
|  | 88 | +    time.sleep(0.5) | 
|  | 89 | +     | 
|  | 90 | +    # Capture and verify the output | 
|  | 91 | +    output = pane.capture_pane() | 
|  | 92 | +    assert any("Hello from pane" in line for line in output) | 
|  | 93 | +``` | 
|  | 94 | + | 
|  | 95 | +## Helper fixtures | 
|  | 96 | + | 
|  | 97 | +### TestServer fixture | 
|  | 98 | + | 
|  | 99 | +```{eval-rst} | 
|  | 100 | +.. autofunction:: libtmux.pytest_plugin.TestServer | 
|  | 101 | +``` | 
|  | 102 | + | 
|  | 103 | +Example usage: | 
|  | 104 | + | 
|  | 105 | +```python | 
|  | 106 | +def test_multiple_servers(TestServer): | 
|  | 107 | +    """Test creating multiple independent tmux servers.""" | 
|  | 108 | +    # Create first server | 
|  | 109 | +    server1 = TestServer() | 
|  | 110 | +    session1 = server1.new_session(session_name="session1") | 
|  | 111 | +     | 
|  | 112 | +    # Create second server (completely independent) | 
|  | 113 | +    server2 = TestServer() | 
|  | 114 | +    session2 = server2.new_session(session_name="session2") | 
|  | 115 | +     | 
|  | 116 | +    # Verify both servers are running | 
|  | 117 | +    assert server1.is_alive() | 
|  | 118 | +    assert server2.is_alive() | 
|  | 119 | +     | 
|  | 120 | +    # Verify sessions exist on their respective servers only | 
|  | 121 | +    assert session1.server is server1 | 
|  | 122 | +    assert session2.server is server2 | 
|  | 123 | +``` | 
|  | 124 | + | 
|  | 125 | +For more advanced usage with custom configuration: | 
|  | 126 | + | 
|  | 127 | +```{literalinclude} ../../tests/pytest_examples/test_direct_testserver.py | 
|  | 128 | +:language: python | 
|  | 129 | +:pyobject: test_custom_server_config | 
|  | 130 | +``` | 
|  | 131 | + | 
|  | 132 | +You can also use TestServer directly as a context manager: | 
|  | 133 | + | 
|  | 134 | +```{literalinclude} ../../tests/pytest_examples/test_direct_testserver.py | 
|  | 135 | +:language: python | 
|  | 136 | +:pyobject: test_testserver_direct_usage | 
|  | 137 | +``` | 
|  | 138 | + | 
|  | 139 | +### Environment fixtures | 
|  | 140 | + | 
|  | 141 | +These fixtures help manage the testing environment: | 
|  | 142 | + | 
|  | 143 | +```{eval-rst} | 
|  | 144 | +.. autofunction:: libtmux.pytest_plugin.home_path | 
|  | 145 | +.. autofunction:: libtmux.pytest_plugin.user_path | 
|  | 146 | +.. autofunction:: libtmux.pytest_plugin.config_file | 
|  | 147 | +``` | 
|  | 148 | + | 
|  | 149 | +## Customizing fixtures | 
|  | 150 | + | 
|  | 151 | +(custom_session_params)= | 
|  | 152 | + | 
|  | 153 | +### Custom session parameters | 
|  | 154 | + | 
|  | 155 | +You can override `session_params` to customize the `session` fixture: | 
|  | 156 | + | 
|  | 157 | +```python | 
|  | 158 | +@pytest.fixture | 
|  | 159 | +def session_params(): | 
|  | 160 | +    """Customize session parameters.""" | 
|  | 161 | +    return { | 
|  | 162 | +        "x": 800, | 
|  | 163 | +        "y": 600, | 
|  | 164 | +        "suppress_history": True | 
|  | 165 | +    } | 
|  | 166 | +``` | 
|  | 167 | + | 
|  | 168 | +These parameters are passed directly to {meth}`Server.new_session`. | 
|  | 169 | + | 
|  | 170 | +(set_home)= | 
|  | 171 | + | 
|  | 172 | +### Setting a temporary home directory | 
|  | 173 | + | 
|  | 174 | +You can customize the home directory used for tests: | 
|  | 175 | + | 
|  | 176 | +```python | 
|  | 177 | +@pytest.fixture | 
|  | 178 | +def set_home(monkeypatch, tmp_path): | 
|  | 179 | +    """Set a custom temporary home directory.""" | 
|  | 180 | +    monkeypatch.setenv("HOME", str(tmp_path)) | 
|  | 181 | +    tmux_config = tmp_path / ".tmux.conf" | 
|  | 182 | +    tmux_config.write_text("set -g status off\nset -g history-limit 1000") | 
|  | 183 | +    return tmp_path | 
|  | 184 | +``` | 
|  | 185 | + | 
|  | 186 | +## Using a custom tmux configuration | 
|  | 187 | + | 
|  | 188 | +If you need to test with a specific tmux configuration: | 
|  | 189 | + | 
|  | 190 | +```python | 
|  | 191 | +@pytest.fixture | 
|  | 192 | +def custom_config(tmp_path): | 
|  | 193 | +    """Create a custom tmux configuration.""" | 
|  | 194 | +    config_file = tmp_path / "tmux.conf" | 
|  | 195 | +    config_file.write_text(""" | 
|  | 196 | +    set -g status off | 
|  | 197 | +    set -g base-index 1 | 
|  | 198 | +    set -g history-limit 5000 | 
|  | 199 | +    """) | 
|  | 200 | +    return str(config_file) | 
|  | 201 | + | 
|  | 202 | +@pytest.fixture | 
|  | 203 | +def server_with_config(custom_config): | 
|  | 204 | +    """Create a server with a custom configuration.""" | 
|  | 205 | +    server = libtmux.Server(config_file=custom_config) | 
|  | 206 | +    yield server | 
|  | 207 | +    server.kill_server() | 
|  | 208 | +``` | 
0 commit comments