Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6db76dc

Browse filesBrowse files
committed
docs,tests(pytest plugin) Examples
1 parent e3e09e1 commit 6db76dc
Copy full SHA for 6db76dc
Expand file treeCollapse file tree

22 files changed

+2030
-119
lines changed
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
myst:
3+
html_meta:
4+
description: "Advanced techniques for testing with the libtmux pytest plugin"
5+
keywords: "tmux, pytest, advanced testing, polling, temporary files"
6+
---
7+
8+
(advanced-techniques)=
9+
10+
# Advanced Techniques
11+
12+
This page covers advanced testing techniques using the libtmux pytest plugin for more sophisticated testing scenarios.
13+
14+
## Testing with Temporary Files
15+
16+
### Creating Temporary Project Directories
17+
18+
```{literalinclude} ../../tests/examples/pytest_plugin/test_temp_files.py
19+
:language: python
20+
:pyobject: temp_project_dir
21+
```
22+
23+
### Working with Files in Temporary Directories
24+
25+
```{literalinclude} ../../tests/examples/pytest_plugin/test_temp_files.py
26+
:language: python
27+
:pyobject: test_project_file_manipulation
28+
```
29+
30+
## Command Polling
31+
32+
### Implementing Robust Wait Functions
33+
34+
```{literalinclude} ../../tests/examples/pytest_plugin/test_command_polling.py
35+
:language: python
36+
:pyobject: wait_for_output
37+
```
38+
39+
### Testing with Command Polling
40+
41+
```{literalinclude} ../../tests/examples/pytest_plugin/test_command_polling.py
42+
:language: python
43+
:pyobject: test_command_with_polling
44+
```
45+
46+
### Error Handling with Polling
47+
48+
```{literalinclude} ../../tests/examples/pytest_plugin/test_command_polling.py
49+
:language: python
50+
:pyobject: test_error_handling
51+
```
52+
53+
## Setting Custom Home Directory
54+
55+
### Temporary Home Directory Setup
56+
57+
```{literalinclude} ../../tests/examples/pytest_plugin/test_home_directory.py
58+
:language: python
59+
:pyobject: set_home
60+
```
61+
62+
## Testing with Complex Layouts
63+
64+
Creating and testing more complex window layouts:
65+
66+
```{literalinclude} ../../tests/examples/pytest_plugin/test_complex_layouts.py
67+
:language: python
68+
:pyobject: test_complex_layouts
69+
```
70+
71+
For an even more advanced layout, you can create a tiled configuration:
72+
73+
```{literalinclude} ../../tests/examples/pytest_plugin/test_complex_layouts.py
74+
:language: python
75+
:pyobject: test_tiled_layout
76+
```
77+
78+
## Testing Across Server Restarts
79+
80+
When you need to test functionality that persists across server restarts:
81+
82+
```{literalinclude} ../../tests/examples/pytest_plugin/test_server_restart.py
83+
:language: python
84+
:pyobject: test_persist_across_restart
85+
```
86+
87+
## Best Practices
88+
89+
### Test Structure
90+
91+
1. **Arrange** - Set up your tmux environment and test data
92+
2. **Act** - Perform the actions you want to test
93+
3. **Assert** - Verify the expected outcome
94+
4. **Clean up** - Reset any state changes (usually handled by fixtures)
95+
96+
### Tips for Reliable Tests
97+
98+
1. **Use appropriate waits**: Terminal operations aren't instantaneous. Add sufficient wait times or use polling techniques.
99+
100+
2. **Capture full pane contents**: Use `pane.capture_pane()` to get all output content for verification.
101+
102+
3. **Isolate tests**: Don't rely on state from other tests. Each test should set up its own environment.
103+
104+
4. **Use descriptive assertions**: When tests fail, the assertion message should clarify what went wrong.
105+
106+
5. **Test error conditions**: Include tests for error handling to ensure your code behaves correctly in failure scenarios.
107+
108+
6. **Keep tests fast**: Minimize wait times while keeping tests reliable.
109+
110+
7. **Use parametrized tests**: For similar tests with different inputs, use pytest's parametrize feature.
111+
112+
8. **Document test requirements**: If tests require specific tmux features, document this in comments.
113+
114+
9. **Mind CI environments**: Tests should work consistently in both local and CI environments, which may have different tmux versions and capabilities.

‎docs/pytest-plugin/fixtures.md

Copy file name to clipboard
+208Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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/examples/pytest_plugin/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/examples/pytest_plugin/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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.