Skip to content

Commit 32c4884

Browse files
Recorder without decorator (#745)
* allow running singe tests * test and document dumping to file
1 parent 9f19f3f commit 32c4884

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

README.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ will produce next output:
130130
status: 202
131131
url: https://httpstat.us/202
132132
133+
If you are in the REPL, you can also activete the recorder for all following responses:
134+
135+
.. code-block:: python
136+
137+
import requests
138+
from responses import _recorder
139+
140+
_recorder.recorder.start()
141+
142+
requests.get("https://httpstat.us/500")
143+
144+
_recorder.recorder.dump_to_file("out.yaml")
145+
146+
# you can stop or reset the recorder
147+
_recorder.recorder.stop()
148+
_recorder.recorder.reset()
133149
134150
Replay responses (populate registry) from files
135151
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

responses/_recorder.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Callable
1010
from typing import Dict
1111
from typing import List
12+
from typing import Optional
1213
from typing import Type
1314
from typing import Union
1415
from responses import FirstMatchRegistry
@@ -122,10 +123,13 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any": # type: ignore[misc]
122123

123124
def dump_to_file(
124125
self,
125-
*,
126126
file_path: "Union[str, bytes, os.PathLike[Any]]",
127-
registered: "List[BaseResponse]",
127+
*,
128+
registered: "Optional[List[BaseResponse]]" = None,
128129
) -> None:
130+
"""Dump the recorded responses to a file."""
131+
if registered is None:
132+
registered = self.get_registry().registered
129133
with open(file_path, "w") as file:
130134
_dump(registered, file, yaml.dump)
131135

responses/tests/test_recorder.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,35 @@ def prepare_server(self, httpserver):
152152
url400 = httpserver.url_for("/status/wrong")
153153
return url202, url400, url404, url500
154154

155+
def test_use_recorder_without_decorator(self, httpserver):
156+
"""I want to be able to record in the REPL."""
157+
url202, url400, url404, url500 = self.prepare_server(httpserver)
158+
159+
_recorder.recorder.start()
160+
161+
def another():
162+
requests.get(url500)
163+
requests.put(url202)
164+
165+
def run():
166+
requests.get(url404)
167+
requests.get(url400)
168+
another()
169+
170+
run()
171+
172+
_recorder.recorder.stop()
173+
_recorder.recorder.dump_to_file(self.out_file)
174+
175+
with open(self.out_file) as file:
176+
data = yaml.safe_load(file)
177+
assert data == get_data(httpserver.host, httpserver.port)
178+
179+
# Now, we test that the recorder is properly reset
180+
assert _recorder.recorder.get_registry().registered
181+
_recorder.recorder.reset()
182+
assert not _recorder.recorder.get_registry().registered
183+
155184

156185
class TestReplay:
157186
def setup_method(self):

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ filterwarnings =
99
[testenv]
1010
extras = tests
1111
commands =
12-
pytest . --asyncio-mode=auto --cov responses --cov-report term-missing
12+
pytest . --asyncio-mode=auto --cov responses --cov-report term-missing {posargs}
1313

1414

1515
[testenv:mypy]

0 commit comments

Comments
 (0)