Skip to content

Commit 44950da

Browse files
committed
tests(pytest_plugin): Add session, server doctest example
1 parent 0d4acff commit 44950da

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/libtmux/pytest_plugin.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,24 @@ def server(
108108
monkeypatch: pytest.MonkeyPatch,
109109
config_file: pathlib.Path,
110110
) -> Server:
111-
"""Returns a new, temporary :class:`libtmux.Server`"""
111+
"""Returns a new, temporary :class:`libtmux.Server`
112+
113+
>>> from libtmux.server import Server
114+
115+
>>> def test_example(server: "Server"):
116+
... session = server.new_session('my session')
117+
... assert len(server.sessions) == 1
118+
... assert [session.name.startswith('my') for session in server.sessions]
119+
120+
.. ::
121+
122+
>>> import inspect
123+
>>> test_mod = pytester.makepyfile(whatever=inspect.getsource(test_example))
124+
>>> result = pytester.inline_run(str(test_mod), '--disable-warnings')
125+
===...
126+
127+
>>> result.assertoutcome(passed=1)
128+
"""
112129
t = Server()
113130
t.socket_name = "libtmux_test%s" % next(namer)
114131

@@ -122,7 +139,28 @@ def fin() -> None:
122139

123140
@pytest.fixture(scope="function")
124141
def session(request: pytest.FixtureRequest, server: Server) -> "Session":
125-
"""Returns a new, temporary :class:`libtmux.Session`"""
142+
"""Returns a new, temporary :class:`libtmux.Session`
143+
144+
>>> from libtmux.session import Session
145+
146+
>>> def test_example(session: "Session"):
147+
... assert isinstance(session.name, str)
148+
... assert session.name.startswith('libtmux_')
149+
... window = session.new_window(window_name='new one')
150+
... assert window.name == 'new one'
151+
152+
.. ::
153+
154+
The nifty little thing above hides our pytester assertions from docs.
155+
156+
>>> import inspect
157+
158+
>>> test_mod = pytester.makepyfile(whatever=inspect.getsource(test_example))
159+
>>> result = pytester.inline_run(str(test_mod), '--disable-warnings')
160+
===...
161+
162+
>>> result.assertoutcome(passed=1)
163+
"""
126164
session_name = "tmuxp"
127165

128166
if not server.has_session(session_name):

0 commit comments

Comments
 (0)