Skip to content

pytest plugin examples #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ $ pip install --user --upgrade --pre libtmux

- _Insert changes/features/fixes for next release here_

## libtmux 0.15.3 (unreleased)

### Tests / docs

- Examples for pytest plugin (#439)

## libtmux 0.15.2 (2022-09-17)

**Maintenance release, no features or fixes**
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest_plugins = ["pytester"]
Empty file added docs/__init__.py
Empty file.
1 change: 1 addition & 0 deletions src/libtmux/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def add_doctest_fixtures(
doctest_namespace["session"] = session
doctest_namespace["window"] = session.attached_window
doctest_namespace["pane"] = session.attached_pane
doctest_namespace["request"] = request


@pytest.fixture(autouse=True, scope="function")
Expand Down
52 changes: 50 additions & 2 deletions src/libtmux/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,31 @@ def server(
monkeypatch: pytest.MonkeyPatch,
config_file: pathlib.Path,
) -> Server:
"""Returns a new, temporary :class:`libtmux.Server`"""
"""Returns a new, temporary :class:`libtmux.Server`

>>> from libtmux.server import Server

>>> def test_example(server: Server) -> None:
... assert isinstance(server, Server)
... session = server.new_session('my session')
... assert len(server.sessions) == 1
... assert [session.name.startswith('my') for session in server.sessions]

.. ::
>>> locals().keys()
dict_keys(...)

>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
>>> pytester = request.getfixturevalue('pytester')

>>> pytester.makepyfile(**{'whatever.py': source})
PosixPath(...)

>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
===...

>>> result.assert_outcomes(passed=1)
"""
t = Server()
t.socket_name = "libtmux_test%s" % next(namer)

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

@pytest.fixture(scope="function")
def session(request: pytest.FixtureRequest, server: Server) -> "Session":
"""Returns a new, temporary :class:`libtmux.Session`"""
"""Returns a new, temporary :class:`libtmux.Session`

>>> from libtmux.session import Session

>>> def test_example(session: "Session") -> None:
... assert isinstance(session.name, str)
... assert session.name.startswith('libtmux_')
... window = session.new_window(window_name='new one')
... assert window.name == 'new one'

.. ::
>>> locals().keys()
dict_keys(...)

>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
>>> pytester = request.getfixturevalue('pytester')

>>> pytester.makepyfile(**{'whatever.py': source})
PosixPath(...)

>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
===...

>>> result.assert_outcomes(passed=1)
"""
session_name = "tmuxp"

if not server.has_session(session_name):
Expand Down