Skip to content

Commit 707a8ba

Browse files
authored
feat(pytest_plugin): Add session_params (#470)
2 parents e5069ab + 0a509c6 commit 707a8ba

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

Diff for: CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ $ pip install --user --upgrade --pre libtmux
1818

1919
- Server.new_session: Accept `x` and `y`, thanks
2020
@rockandska (#469)
21+
- New test fixture: `session_params`. The dict is used direclty in the `session`
22+
pytest fixture (#470)
2123

2224
## libtmux 0.19.1 (2022-01-07)
2325

Diff for: docs/pytest-plugin/index.md

+24
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ options:
6969

7070
You could also read the code and override {func}`server fixtures <libtmux.pytest_plugin.server>`'s in your own doctest. doctest.
7171

72+
(custom_session_params)=
73+
74+
### Custom session parameters
75+
76+
You can override `session_params` to custom the `session` fixture. The
77+
dictionary will directly pass into :meth:`Server.new_sesion` keyword arguments.
78+
79+
```python
80+
import pytest
81+
82+
@pytest.fixture
83+
def session_params():
84+
return {
85+
'x': 800,
86+
'y': 600
87+
}
88+
89+
90+
def test_something(session):
91+
assert session
92+
```
93+
94+
The above will assure the libtmux session launches with `-x 800 -y 600`.
95+
7296
(set_home)=
7397

7498
### Setting a temporary home directory

Diff for: src/libtmux/pytest_plugin.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,47 @@ def fin() -> None:
144144

145145

146146
@pytest.fixture(scope="function")
147-
def session(request: pytest.FixtureRequest, server: Server) -> "Session":
147+
def session_params() -> t.Dict[str, t.Any]:
148+
"""Returns a new, temporary :class:`libtmux.Session`
149+
150+
>>> import pytest
151+
>>> from libtmux.session import Session
152+
153+
>>> @pytest.fixture
154+
... def session_params(session_params):
155+
... return {
156+
... 'x': 800,
157+
... 'y': 600,
158+
... }
159+
160+
>>> def test_example(session: "Session") -> None:
161+
... assert isinstance(session.name, str)
162+
... assert session.name.startswith('libtmux_')
163+
... window = session.new_window(window_name='new one')
164+
... assert window.name == 'new one'
165+
166+
.. ::
167+
>>> locals().keys()
168+
dict_keys(...)
169+
170+
>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:4])
171+
>>> pytester = request.getfixturevalue('pytester')
172+
173+
>>> pytester.makepyfile(**{'whatever.py': source})
174+
PosixPath(...)
175+
176+
>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
177+
===...
178+
179+
>>> result.assert_outcomes(passed=1)
180+
"""
181+
return {}
182+
183+
184+
@pytest.fixture(scope="function")
185+
def session(
186+
request: pytest.FixtureRequest, session_params: t.Dict[str, t.Any], server: Server
187+
) -> "Session":
148188
"""Returns a new, temporary :class:`libtmux.Session`
149189
150190
>>> from libtmux.session import Session
@@ -185,7 +225,7 @@ def session(request: pytest.FixtureRequest, server: Server) -> "Session":
185225
TEST_SESSION_NAME = get_test_session_name(server=server)
186226

187227
try:
188-
session = server.new_session(session_name=TEST_SESSION_NAME)
228+
session = server.new_session(session_name=TEST_SESSION_NAME, **session_params)
189229
except exc.LibTmuxException as e:
190230
raise e
191231

0 commit comments

Comments
 (0)