Skip to content

Commit 12ac7ce

Browse files
committed
docs(pytest-plugin) Examples to test files
1 parent 9853289 commit 12ac7ce

File tree

4 files changed

+184
-35
lines changed

4 files changed

+184
-35
lines changed

docs/pytest-plugin/index.md

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,16 @@ You could also read the code and override {func}`server fixture <libtmux.pytest_
8686
You can override `session_params` to customize the `session` fixture. The
8787
dictionary will directly pass into {meth}`Server.new_session` keyword arguments.
8888

89-
```python
90-
import pytest
91-
92-
@pytest.fixture
93-
def session_params():
94-
return {
95-
'x': 800,
96-
'y': 600
97-
}
89+
```{literalinclude} ../../tests/pytest_examples/test_session_params.py
90+
:language: python
91+
:pyobject: session_params
92+
```
9893

94+
Simple test using the custom session parameters:
9995

100-
def test_something(session):
101-
assert session
96+
```{literalinclude} ../../tests/pytest_examples/test_session_params.py
97+
:language: python
98+
:pyobject: test_custom_session_dimensions
10299
```
103100

104101
The above will assure the libtmux session launches with `-x 800 -y 600`.
@@ -109,24 +106,16 @@ The above will assure the libtmux session launches with `-x 800 -y 600`.
109106

110107
If you need multiple independent tmux servers in your tests, the {func}`TestServer fixture <libtmux.pytest_plugin.TestServer>` provides a factory that creates servers with unique socket names. Each server is automatically cleaned up when the test completes.
111108

112-
```python
113-
def test_something(TestServer):
114-
Server = TestServer() # Get unique partial'd Server
115-
server = Server() # Create server instance
116-
117-
session = server.new_session()
118-
assert server.is_alive()
109+
```{literalinclude} ../../tests/pytest_examples/test_multiple_servers.py
110+
:language: python
111+
:pyobject: test_basic_test_server
119112
```
120113

121114
You can also use it with custom configurations, similar to the {ref}`server fixture <Setting a tmux configuration>`:
122115

123-
```python
124-
def test_with_config(TestServer, tmp_path):
125-
config_file = tmp_path / "tmux.conf"
126-
config_file.write_text("set -g status off")
127-
128-
Server = TestServer()
129-
server = Server(config_file=str(config_file))
116+
```{literalinclude} ../../tests/pytest_examples/test_multiple_servers.py
117+
:language: python
118+
:pyobject: test_with_config
130119
```
131120

132121
This is particularly useful when testing interactions between multiple tmux servers or when you need to verify behavior across server restarts.
@@ -135,16 +124,9 @@ This is particularly useful when testing interactions between multiple tmux serv
135124

136125
### Setting a temporary home directory
137126

