-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpytest_responses.py
42 lines (30 loc) · 1.03 KB
/
pytest_responses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from packaging import version
import pytest
import responses as responses_
def get_withoutresponses_marker(item):
if version.parse(pytest.__version__) >= version.parse('4.0.0'):
return item.get_closest_marker('withoutresponses')
else:
return item.get_marker('withoutresponses')
# pytest plugin support
def pytest_configure(config):
config.addinivalue_line(
'markers',
'withoutresponses: Tests which need access to external domains.'
)
def pytest_runtest_setup(item):
if not get_withoutresponses_marker(item):
responses_.start()
def pytest_runtest_teardown(item):
if not get_withoutresponses_marker(item):
try:
responses_.stop()
responses_.reset()
except (AttributeError, RuntimeError):
# patcher was already uninstalled (or not installed at all) and
# responses doesnt let us force maintain it
pass
@pytest.yield_fixture
def responses():
with responses_.RequestsMock() as rsps:
yield rsps