Skip to content

Commit a0e9f1f

Browse files
authored
docs,tests: Improve test utility fixtures (#437)
2 parents bfcbfa6 + 1f1aeba commit a0e9f1f

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ quickstart
1919
about
2020
topics/index
2121
reference/index
22-
pytest-plugin
22+
pytest-plugin/index
2323
```
2424

2525
```{toctree}

docs/internals/index.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ These APIs are internal and not covered by versioning policy.
88

99
:::
1010

11-
```{toctree}
12-
13-
test
14-
```
15-
1611
## Environmental variables
1712

1813
(LIBTMUX_TMUX_FORMAT_SEPARATOR)=

docs/pytest-plugin.md renamed to docs/pytest-plugin/index.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ $ pip install libtmux
2828

2929
The pytest plugin will be automatically detected via pytest, and the fixtures will be added.
3030

31-
## Fixtures
31+
### Real world usage
32+
33+
View libtmux's own [tests/](https://github.com/tmux-python/libtmux/tree/master/tests) as well as
34+
tmuxp's [tests/](https://github.com/tmux-python/tmuxp/tree/master/tests).
35+
36+
libtmux's tests `autouse` the {ref}`recommended-fixtures` above to ensure stable, assertions and
37+
object lookups in the test grid.
38+
39+
## pytest-tmux
3240

3341
`pytest-tmux` works through providing {ref}`pytest fixtures <pytest:fixtures-api>` - so read up on
3442
those!
@@ -77,15 +85,7 @@ def set_home(
7785
monkeypatch.setenv("HOME", str(user_path))
7886
```
7987

80-
## See examples
81-
82-
View libtmux's own [tests/](https://github.com/tmux-python/libtmux/tree/master/tests) as well as
83-
tmuxp's [tests/](https://github.com/tmux-python/tmuxp/tree/master/tests).
84-
85-
libtmux's tests `autouse` the {ref}`recommended-fixtures` above to ensure stable, assertions and
86-
object lookups in the test grid.
87-
88-
## API reference
88+
## Fixtures
8989

9090
```{eval-rst}
9191
.. automodule:: libtmux.pytest_plugin
@@ -95,3 +95,11 @@ object lookups in the test grid.
9595
:show-inheritance:
9696
:member-order: bysource
9797
```
98+
99+
## Test utilities
100+
101+
```{toctree}
102+
:maxdepth: 1
103+
104+
test
105+
```
File renamed without changes.

docs/redirects.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
"traversal.md" "topics/traversal.md"
66
"sessions.md" "reference/sessions.md"
77
"api.md" "internals/index.md"
8+
"pytest-plugin.md" "pytest-plugin/index.md"

src/libtmux/test.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class RandomStrSequence:
2727
def __init__(
2828
self, characters: str = "abcdefghijklmnopqrstuvwxyz0123456789_"
2929
) -> None:
30+
"""Create a random letter / number generator. 8 chars in length.
31+
32+
>>> rng = RandomStrSequence()
33+
>>> next(rng)
34+
'...'
35+
>>> len(next(rng))
36+
8
37+
>>> type(next(rng))
38+
<class 'str'>
39+
"""
3040
self.characters: str = characters
3141

3242
def __iter__(self) -> "RandomStrSequence":
@@ -68,7 +78,6 @@ def retry_until(
6878
6979
Examples
7080
--------
71-
7281
>>> def fn():
7382
... p = session.attached_window.attached_pane
7483
... p.server._update_panes()
@@ -110,6 +119,15 @@ def get_test_session_name(server: "Server", prefix: str = TEST_SESSION_PREFIX) -
110119
-------
111120
str
112121
Random session name guaranteed to not collide with current ones.
122+
123+
Examples
124+
--------
125+
>>> get_test_session_name(server=server)
126+
'libtmux_...'
127+
128+
Never the same twice:
129+
>>> get_test_session_name(server=server) != get_test_session_name(server=server)
130+
True
113131
"""
114132
while True:
115133
session_name = prefix + next(namer)
@@ -138,6 +156,15 @@ def get_test_window_name(
138156
-------
139157
str
140158
Random window name guaranteed to not collide with current ones.
159+
160+
Examples
161+
--------
162+
>>> get_test_window_name(session=session)
163+
'libtmux_...'
164+
165+
Never the same twice:
166+
>>> get_test_window_name(session=session) != get_test_window_name(session=session)
167+
True
141168
"""
142169
assert prefix is not None
143170
while True:
@@ -178,10 +205,9 @@ def temp_session(
178205
179206
Examples
180207
--------
181-
182208
>>> with temp_session(server) as session:
183209
... session.new_window(window_name='my window')
184-
Window(@... ...:..., Session($... ...))
210+
Window(@3 2:my window, Session($... ...))
185211
"""
186212

187213
if "session_name" in kwargs:
@@ -230,17 +256,15 @@ def temp_window(
230256
231257
Examples
232258
--------
233-
234259
>>> with temp_window(session) as window:
235260
... window
236-
Window(@... ...:..., Session($... ...))
261+
Window(@2 2:... Session($1 libtmux_...))
237262
238263
239264
>>> with temp_window(session) as window:
240265
... window.split_window()
241-
Pane(%... Window(@... ...:..., Session($... ...)))
266+
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
242267
"""
243-
244268
if "window_name" not in kwargs:
245269
window_name = get_test_window_name(session)
246270
else:
@@ -262,21 +286,18 @@ def temp_window(
262286

263287

264288
class EnvironmentVarGuard:
265-
266289
"""Mock environmental variables safetly.
267290
268291
Helps rotect the environment variable properly. Can be used as context
269292
manager.
270293
271294
Notes
272295
-----
273-
274296
Vendorized to fix issue with Anaconda Python 2 not including test module,
275297
see #121 [1]_
276298
277299
References
278300
----------
279-
280301
.. [1] Just installed, "ImportError: cannot import name test_support".
281302
GitHub issue for tmuxp. https://github.com/tmux-python/tmuxp/issues/121.
282303
Created October 12th, 2015. Accessed April 7th, 2018.

0 commit comments

Comments
 (0)