138-
```python
139-
import pathlib
140-
import pytest
141-
142-
@pytest.fixture(autouse=True, scope="function")
143-
def set_home(
144-
monkeypatch: pytest.MonkeyPatch,
145-
user_path: pathlib.Path,
146-
):
147-
monkeypatch.setenv("HOME", str(user_path))
127+
```{literalinclude} ../../tests/pytest_examples/test_home_directory.py
128+
:language: python
129+
:pyobject: set_home
148130
```
149131

150132
## Advanced Testing Techniques
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Examples for setting up a temporary home directory for tests.
2+
3+
This file demonstrates how to set up and use a temporary home directory
4+
for tests, which is useful for isolating configuration files.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import os
10+
import time
11+
from typing import TYPE_CHECKING
12+
13+
import pytest
14+
15+
if TYPE_CHECKING:
16+
import pathlib
17+
18+
19+
@pytest.fixture(autouse=True)
20+
def set_home(
21+
monkeypatch: pytest.MonkeyPatch,
22+
user_path: pathlib.Path,
23+
) -> None:
24+
"""Set the HOME environment variable to a temporary directory."""
25+
monkeypatch.setenv("HOME", str(user_path))
26+
27+
28+
def test_home_directory_set(session) -> None:
29+
"""Test that the HOME environment variable is set correctly."""
30+
# Get the active pane to run commands
31+
pane = session.active_window.active_pane
32+
33+
# Execute a command to print the HOME directory
34+
pane.send_keys("echo $HOME", enter=True)
35+
36+
# Wait for the command to execute
37+
time.sleep(1)
38+
39+
# Capture the output
40+
output = pane.capture_pane()
41+
42+
# Get what we expect the HOME to be
43+
expected_home = os.environ.get("HOME")
44+
assert expected_home, "HOME environment variable not set"
45+
46+
# The output should include the HOME path somewhere
47+
found_home = False
48+
for line in output:
49+
if expected_home in line:
50+
found_home = True
51+
break
52+
53+
assert found_home, f"Expected to find {expected_home} in output: {output}"
54+
55+
56+
def test_create_config_in_home(session, user_path) -> None:
57+
"""Test creating and using configuration in the temporary home directory."""
58+
# Create a simple configuration file in the home directory
59+
config_file = user_path / ".my-test-config"
60+
config_file.write_text("test-config-value")
61+
62+
# Get the active pane to run commands
63+
pane = session.active_window.active_pane
64+
65+
# Execute a command to check the config file exists
66+
pane.send_keys(f"cat {config_file}", enter=True)
67+
68+
# Wait a moment for the command to execute
69+
time.sleep(1)
70+
71+
# Capture and verify the output
72+
output = pane.capture_pane()
73+
assert any("test-config-value" in line for line in output), (
74+
"Config file content not found"
75+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Examples for working with multiple tmux servers.
2+
3+
This file contains examples of using the TestServer fixture to create
4+
and manage multiple independent tmux server instances.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
10+
def test_basic_test_server(TestServer) -> None:
11+
"""Test creating and using a server via TestServer fixture."""
12+
# TestServer returns a partial Server class that we can instantiate
13+
ServerClass = TestServer() # Get uniquely configured Server class
14+
server = ServerClass # This is already an instance, not a class
15+
16+
server.new_session()
17+
assert server.is_alive()
18+
19+
# Clean up is automatic at the end of the test
20+
21+
22+
def test_with_config(TestServer, tmp_path) -> None:
23+
"""Test creating a server with custom configuration."""
24+
# Create a custom tmux configuration in the temporary directory
25+
config_file = tmp_path / "tmux.conf"
26+
config_file.write_text("set -g status off")
27+
28+
# Create a server using this configuration
29+
ServerClass = TestServer()
30+
# Override the server with a new one that uses our config
31+
server = type(ServerClass)(config_file=str(config_file))
32+
33+
# Verify the server is running
34+
assert server.is_alive()
35+
36+
# Create a session to work with
37+
server.new_session()
38+
39+
# Optional: Verify the configuration was applied
40+
# This would require specific tests for the status being off
41+
42+
# Clean up is automatic at the end of the test
43+
44+
45+
def test_multiple_independent_servers(TestServer) -> None:
46+
"""Test running multiple independent tmux servers simultaneously."""
47+
# Create first server
48+
ServerClass1 = TestServer()
49+
server1 = ServerClass1 # This is already an instance
50+
session1 = server1.new_session(session_name="session1")
51+
52+
# Create second server (completely independent)
53+
ServerClass2 = TestServer()
54+
server2 = ServerClass2 # This is already an instance
55+
session2 = server2.new_session(session_name="session2")
56+
57+
# Verify both servers are running
58+
assert server1.is_alive()
59+
assert server2.is_alive()
60+
61+
# Verify sessions exist on their respective servers only
62+
assert session1.server is server1
63+
assert session2.server is server2
64+
65+
# Verify session names are independent
66+
assert session1.name == "session1"
67+
assert session2.name == "session2"
68+
69+
# Clean up is automatic at the end of the test
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Example test demonstrating custom session parameters.
2+
3+
This example shows how to customize the session fixture parameters.
4+
"""
5+
6+
from __future__ import annotations
7+
8+
import pytest
9+
10+
11+
@pytest.fixture
12+
def session_params():
13+
"""Override session_params to specify custom session dimensions."""
14+
return {"x": 800, "y": 600}
15+
16+
17+
def test_custom_session_dimensions(session) -> None:
18+
"""Test that session is created with custom dimensions."""
19+
assert session
20+
21+
# Optional: Additional assertions about session dimensions
22+
# These would require accessing tmux directly to verify the dimensions
23+
# but we include the example for documentation completeness

0 commit comments

Comments
 (0)