Skip to content

Commit 40029d5

Browse files
committed
Expose the MessageRecorder class, not the pytest fixture
I'm not sure pytest fixtures are supposed to be re-used like that. Anyway mypy users need the MessageRecorder class to annotate their test functions ... Also, add an explicit start() method. We don't expect all the records to be lost just because a new MessageRecorder() is constructed
1 parent 232cdae commit 40029d5

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

cli_ui/tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .conftest import message_recorder
1+
from .conftest import MessageRecorder # noqa

cli_ui/tests/conftest.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@
66

77

88
class MessageRecorder:
9+
""" Helper class to tests emitted messages """
10+
911
def __init__(self) -> None:
10-
cli_ui.CONFIG["record"] = True
1112
cli_ui._MESSAGES = list()
1213

14+
def start(self) -> None:
15+
""" Start recording messages """
16+
cli_ui.CONFIG["record"] = True
17+
1318
def stop(self) -> None:
19+
""" Stop recording messages """
1420
cli_ui.CONFIG["record"] = False
1521
cli_ui._MESSAGES = list()
1622

1723
def reset(self) -> None:
24+
""" Reset the list """
1825
cli_ui._MESSAGES = list()
1926

2027
def find(self, pattern: str) -> Optional[str]:
28+
""" Find a message in the list of recorded message
29+
30+
:param pattern: regular expression pattern to use
31+
when looking for recorded message
32+
"""
2133
regexp = re.compile(pattern)
2234
for message in cli_ui._MESSAGES:
2335
if re.search(regexp, message):
@@ -27,16 +39,7 @@ def find(self, pattern: str) -> Optional[str]:
2739

2840
@pytest.fixture
2941
def message_recorder(request: Any) -> Iterator[MessageRecorder]:
30-
""" Start recording messages
31-
32-
*Methods*
33-
34-
* `stop()`: stop recording
35-
* `reset()`: clear the list of recorded messages.
36-
* `find(regex)` find a message in the list matching the given regular
37-
expression
38-
39-
"""
4042
recorder = MessageRecorder()
43+
recorder.start()
4144
yield recorder
4245
recorder.stop()

docs/index.rst

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,22 +360,35 @@ Auto-correct
360360
:func:`ask_choice` instead.
361361

362362

363-
.. _pytest:
364363

365-
Testing with pytest
366-
++++++++++++++++++++
364+
Testing
365+
+++++++
367366

368-
.. autofunction:: cli_ui.tests.message_recorder
367+
.. autoclass:: cli_ui.tests.MessageRecorder
368+
:members:
369369

370370
::
371371

372-
from cli_ui.tests import message_recorder
372+
# Example with pytest
373+
374+
# in conftest.py
375+
from cli_ui.tests import MessageRecorder
376+
import pytest
377+
378+
@pytest.fixture()
379+
def message_recorder():
380+
message_recorder = MessageRecorder()
381+
message_recorder.start()
382+
yield message_recorder
383+
message_recorder.stop()
373384

374385

386+
# in foo.py
375387
def foo():
376388
cli_ui.info("Fooing")
377389

378390

391+
# in test_foo.py
379392
def test_foo(message_recorder):
380393
foo()
381394
assert message_recorder.find("Fooing")

0 commit comments

Comments
 (0)