Skip to content

Commit bd315dd

Browse files
authored
docs,tests: pytest plugin examples (#439)
2 parents 569dac7 + 6628544 commit bd315dd

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ $ pip install --user --upgrade --pre libtmux
1212

1313
- _Insert changes/features/fixes for next release here_
1414

15+
## libtmux 0.15.3 (unreleased)
16+
17+
### Tests / docs
18+
19+
- Examples for pytest plugin (#439)
20+
1521
## libtmux 0.15.2 (2022-09-17)
1622

1723
**Maintenance release, no features or fixes**

conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest_plugins = ["pytester"]

docs/__init__.py

Whitespace-only changes.

src/libtmux/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def add_doctest_fixtures(
2424
doctest_namespace["session"] = session
2525
doctest_namespace["window"] = session.attached_window
2626
doctest_namespace["pane"] = session.attached_pane
27+
doctest_namespace["request"] = request
2728

2829

2930
@pytest.fixture(autouse=True, scope="function")

src/libtmux/pytest_plugin.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,31 @@ 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) -> None:
116+
... assert isinstance(server, Server)
117+
... session = server.new_session('my session')
118+
... assert len(server.sessions) == 1
119+
... assert [session.name.startswith('my') for session in server.sessions]
120+
121+
.. ::
122+
>>> locals().keys()
123+
dict_keys(...)
124+
125+
>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
126+
>>> pytester = request.getfixturevalue('pytester')
127+
128+
>>> pytester.makepyfile(**{'whatever.py': source})
129+
PosixPath(...)
130+
131+
>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
132+
===...
133+
134+
>>> result.assert_outcomes(passed=1)
135+
"""
112136
t = Server()
113137
t.socket_name = "libtmux_test%s" % next(namer)
114138

@@ -122,7 +146,31 @@ def fin() -> None:
122146

123147
@pytest.fixture(scope="function")
124148
def session(request: pytest.FixtureRequest, server: Server) -> "Session":
125-
"""Returns a new, temporary :class:`libtmux.Session`"""
149+
"""Returns a new, temporary :class:`libtmux.Session`
150+
151+
>>> from libtmux.session import Session
152+
153+
>>> def test_example(session: "Session") -> None:
154+
... assert isinstance(session.name, str)
155+
... assert session.name.startswith('libtmux_')
156+
... window = session.new_window(window_name='new one')
157+
... assert window.name == 'new one'
158+
159+
.. ::
160+
>>> locals().keys()
161+
dict_keys(...)
162+
163+
>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
164+
>>> pytester = request.getfixturevalue('pytester')
165+
166+
>>> pytester.makepyfile(**{'whatever.py': source})
167+
PosixPath(...)
168+
169+
>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
170+
===...
171+
172+
>>> result.assert_outcomes(passed=1)
173+
"""
126174
session_name = "tmuxp"
127175

128176
if not server.has_session(session_name):

0 commit comments

Comments
 (0)