Fix flaky DeprecationWarning tests
MODIFY
test/test_exceptions.py
test/test_exceptions.py
@@ -52,7 +52,7 @@ class TestFormat:
class TestNewConnectionError:
def test_pool_property_deprecation_warning(self):
err = NewConnectionError(HTTPConnection("localhost"), "test")
- with pytest.warns(DeprecationWarning) as record:
+ with pytest.warns(DeprecationWarning) as records:
err.pool
assert err.pool is err.conn
@@ -60,4 +60,4 @@ class TestNewConnectionError:
"The 'pool' property is deprecated and will be removed "
"in a later urllib3 v2.x release. use 'conn' instead."
)
- assert record[0].message.args[0] == msg
+ assert any(record.message.args[0] == msg for record in records)

MODIFY
test/test_poolmanager.py
test/test_poolmanager.py
@@ -277,15 +277,14 @@ class TestPoolManager:
"port": 8080,
"strict": True,
}
- with pytest.warns(DeprecationWarning) as record:
+ with pytest.warns(DeprecationWarning) as records:
p.connection_from_context(context)
- assert 1 == len(record)
msg = (
"The 'strict' parameter is no longer needed on Python 3+. "
"This will raise an error in urllib3 v3.0.0."
)
- assert record[0].message.args[0] == msg
+ assert any(record.message.args[0] == msg for record in records)
_, kwargs = connection_from_pool_key.call_args
assert kwargs["request_context"] == {

MODIFY
test/with_dummyserver/test_integration.py
test/with_dummyserver/test_integration.py
@@ -7,29 +7,27 @@ from dummyserver.testcase import HTTPDummyServerTestCase, HTTPSDummyServerTestCa
class TestHTTPIntegration(HTTPDummyServerTestCase):
def test_requests_integration(self):
- with pytest.warns(DeprecationWarning) as record:
+ with pytest.warns(DeprecationWarning) as records:
response = requests.get(f"{self.scheme}://{self.host}:{self.port}")
assert 200 == response.status_code
- assert 1 == len(record)
msg = (
"The 'strict' parameter is no longer needed on Python 3+. "
"This will raise an error in urllib3 v3.0.0."
)
- assert record[0].message.args[0] == msg
+ assert any(record.message.args[0] == msg for record in records)
class TestHTTPSIntegration(HTTPSDummyServerTestCase):
def test_requests_integration(self):
- with pytest.warns(DeprecationWarning) as record:
+ with pytest.warns(DeprecationWarning) as records:
response = requests.get(
f"{self.scheme}://{self.host}:{self.port}", verify=DEFAULT_CA
)
assert 200 == response.status_code
- assert 1 == len(record)
msg = (
"The 'strict' parameter is no longer needed on Python 3+. "
"This will raise an error in urllib3 v3.0.0."
)
- assert record[0].message.args[0] == msg
+ assert any(record.message.args[0] == msg for record in records)

Fix flaky DeprecationWarning tests