Skip to content

Commit 2c10b0b

Browse files
committed
Export more names from responses in pytest-responses
This makes it easier to use the names, since the fixture name `responses` clashes with the module import. For example, it is possible to add a type annotation to a pytest module using the responses fixture: ``` def test_my_request(responses: pytest_responses.RequestsMock) -> None: ... ``` It is also possible to access matchers via pytest_responses.matchers, also assemble a Response object, use registries, etc. Also update to accessing default start/stop/reset via the `requests.mock` object, and using the `@pytest.fixtures` decorator since `@yield_fixture` has been long deprecated.
1 parent c871625 commit 2c10b0b

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

pytest_responses.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
import pytest
44

5-
import responses as responses_
5+
from responses import (
6+
Call,
7+
CallbackResponse,
8+
CallList,
9+
PassthroughResponse,
10+
RequestsMock,
11+
Response,
12+
matchers,
13+
registries,
14+
)
15+
from responses import mock as _mock
616

717

818
def get_withoutresponses_marker(item):
@@ -22,21 +32,38 @@ def pytest_configure(config):
2232

2333
def pytest_runtest_setup(item):
2434
if not get_withoutresponses_marker(item):
25-
responses_.start()
35+
_mock.start()
2636

2737

2838
def pytest_runtest_teardown(item):
2939
if not get_withoutresponses_marker(item):
3040
try:
31-
responses_.stop()
32-
responses_.reset()
41+
_mock.stop()
42+
_mock.reset()
3343
except (AttributeError, RuntimeError):
3444
# patcher was already uninstalled (or not installed at all) and
3545
# responses doesnt let us force maintain it
3646
pass
3747

3848

39-
@pytest.yield_fixture
49+
@pytest.fixture
4050
def responses():
41-
with responses_.RequestsMock() as rsps:
51+
with RequestsMock() as rsps:
4252
yield rsps
53+
54+
55+
__all__ = [
56+
'responses',
57+
'Call',
58+
'CallbackResponse',
59+
'CallList',
60+
'PassthroughResponse',
61+
'RequestsMock',
62+
'Response',
63+
'matchers',
64+
'registries',
65+
# pytest plug-in setup
66+
'pytest_configure',
67+
'pytest_runtest_setup',
68+
'pytest_runtest_teardown',
69+
]

0 commit comments

Comments
 (0)