diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index beb18f88c4c6e4..7e3607842fdbdd 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -12,6 +12,7 @@ from test.support import os_helper from test.support import socket_helper from test.support import warnings_helper +from test.support.testcase import ExtraAssertions import os try: import ssl @@ -139,7 +140,7 @@ def unfakeftp(self): urllib.request.ftpwrapper = self._ftpwrapper_class -class urlopen_FileTests(unittest.TestCase): +class urlopen_FileTests(unittest.TestCase, ExtraAssertions): """Test urlopen() opening a temporary file. Try to test as much functionality as possible so as to cut down on reliance @@ -169,9 +170,7 @@ def test_interface(self): # Make sure object returned by urlopen() has the specified methods for attr in ("read", "readline", "readlines", "fileno", "close", "info", "geturl", "getcode", "__iter__"): - self.assertTrue(hasattr(self.returned_obj, attr), - "object returned by urlopen() lacks %s attribute" % - attr) + self.assertHasAttr(self.returned_obj, attr) def test_read(self): self.assertEqual(self.text, self.returned_obj.read()) @@ -601,7 +600,7 @@ def test_URLopener_deprecation(self): urllib.request.URLopener() -class urlopen_DataTests(unittest.TestCase): +class urlopen_DataTests(unittest.TestCase, ExtraAssertions): """Test urlopen() opening a data URL.""" def setUp(self): @@ -640,9 +639,7 @@ def test_interface(self): # Make sure object returned by urlopen() has the specified methods for attr in ("read", "readline", "readlines", "close", "info", "geturl", "getcode", "__iter__"): - self.assertTrue(hasattr(self.text_url_resp, attr), - "object returned by urlopen() lacks %s attribute" % - attr) + self.assertHasAttr(self.text_url_resp, attr) def test_info(self): self.assertIsInstance(self.text_url_resp.info(), email.message.Message) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9ceba05a6e356a..725fe3493c58ae 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -3,6 +3,7 @@ from test.support import os_helper from test.support import requires_subprocess from test.support import warnings_helper +from test.support.testcase import ExtraAssertions from test import test_urllib from unittest import mock @@ -724,7 +725,7 @@ def sanepathname2url(path): return urlpath -class HandlerTests(unittest.TestCase): +class HandlerTests(unittest.TestCase, ExtraAssertions): def test_ftp(self): class MockFTPWrapper: @@ -1179,15 +1180,15 @@ def test_errors(self): r = MockResponse(200, "OK", {}, "", url) newr = h.http_response(req, r) self.assertIs(r, newr) - self.assertFalse(hasattr(o, "proto")) # o.error not called + self.assertNotHasAttr(o, "proto") # o.error not called r = MockResponse(202, "Accepted", {}, "", url) newr = h.http_response(req, r) self.assertIs(r, newr) - self.assertFalse(hasattr(o, "proto")) # o.error not called + self.assertNotHasAttr(o, "proto") # o.error not called r = MockResponse(206, "Partial content", {}, "", url) newr = h.http_response(req, r) self.assertIs(r, newr) - self.assertFalse(hasattr(o, "proto")) # o.error not called + self.assertNotHasAttr(o, "proto") # o.error not called # anything else calls o.error (and MockOpener returns None, here) r = MockResponse(502, "Bad gateway", {}, "", url) self.assertIsNone(h.http_response(req, r)) @@ -1402,7 +1403,7 @@ def http_open(self, req): response = opener.open('http://example.com/') expected = b'GET ' + result + b' ' request = handler.last_buf - self.assertTrue(request.startswith(expected), repr(request)) + self.assertStartsWith(request, expected) def test_redirect_head_request(self): from_url = "http://example.com/a.html" @@ -1833,7 +1834,7 @@ def test_invalid_closed(self): self.assertTrue(conn.fakesock.closed, "Connection not closed") -class MiscTests(unittest.TestCase): +class MiscTests(unittest.TestCase, ExtraAssertions): def opener_has_handler(self, opener, handler_class): self.assertTrue(any(h.__class__ == handler_class @@ -1892,9 +1893,9 @@ def test_HTTPError_interface(self): url = code = fp = None hdrs = 'Content-Length: 42' err = urllib.error.HTTPError(url, code, msg, hdrs, fp) - self.assertTrue(hasattr(err, 'reason')) + self.assertHasAttr(err, 'reason') self.assertEqual(err.reason, 'something bad happened') - self.assertTrue(hasattr(err, 'headers')) + self.assertHasAttr(err, 'headers') self.assertEqual(err.headers, 'Content-Length: 42') expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg) self.assertEqual(str(err), expected_errmsg) diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py index 9cb15d61c2ad4d..23aae1704ee6d6 100644 --- a/Lib/test/test_urllib2_localnet.py +++ b/Lib/test/test_urllib2_localnet.py @@ -11,6 +11,7 @@ from test import support from test.support import hashlib_helper from test.support import threading_helper +from test.support.testcase import ExtraAssertions try: import ssl @@ -442,7 +443,7 @@ def log_message(self, *args): return FakeHTTPRequestHandler -class TestUrlopen(unittest.TestCase): +class TestUrlopen(unittest.TestCase, ExtraAssertions): """Tests urllib.request.urlopen using the network. These tests are not exhaustive. Assuming that testing using files does a @@ -606,8 +607,7 @@ def test_basic(self): handler = self.start_server() with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url: for attr in ("read", "close", "info", "geturl"): - self.assertTrue(hasattr(open_url, attr), "object returned from " - "urlopen lacks the %s attribute" % attr) + self.assertHasAttr(open_url, attr) self.assertTrue(open_url.read(), "calling 'read' failed") def test_info(self): diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py index 49a3b5afdebb2f..6733fe9c6eaaf7 100644 --- a/Lib/test/test_urllibnet.py +++ b/Lib/test/test_urllibnet.py @@ -2,6 +2,7 @@ from test import support from test.support import os_helper from test.support import socket_helper +from test.support.testcase import ExtraAssertions import contextlib import socket @@ -34,7 +35,7 @@ def testURLread(self): f.read() -class urlopenNetworkTests(unittest.TestCase): +class urlopenNetworkTests(unittest.TestCase, ExtraAssertions): """Tests urllib.request.urlopen using the network. These tests are not exhaustive. Assuming that testing using files does a @@ -70,8 +71,7 @@ def test_basic(self): with self.urlopen(self.url) as open_url: for attr in ("read", "readline", "readlines", "fileno", "close", "info", "geturl"): - self.assertTrue(hasattr(open_url, attr), "object returned from " - "urlopen lacks the %s attribute" % attr) + self.assertHasAttr(open_url, attr) self.assertTrue(open_url.read(), "calling 'read' failed") def test_readlines(self): diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 5e429b9259fee7..1fa27257c3c423 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -2,6 +2,7 @@ import unicodedata import unittest import urllib.parse +from test.support.testcase import ExtraAssertions RFC1808_BASE = "http://a/b/c/d;p?q#f" RFC2396_BASE = "http://a/b/c/d;p?q" @@ -101,7 +102,7 @@ (b"%81=%A9", {b'\x81': [b'\xa9']}), ] -class UrlParseTestCase(unittest.TestCase): +class UrlParseTestCase(unittest.TestCase, ExtraAssertions): def checkRoundtrips(self, url, parsed, split, url2=None): if url2 is None: @@ -1033,14 +1034,13 @@ def test_parse_fragments(self): with self.subTest(url=url, function=func): result = func(url, allow_fragments=False) self.assertEqual(result.fragment, "") - self.assertTrue( - getattr(result, attr).endswith("#" + expected_frag)) + self.assertEndsWith(getattr(result, attr), + "#" + expected_frag) self.assertEqual(func(url, "", False).fragment, "") result = func(url, allow_fragments=True) self.assertEqual(result.fragment, expected_frag) - self.assertFalse( - getattr(result, attr).endswith(expected_frag)) + self.assertNotEndsWith(getattr(result, attr), expected_frag) self.assertEqual(func(url, "", True).fragment, expected_frag) self.assertEqual(func(url).fragment, expected_frag)