Skip to content

Commit f98b9b4

Browse files
gh-71339: Use new assertion methods in the urllib tests (GH-129056)
1 parent 7a29c98 commit f98b9b4

File tree

5 files changed

+13
-20
lines changed

5 files changed

+13
-20
lines changed

Lib/test/test_urllib.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ def test_interface(self):
121121
# Make sure object returned by urlopen() has the specified methods
122122
for attr in ("read", "readline", "readlines", "fileno",
123123
"close", "info", "geturl", "getcode", "__iter__"):
124-
self.assertTrue(hasattr(self.returned_obj, attr),
125-
"object returned by urlopen() lacks %s attribute" %
126-
attr)
124+
self.assertHasAttr(self.returned_obj, attr)
127125

128126
def test_read(self):
129127
self.assertEqual(self.text, self.returned_obj.read())
@@ -544,9 +542,7 @@ def test_interface(self):
544542
# Make sure object returned by urlopen() has the specified methods
545543
for attr in ("read", "readline", "readlines",
546544
"close", "info", "geturl", "getcode", "__iter__"):
547-
self.assertTrue(hasattr(self.text_url_resp, attr),
548-
"object returned by urlopen() lacks %s attribute" %
549-
attr)
545+
self.assertHasAttr(self.text_url_resp, attr)
550546

551547
def test_info(self):
552548
self.assertIsInstance(self.text_url_resp.info(), email.message.Message)

Lib/test/test_urllib2.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1193,15 +1193,15 @@ def test_errors(self):
11931193
r = MockResponse(200, "OK", {}, "", url)
11941194
newr = h.http_response(req, r)
11951195
self.assertIs(r, newr)
1196-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1196+
self.assertNotHasAttr(o, "proto") # o.error not called
11971197
r = MockResponse(202, "Accepted", {}, "", url)
11981198
newr = h.http_response(req, r)
11991199
self.assertIs(r, newr)
1200-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1200+
self.assertNotHasAttr(o, "proto") # o.error not called
12011201
r = MockResponse(206, "Partial content", {}, "", url)
12021202
newr = h.http_response(req, r)
12031203
self.assertIs(r, newr)
1204-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1204+
self.assertNotHasAttr(o, "proto") # o.error not called
12051205
# anything else calls o.error (and MockOpener returns None, here)
12061206
r = MockResponse(502, "Bad gateway", {}, "", url)
12071207
self.assertIsNone(h.http_response(req, r))
@@ -1416,7 +1416,7 @@ def http_open(self, req):
14161416
response = opener.open('http://example.com/')
14171417
expected = b'GET ' + result + b' '
14181418
request = handler.last_buf
1419-
self.assertTrue(request.startswith(expected), repr(request))
1419+
self.assertStartsWith(request, expected)
14201420

14211421
def test_redirect_head_request(self):
14221422
from_url = "http://example.com/a.html"
@@ -1906,9 +1906,9 @@ def test_HTTPError_interface(self):
19061906
url = code = fp = None
19071907
hdrs = 'Content-Length: 42'
19081908
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1909-
self.assertTrue(hasattr(err, 'reason'))
1909+
self.assertHasAttr(err, 'reason')
19101910
self.assertEqual(err.reason, 'something bad happened')
1911-
self.assertTrue(hasattr(err, 'headers'))
1911+
self.assertHasAttr(err, 'headers')
19121912
self.assertEqual(err.headers, 'Content-Length: 42')
19131913
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
19141914
self.assertEqual(str(err), expected_errmsg)

Lib/test/test_urllib2_localnet.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,7 @@ def test_basic(self):
606606
handler = self.start_server()
607607
with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
608608
for attr in ("read", "close", "info", "geturl"):
609-
self.assertTrue(hasattr(open_url, attr), "object returned from "
610-
"urlopen lacks the %s attribute" % attr)
609+
self.assertHasAttr(open_url, attr)
611610
self.assertTrue(open_url.read(), "calling 'read' failed")
612611

613612
def test_info(self):

Lib/test/test_urllibnet.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def test_basic(self):
7171
with self.urlopen(self.url) as open_url:
7272
for attr in ("read", "readline", "readlines", "fileno", "close",
7373
"info", "geturl"):
74-
self.assertTrue(hasattr(open_url, attr), "object returned from "
75-
"urlopen lacks the %s attribute" % attr)
74+
self.assertHasAttr(open_url, attr)
7675
self.assertTrue(open_url.read(), "calling 'read' failed")
7776

7877
def test_readlines(self):

Lib/test/test_urlparse.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1164,14 +1164,13 @@ def test_parse_fragments(self):
11641164
with self.subTest(url=url, function=func):
11651165
result = func(url, allow_fragments=False)
11661166
self.assertEqual(result.fragment, "")
1167-
self.assertTrue(
1168-
getattr(result, attr).endswith("#" + expected_frag))
1167+
self.assertEndsWith(getattr(result, attr),
1168+
"#" + expected_frag)
11691169
self.assertEqual(func(url, "", False).fragment, "")
11701170

11711171
result = func(url, allow_fragments=True)
11721172
self.assertEqual(result.fragment, expected_frag)
1173-
self.assertFalse(
1174-
getattr(result, attr).endswith(expected_frag))
1173+
self.assertNotEndsWith(getattr(result, attr), expected_frag)
11751174
self.assertEqual(func(url, "", True).fragment,
11761175
expected_frag)
11771176
self.assertEqual(func(url).fragment, expected_frag)

0 commit comments

Comments
 (0)