Skip to content

Commit

Permalink
Remove pytest-mock requirement (#6505)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt authored Aug 13, 2023
1 parent d63e94f commit 005571d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pytest>=2.8.0,<=6.2.5
pytest-cov
pytest-httpbin==2.0.0
pytest-mock==2.0.0
httpbin==0.10.0
trustme
wheel
Expand Down
14 changes: 8 additions & 6 deletions tests/test_help.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from requests.help import info


Expand All @@ -11,15 +13,15 @@ def __init__(self, version):
self.__version__ = version


def test_idna_without_version_attribute(mocker):
def test_idna_without_version_attribute():
"""Older versions of IDNA don't provide a __version__ attribute, verify
that if we have such a package, we don't blow up.
"""
mocker.patch("requests.help.idna", new=None)
assert info()["idna"] == {"version": ""}
with mock.patch("requests.help.idna", new=None):
assert info()["idna"] == {"version": ""}


def test_idna_with_version_attribute(mocker):
def test_idna_with_version_attribute():
"""Verify we're actually setting idna version when it should be available."""
mocker.patch("requests.help.idna", new=VersionedPackage("2.6"))
assert info()["idna"] == {"version": "2.6"}
with mock.patch("requests.help.idna", new=VersionedPackage("2.6")):
assert info()["idna"] == {"version": "2.6"}
31 changes: 15 additions & 16 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pickle
import re
import warnings
from unittest import mock

import pytest
import urllib3
Expand Down Expand Up @@ -972,12 +973,12 @@ def test_invalid_ssl_certificate_files(self, httpbin_secure):
),
),
)
def test_env_cert_bundles(self, httpbin, mocker, env, expected):
def test_env_cert_bundles(self, httpbin, env, expected):
s = requests.Session()
mocker.patch("os.environ", env)
settings = s.merge_environment_settings(
url=httpbin("get"), proxies={}, stream=False, verify=True, cert=None
)
with mock.patch("os.environ", env):
settings = s.merge_environment_settings(
url=httpbin("get"), proxies={}, stream=False, verify=True, cert=None
)
assert settings["verify"] == expected

def test_http_with_certificate(self, httpbin):
Expand Down Expand Up @@ -1464,11 +1465,9 @@ def test_response_chunk_size_type(self):
(urllib3.exceptions.SSLError, tuple(), RequestsSSLError),
),
)
def test_iter_content_wraps_exceptions(
self, httpbin, mocker, exception, args, expected
):
def test_iter_content_wraps_exceptions(self, httpbin, exception, args, expected):
r = requests.Response()
r.raw = mocker.Mock()
r.raw = mock.Mock()
# ReadTimeoutError can't be initialized by mock
# so we'll manually create the instance with args
r.raw.stream.side_effect = exception(*args)
Expand Down Expand Up @@ -2093,16 +2092,16 @@ def test_response_iter_lines_reentrant(self, httpbin):
next(r.iter_lines())
assert len(list(r.iter_lines())) == 3

def test_session_close_proxy_clear(self, mocker):
def test_session_close_proxy_clear(self):
proxies = {
"one": mocker.Mock(),
"two": mocker.Mock(),
"one": mock.Mock(),
"two": mock.Mock(),
}
session = requests.Session()
mocker.patch.dict(session.adapters["http://"].proxy_manager, proxies)
session.close()
proxies["one"].clear.assert_called_once_with()
proxies["two"].clear.assert_called_once_with()
with mock.patch.dict(session.adapters["http://"].proxy_manager, proxies):
session.close()
proxies["one"].clear.assert_called_once_with()
proxies["two"].clear.assert_called_once_with()

def test_proxy_auth(self):
adapter = HTTPAdapter()
Expand Down
9 changes: 5 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import zipfile
from collections import deque
from io import BytesIO
from unittest import mock

import pytest

Expand Down Expand Up @@ -751,13 +752,13 @@ def test_should_bypass_proxies(url, expected, monkeypatch):
("http://user:pass@hostname:5000", "hostname"),
),
)
def test_should_bypass_proxies_pass_only_hostname(url, expected, mocker):
def test_should_bypass_proxies_pass_only_hostname(url, expected):
"""The proxy_bypass function should be called with a hostname or IP without
a port number or auth credentials.
"""
proxy_bypass = mocker.patch("requests.utils.proxy_bypass")
should_bypass_proxies(url, no_proxy=None)
proxy_bypass.assert_called_once_with(expected)
with mock.patch("requests.utils.proxy_bypass") as proxy_bypass:
should_bypass_proxies(url, no_proxy=None)
proxy_bypass.assert_called_once_with(expected)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 005571d

Please sign in to comment.