From 005571d1180835eb5266a3fdbdbe8fdae57d90c2 Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Sun, 13 Aug 2023 16:08:21 -0700 Subject: [PATCH] Remove pytest-mock requirement (#6505) --- requirements-dev.txt | 1 - tests/test_help.py | 14 ++++++++------ tests/test_requests.py | 31 +++++++++++++++---------------- tests/test_utils.py | 9 +++++---- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index c24a6bc6d4..f137c46a33 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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 diff --git a/tests/test_help.py b/tests/test_help.py index fb4e967c53..5fca6207ef 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -1,3 +1,5 @@ +from unittest import mock + from requests.help import info @@ -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"} diff --git a/tests/test_requests.py b/tests/test_requests.py index 6e831a91e7..5d8d2cd3af 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -8,6 +8,7 @@ import pickle import re import warnings +from unittest import mock import pytest import urllib3 @@ -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): @@ -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) @@ -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() diff --git a/tests/test_utils.py b/tests/test_utils.py index 112bbd1eaf..8988eaf69c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -5,6 +5,7 @@ import zipfile from collections import deque from io import BytesIO +from unittest import mock import pytest @@ -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(